php登录脚本无法正常工作并在脚本中停止

时间:2012-08-25 05:46:21

标签: php phpmyadmin

嗨,我是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>

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

2 个答案:

答案 0 :(得分:0)

您的登录脚本应为:     

$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

答案 1 :(得分:0)

我已经解决了...我刚用$ _SESSION($ uname)= $ uname和$ _SESSION($ pword)= $ pword

替换了session_register(“uname”)和session_register(“pword”)