.htaccess 和过滤 $_GET
您好,我正在编写一个配置文件页面脚本,在这个脚本中,我检查传入的 $_GET 变量的值并验证它是否是一个整数,然后我根据 $_SESSION 值验证该值以确认他们只能访问自己的帐户。代码如下所示:
// validate $_GET field
if(isset($_GET['identity']) && filter_var($_GET['identity'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
if(isset($_SESSION['user_identity']) && ((int)$_SESSION['user_identity'] === (int)$_GET['identity'])) { // if session exists and is === $_GET['identity']
// Proceed with code
例如,如果我尝试传递“0”、“2-2”、“abc”或不传递任何值作为 $_GET 值,则查询会正确失败并将它们重定向到主页。
然后我尝试做的是更改我的 .htaccess 文件以将 URL 映射到“profile/1”,只是为了整理它。
RewriteRule ^profile$ profile.php
RewriteRule ^profile/([0-9]+)$ profile.php?identity=$1 [NC,L]
我现在发现页面不再使用上面那些无效的 $_GET 参数进行重定向。它只是试图找到“profile/abc”。
有谁知道为什么?
Hello I am writing a profile page script, in this script I check the value of an incoming $_GET variable and validate that it is an integer, I then validate this value against a $_SESSION value to confirm that they can only access their own accounts. The code looks like this:
// validate $_GET field
if(isset($_GET['identity']) && filter_var($_GET['identity'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
if(isset($_SESSION['user_identity']) && ((int)$_SESSION['user_identity'] === (int)$_GET['identity'])) { // if session exists and is === $_GET['identity']
// Proceed with code
This works fine for instance if I try to pass '0','2-2','abc' or no value as the $_GET value the query correctly fails and redirects them to the home page.
What I then tried to do was alter my .htaccess file to map the URLs to 'profile/1' just to tidy it up.
RewriteRule ^profile$ profile.php
RewriteRule ^profile/([0-9]+)$ profile.php?identity=$1 [NC,L]
What I found now is that the page doesn't redirect any more using those invalid $_GET parameters above. It just tries to find 'profile/abc.
Does anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用这个并且它对我有用:
现在,你是如何获得
profile/abc
的?如果您尝试在规则中传递字母,它将不起作用,因为您只指定数字([0-9]+)
。如果您想传递信件,您将需要使用:I use this and it works for me:
Now, how did you get
profile/abc
? if you try to pass letters in the rule it wont work since you only specify numbers([0-9]+)
. If you want to pass letters you will need to use: