导航首页 » 技术教程 » 分享下PHP register_globals 值为on与off的理解
分享下PHP register_globals 值为on与off的理解 153 2023-12-09   

register_globals的值可以设置为:On或者Off,我们举一段代码来分别描述它们的不同。

代码:


<form name="frmTest" id="frmTest" action="URL">
<input type="text" name="user_name" id="user_name">
<input type="password" name="user_pass" id="user_pass">
<input type="submit" value="login">
</form>

当register_globals=Off的时候,下一个程序接收的时候应该用$_GET['user_name']和$_GET['user_pass']来接受传递过来的值。(注:当<form>的method属性为post的时候应该用$_POST['user_name']和$_POST['user_pass'])

当register_globals=On的时候,下一个程序可以直接使用$user_name和$user_pass来接受值。

顾名思义,register_globals的意思就是注册为全局变量,所以当On的时候,传递过来的值会被直接的注册为全局变量直接使用,而Off的时候,我们需要到特定的数组里去得到它。所以,碰到上边那些无法得到值的问题的朋友应该首先检查一下你的register_globals的设置和你获取值的方法是否匹配。(查看可以用phpinfo()函数或者直接查看php.ini)

下面来看看这里有什么错误?

看看下面的这段PHP脚本,它用来在输入的用户名及口令正确时授权访问一个Web页面:


<?php
// 检查用户名及口令
if ($username == 'kevin' and $password == 'secret')
$authorized = true;
?>
<?php if (!$authorized): ?>
< 未授权的用户将在这里给予提示 -->
<p>Please enter your username and password:</p>
<form action="<?=$PHP_SELF?>" method="POST">
<p>Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" /></p>
</form>
<?php else: ?>
< 有安全要求的HTML内容 -->
<?php endif; ?>

上面的代码中存在的问题是你可以很容易地获得访问的权力,而不需要提供正确的用户名和口令。只在要你的浏览器的地址栏的最后添加?authorized=1。因为PHP会自动地为每一个提交的值创建一个变量 -- 不论是来自动一个提交的表单、URL查询字符串还是一个cookie -- 这会将$authorized设置为1,这样一个未授权的用户也可以突破安全限制。


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103