即使存在有效的 CASE,PHP SWITCH 也会进入 DEFAULT?
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的第一直觉是拿出调试器并单步执行代码,这样您就可以准确地看到发生了什么。如果您没有安装调试器(为什么不安装?您应该安装,它将改变您的生活!)或者如果您无法使用调试器重现问题,因为它看起来是随机的,那么下一个最好的办法就是添加一些日志记录到默认块,这样您就可以找出它到达那里的原因。我的猜测是 $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.
然后你可以使用
if
代替switch
。对于前。
Then u can use,
if
's instead ofswitch
.For ex.