使用htaccess时是否不需要使用$ _GET ['varName']?

jme*_*zes 4 php .htaccess

我刚写了一个htaccess文件和一个简单的规则.

RewriteRule ^/?([a-z]{2})/([0-9]{4})/?$ /run/run.php?country=$1&year=$2 [NC,L]
This does http://www.localhost/us/2014
Run Code Online (Sandbox Code Playgroud)

在php页面上,我意外地做了:

echo $country.' '.$year;
Run Code Online (Sandbox Code Playgroud)

这给了我下面的输出,这是正确的.

us 2014
Run Code Online (Sandbox Code Playgroud)

我没有做:

$country = $_GET['country'];
$year = $_GET['year'];
Run Code Online (Sandbox Code Playgroud)

但它仍然奏效.这是正常的行为吗?我可以将此用于其他规则和网站吗?我在Windows 7 Home Premium上使用WAMP.

Nau*_*hal 13

你可能已经register_globals在php.ini中打开了.这就是为什么你要使用数组索引的名称变量(这里$_GET).开启并非最佳做法register_globals.

你可以在这里阅读更多内容,为什么register_globals是坏的.

  • @jmenezes:不会.你的全局变量不会有任何问题. (2认同)