5

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

有谁知道为什么?

4

1 回答 1

10

我使用它,它对我有用:

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]
于 2011-10-14T23:40:53.790 回答