你好我在php中創建了一個示例登錄系統。但它似乎並不奏效。下面的代碼有什麼問題。示例登錄Php未重定向
login_success.php
<?php
session_start();
if(!$_SESSION['myusername'])
{
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
它說,它「檢查,如果會議沒有註冊,重定向回主頁面。」但每當我把無效的用戶名和密碼它不會去main_login.php,而是去check_login.php
**更新: 無效的用戶名和密碼彈出check_login.php我認爲它應該是在main_login.php上。我會發布我的整個代碼,以便您能不能幫我
check_login.php
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$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 username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("Location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
main_login.php
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="check_login.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
'var_dump($ _ SESSION);' – zerkms 2013-05-01 00:42:37
嘗試'header(「Location:main_login.php」);' – Daniel 2013-05-01 00:47:43
sir我會在那裏放置? – 2013-05-01 00:47:48