.htaccess 和过滤 $_GET

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

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

有谁知道为什么?

最佳答案

我使用这个,它对我有用:

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]

关于.htaccess 和过滤 $_GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7769774/

相关文章:

apache - Ajax 和管理请求通过输入 mod-rewrite 规则停止运行

apache - 重写URL并允许多个查询字符串

css - 无法识别我的 css、js 等

php - htaccess 将别名重定向到主域

.htaccess - 仅通过 HTTP 访问网站,除了 2 个页面

php - =$1 在 url 重写中是什么意思?

javascript - 使用 URL 重写访问静态站点上的菜单项?

nginx - 配置 Nginx 以忽略 url 中的路径

apache - 强制删除 .htaccess 中的 .php 扩展名

.htaccess - Htaccess 将所有文件从一个域中的子目录重定向到另一个域