即使存在有效的 CASE,PHP SWITCH 也会进入 DEFAULT?

发布于 2024-12-13 19:14:50 字数 893 浏览 1 评论 0原文

我有这个 SWITCH 块:

switch ($_GET['page']) {

    case "listaOferteTest":
        include("php_views/lista_oferte_test.php");
    break;

    case "categorieOferte":
        include("php_views/categorie_oferte.php");
    break;

    case "pagina":
        include("php_views/pagina.php");
    break;

    default:
        include("php_views/page_not_found_redirect.php");
    break;
}

和一些 php 链接 [它们是动态生成的,但我将粘贴 html]:

<a href='/pagina/termeni-si-conditii/'>
Termeni si conditii
</a>
<a href='/pagina/informatii-utile/'>
Informatii utile
</a>
<a href='/pagina/contact/'>
Contact
</a>

我有一个 .htaccess,我像这样对待链接:

RewriteRule (.*)/(.*)/(.*)/ index.php?page=$1&subPage=$2&subSubPage=$3 [L]

问题:当我测试上面的链接时,我注意到一个奇怪的行为 - 点击随机链接大约 10 次后,也会得到默认链接。这怎么可能?谢谢!

I have this SWITCH block:

switch ($_GET['page']) {

    case "listaOferteTest":
        include("php_views/lista_oferte_test.php");
    break;

    case "categorieOferte":
        include("php_views/categorie_oferte.php");
    break;

    case "pagina":
        include("php_views/pagina.php");
    break;

    default:
        include("php_views/page_not_found_redirect.php");
    break;
}

and some php links [they are dinamicaly generated, but i'll paste the html]:

<a href='/pagina/termeni-si-conditii/'>
Termeni si conditii
</a>
<a href='/pagina/informatii-utile/'>
Informatii utile
</a>
<a href='/pagina/contact/'>
Contact
</a>

I have a .htaccess where i treat the link like this:

RewriteRule (.*)/(.*)/(.*)/ index.php?page=$1&subPage=$2&subSubPage=$3 [L]

The problem: When I tested the links above I noticed a strange behavior - from about 10 clicks on random links one gets also the default. How is this possible? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-12-20 19:14:50

我的第一直觉是拿出调试器并单步执行代码,这样您就可以准确地看到发生了什么。如果您没有安装调试器(为什么不安装?您应该安装,它将改变您的生活!)或者如果您无法使用调试器重现问题,因为它看起来是随机的,那么下一个最好的办法就是添加一些日志记录到默认块,这样您就可以找出它到达那里的原因。我的猜测是 $glob['page'] 与 'pagina' 不完全匹配;也许它有一个前导/尾随空格或斜线,或者它可能是空的,因为提取该值的方式存在错误。像 Monolog 这样的东西就可以完成这项工作在日志记录方面。

My first instinct would be to get the debugger out and step through the code so you can see exactly what's going on. If you don't have a debugger installed (why not? you should, it'll change your life!) or if you can't recreate the problem with a debugger given it's seemingly random nature, then next best thing would be to add some logging to the default block so you can work out why it's getting there. My guess would be that $glob['page'] doesn't match 'pagina' exactly; maybe it's got a leading/trailing space or slash, or maybe it's empty because there's a bug in how that value is being extracted. Something like Monolog would do the job in terms of logging.

舂唻埖巳落 2024-12-20 19:14:50

然后你可以使用if代替switch

对于前。

if (isset($_GET['page'])) {
   if($_GET['page']=='something') {...}
   if($_GET['page']=='something2') {...}
   ...
}

Then u can use, if's instead of switch.

For ex.

if (isset($_GET['page'])) {
   if($_GET['page']=='something') {...}
   if($_GET['page']=='something2') {...}
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文