<!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=utf-8" />
<title>Login</title>
<style>
label{
width:100px;
float:left;
}
</style>
</head>
<body>
<div class="login_form">
<form action="login.php" method="post" >
<p>
<label for="email">E-mail:</label>
<input name="email" type="text" id="email" size="30"/>
</p>
<p>
<label for="password">Password:</label>
<input name="pass" type="password" id="pass" size="30"/>
</p>
<p>
<input name="submit" type="submit" value="Submit"/>
</p>
</form>
</div>
<?php
echo $count;
session_start();
include('configdb.php');
mysql_select_db("upload_site2", $con);
// username and password sent from form
$email=$_POST['email'];
$pass=$_POST['pass'];
// To protect MySQL injection (more detail about MySQL injection)
$email = stripslashes($email);
$pass = stripslashes($pass);
$email = mysql_real_escape_string($email);
$pass = mysql_real_escape_string($pass);
$sql2=mysql_query("SELECT * FROM registration WHERE email='$email' and password='$pass'");
$result=mysql_query($sql2);
echo $email;
// 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("email");
session_register("password");
header("location:member.php");
}
else{echo'wrong';}
?>
</body>
</html>
当我打开此代码的页面时,它直接在屏幕上显示回声(错误(,甚至在我写电子邮件之前传递.too,当我输入电子邮件并传递它时,它不会重定向到"member.php"但它保持在同一页面上...感谢帮助
为了解决在提交表单之前尝试登录的问题,您需要将登录代码包装在 if 语句中,这将确定表单是否已提交。
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// now do the login code
}
它不重定向的第二个问题可能是由于您的回声发生在重定向之前。在重定向之前,您无法回显或打印任何内容。因此,请删除所有这些回声$count和回声$email等。
此外,请检查错误日志或打开错误报告以了解更多详细信息。最后,如果您认为查询不起作用,请使用 mysql_error(( 调试查询。
打开错误报告(在脚本的最顶部(:
error_reporting(E_ALL);
ini_set('display_errors', '1');
您遇到的另一个问题是以下代码:
$sql2=mysql_query("SELECT * FROM registration WHERE email='$email' and password='$pass'");
$result=mysql_query($sql2);
将其更改为
$sql2 ="SELECT * FROM registration WHERE email='$email' and password='$pass'";
$result=mysql_query($sql2);
if(!$result)
{
die('error: ' . mysql_error()); // remove this after you have finished debugging
}
如果标头已发送,则无法使用header("location:member.php");
。 发送标头是因为您在header
函数之前有 html 字符,不仅header
,session_register
也有同样的问题。
尝试:
<?php
session_start();
include('configdb.php');
mysql_select_db("upload_site2", $con);
// username and password sent from form
$email=$_POST['email'];
$pass=$_POST['pass'];
// To protect MySQL injection (more detail about MySQL injection)
$email = stripslashes($email);
$pass = stripslashes($pass);
$email = mysql_real_escape_string($email);
$pass = mysql_real_escape_string($pass);
$sql2=mysql_query("SELECT * FROM registration WHERE email='$email' and password='$pass'");
$result=mysql_query($sql2);
echo $email;
// 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("email");
session_register("password");
header("location:member.php");
}
else{if(!empty($email))$answer='wrong';}
?>
<!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=utf-8" />
<title>Login</title>
<style>
label{
width:100px;
float:left;
}
</style>
</head>
<body>
<div class="login_form">
<form action="login.php" method="post" >
<p>
<label for="email">E-mail:</label>
<input name="email" type="text" id="email" size="30"/>
</p>
<p>
<label for="password">Password:</label>
<input name="pass" type="password" id="pass" size="30"/>
</p>
<p>
<input name="submit" type="submit" value="Submit"/>
</p>
</form>
</div>
<?=$answer?>
</body>
</html>
<?php
session_start();
if (!isset($_SESSION['email'])) {
header('Location: index.php');
}
echo $_SESSION['email'];
?>
<html>
<head>
<title>Secured Page</title>
</head>
<body>
<p>This is secured page with session: <b><?php echo $_SESSION['email']; ?></b>
<br>You can put your restricted information here.</p>
<p><a href="logout.php">Logout</a></p>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file"><br>
<input type="submit" value="Upload" name="upload">
</form>
<p>
<a href="list_files.php">See all files</a>
</p>
</body>
</html>
这是成员.php
重定向页面后添加exit();
。