我正在学习 PHP,并且在使以下代码正常工作时遇到问题。基本上登录页面显示正确,并且没有错误并且变量似乎被正确分配,但是在页面重新加载时我只是得到相同的登录表单,看起来数据要么没有被传递,因此没有被采取行动。
我一遍又一遍地查看代码,甚至尝试了不同的方法(产生了相同的结果!)所以如果有人能帮助我花一分钟时间并指出正确的方向,那就太好了。
可能存在问题的一件事是我的服务器运行的是 5.3.9,而我正在使用的书是 PHP5,所以我调用的某些函数可能已被弃用。那会很痛苦...
<?php
include_once "common_db.inc";
$register_script = "register.php";
if (!isset ($userid)) {
login_form();
exit;
} else {
session_start();
session_register ("userid", "userpassword");
$username = auth_user ($_POST['userid'], $_POST['userpassword']);
if (!$username) {
$PHP_SELF = $_SERVER['PHP_SELF'];
session_unregister ("userid");
session_unregister ("userpassword");
echo "Failed to authorize. " .
"Enter a valid DX number and password." .
"Click the link below to try again.<br>\n";
echo "<a href=\"$PHP_SELF\">login</a><br>";
echo "Click the following link to register<br>\n";
echo "<a href=\"$register_script\">Register</a>";
exit;
} else {
echo "Welcome, $username!";
}
}
function login_form()
{
global $PHP_SELF;
?>
<form method="post" action="<?php echo "$PHP_SELF"; ?>">
<div align="center"><center>
<h3>Please login to use the page you requested</h3>
<table width="200" cellpadding="5">
<tr>
<th width="18%" align="right" nowrap>id</th>
<td width="82%" nowrap>
<input type="text" name="userid" />
</td>
</tr>
<tr>
<th width="18%" align="right" nowrap>password</th>
<td width="82%" nowrap>
<input type="password" name="userpassword" />
</td>
</tr>
<tr>
<td colspan="2" width="100%" nowrap>
<input type="submit" value="login" name="Submit" />
</td>
</tr>
</table>
</center>
</div>
</form>
<?php
}
function auth_user($userid, $userpassword)
{
global $dbname, $user_tablename;
$link_id = db_connect($dbname);
$query = "SELECT DXNumber FROM $user_tablename WHERE DXNumber = '$userid'
AND userpassword = password ('$userpassword')";
$result = mysql_query ($query);
if (!mysql_num_rows($result)){
return 0;
}else{
$query_data = mysql_fetch_row($results);
return $query_data[0];
}
}
?>