php登录脚本不起作用,并在脚本中停止


php login script not working and stops in the script

hi我是php和mysql的新手,我正在一个网站上工作,我正在阅读一些关于如何创建登录页面的教程,当我尝试创建自己的登录页面时,它似乎停止在脚本中,如果登录成功,则不会继续下一页

这是代码

master1.php
<form action="loginscript.php" method="post" name="loginform">
<table class="logintable">
<!-- username-->
<tr>
<td class="tdalignright"><strong>
Username:</strong>
</td>
<td>
<input type="text" name="uname" id="uname"/>
</td>
</tr>
<!-- password -->
<tr>
<td class="tdalignright"><strong>
Password:</strong>
</td>
<td>
<input type="password" name="pword" id="pword"/>
</td>
</tr>
<!-- login button -->
<tr>
<td>
</td>
<td class="tdalignright">
<input type="submit" name="Submit" value="Login" />
</td>
</tr>
</table> 
</form>

这是登录脚本

loginscript.php
<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="seelsdb"; // Database name 
$tbl_name="tblteacher"; // 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");
// username and password sent from form 
$uname=$_POST['uname']; 
$pword=$_POST['pword']; 
// To protect MySQL injection (more detail about MySQL injection)
$uname = stripslashes($uname);
$pword = stripslashes($pword);
$uname = mysql_real_escape_string($uname);
$pword = mysql_real_escape_string($pword);
$sql="SELECT * FROM $tbl_name WHERE teacherEmail='$uname' and teacherPass='$pword'";
$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("uname");
session_register("pword"); 
header("location:schedule.php");
}
else {
echo "Wrong Username or Password";
}
?>

这是成功登录后的下一页

schedule.php
<?php
session_start();
if(!session_is_registered(uname)){
header("location:master1.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
LOGIN SUCCESSFUL
</body>
</html>

我似乎无法追踪错误,希望你们能帮忙。。。感谢

您的登录脚本应该是:

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="seelsdb"; // Database name 
$tbl_name="tblteacher"; // 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");
// username and password sent from form 
$uname=$_POST['uname']; 
$pword=$_POST['pword']; 
// To protect MySQL injection (more detail about MySQL injection)
$uname = stripslashes($uname);
$pword = stripslashes($pword);
$uname = mysql_real_escape_string($uname);
$pword = mysql_real_escape_string($pword);
$sql="SELECT * FROM $tbl_name WHERE teacherEmail='".$uname."' and teacherPass='".$pword."'";
$result=mysql_query($sql);
if(!$result)
{
 echo 'Mysql did not respond to query'.mysql_error();
}
// 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("uname");
session_register("pword"); 
header("location:schedule.php");
}
else {
echo "Wrong Username or Password";
}
?>

但是,我强烈建议您选择MySQLi而不是mysql_*函数。如果你觉得舒服,也可以参考PDO

我已经解决了它……我刚刚用$_session($uname)=$uname和$_session[$pword]=$pword 替换了session_register("uname")和session_registor("pword")