PHP を学習していますが、次のコードを正しく動作させるのに問題があります。基本的に、ログイン ページは正しく表示され、エラーも発生せず、変数が正しく割り当てられているように見えますが、ページをリロードすると同じログイン フォームが表示され、データが渡されていないため、処理されていないように見えます。
私は何度も何度もコードを見て、別の方法を試してみました (同じ結果が得られました!)。
問題になる可能性のあることの 1 つは、サーバーが 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];
}
}
?>