嗨,我在这里有一个 PHP 中的登录注销功能,用户可以在其中登录。如果他是版主,他将被重定向到 moderator.php,如果他是代理,他将转到 agent.php 页面。
在他们应该登录的 index.php 中。这是代码:
<form name="form1" method="post" action="check_login.php">
<center><div id='login_header'><b><font face='Arial Black' color='black' size='4px'>Sign in to Minquep!</font></b></div></cen
<br/><br/>
<center><label>Username:</label><input type='text' name='myusername' size='20'><br/><label>Password:</label>
<input type='password' name='mypassword' size='20'><br/>
<input type='text' name='myfirstname' style='display:none;'>
</center>
<br/><input type='submit' value='Submit' name='submit' class='submit'>
</form>
check_login.php 是这样的:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="minquep_test"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or
die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE login='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_fetch_array($result);
if($count['user_type']== "agent"){
echo '<script type="text/javascript">alert("You have logged in successfully!\n"); return false;</script>';
echo "<meta http-equiv=\"refresh\" content=\"0;URL=pages/agent.php\">";
session_register("myusername");
session_register("mypassword");
//header("location:pages/agent.php");
}
else if ($count['user_type']== "moderator"){
echo '<script type="text/javascript">alert("You have logged in successfully!\n"); return false;</script>';
echo "<meta http-equiv=\"refresh\" content=\"0;URL=pages/moderator.php\">";
session_register("myusername");
session_register("mypassword");
//header("location:pages/moderator.php");
}
else {
echo "<script type='text/javascript'>alert('Invalid Login! Please Try Again!');</script>";
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
?>
在 moderator.php 和 agent.php 中,我将登录用户的用户名输出为:
<?php
session_start();
print("<b><h2>Hi! $_SESSION[myusername] (You are logged in as moderator / agent) </h2>");
?>
在我的 logout.php 中,我只是销毁会话并将其重定向回 index.php
<?php
session_start();
session_destroy();
echo "<meta http-equiv=\"refresh\" content=\"0;URL=../index.php\">";
exit;
?>
所以问题是,在我第一次使用版主或代理帐户登录测试期间......登录后用户名不显示。它只是说“嗨!您以版主身份登录”或“嗨!您已登录作为代理”。但是在注销并再次登录后,用户名已经显示出来了……就像它应该的样子:“嗨,你的名字!你以主持人的身份登录”。或“您好!您以代理身份登录”。
那么我**$_SESSION("myusername")**
在第一次 check_login 期间不起作用的问题可能是什么。
谢谢。