【问题标题】:.htaccess and filtering $_GET.htaccess 和过滤 $_GET
【发布时间】:2015-07-03 15:43:56
【问题描述】:

您好,我正在编写个人资料页面脚本,在此脚本中,我检查传入的 $_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。

有人知道为什么吗?

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting filter-var


    【解决方案1】:

    我用这个,它对我有用:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^profile$ profile.php
    RewriteRule ^profile/([a-z0-9\-]+)$ profile.php?identity=$1 [NC,L,QSA]
    

    现在,您是如何获得profile/abc 的?如果您尝试在规则中传递字母,它将不起作用,因为您只指定数字([0-9]+)。如果你想传递字母,你需要使用:

    RewriteRule ^profile/([a-z0-9\-]+)/?$ profile.php?identity=$1 [NC,L,QSA]
    

    【讨论】:

    • 谢谢,我会试试你的规则。我不知道,这一定与我的 .htacces 规则有关,因为 php 语句明确排除了任何不是 === 整数的内容,并且在未屏蔽的 url 中传递 $_GET 会抛出其他所有内容。
    • 我刚试了一下,结果还是一样。多么奇怪,也许这是我系统中的一些怪癖。感谢您的帮助
    • 我目前只有特定页面的重写,我处于开发初期。他们只是删除了尾随的 .php 扩展名,并且一个 url 允许 ^page/sub-category。 .htacces 与站点本身一起位于我的根 htdocs 文件夹中。当我说页面现在试图找到“profile/abc”时,为了澄清我的初始帖子,它只是试图找到我列出的任何不良示例,例如“profile/2-2”或“profile/”
    • 可能你的规则有冲突,能不能显示.htaccess?
    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    相关资源
    最近更新 更多