logout.php页面不会将我重定向到member.php页面

时间:2015-02-06 09:14:06

标签: php html

<!doctype HTML>
<html>
<head>
<title>register</title>
</head>
<body>
<p> <a href="register.php">register</a> | <a href="login.php">login</a></p>
<h3>registration form</h3>
<form action="" method="POST">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
emailid: <input type="text" name="email"><br/>
<input type="submit" value="login" name="submit"/>
</form>
<?php
if(isset($_POST["submit"])){
$user=$_POST['user'];
$pass=$_POST['pass'];
$email=$_POST['email'];

$con=mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('user_registration') or die ("connect select DB");

$query=mysql_query("SELECT * FROM login WHERE username='".$user."'" );
$numrows=mysql_num_rows($query);
if($numrows==0)
{
$sql="INSERT INTO login(username,password,emailid ) VALUES('$user','$pass','$email')";
$result=mysql_query($sql);

if($result){
echo "acc succ created";
}
else { echo "failure"; }
}
else { echo "username already exist"; }
}
?>
</body>
</html>

**LOGIN FORM** please go through this login code

一切正常,但登录页面不会将我重定向到member.php页面并保持在同一页面上。

<!DOCTYPE html>
<html>
<head>
<title>login</title>

</head><body>
<p><a href="register.php">register</a> | <a href="login.php">login</a></p>
<h3> login form</h3>
<form action="" method="POST">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" value="login" name="submit"/>
</form>
<?php
if(isset($_POST["submit"])){
$user=$_POST['user'];
$pass=$_POST['pass'];

$con=mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('user_registration') or die ("connect select DB");

$query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['username'];
$dbpassword=$row['password'];
}
if($user == $row['username'] && $pass == $row['password'])
{
session_start();

$_SESSION['sess_user']=$user;


header("location:member.php");
}
}
else {
echo "invalid username or password";
}
}
?>
</body>
</html>

**MEMBER PAGE**
<?php
session_start();
if(!isset($_SESSION["sess user"])){
header("location:login.php");  

} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>welcome</title>

</head>
<body>
<h2> welcome <?php $_SESSION['sess_user'];?> ! <a     
href="logout.php">logout</a></h2>
<p>
jfdsjkfhdsf dsf ds fds f sdf ds fds f ds fd sf ds f
</p>
</body>
</html>
<?php
}
?>

MY LOGOUT PAGE

<?php
session_start();
unset($_Session['sess_user']);
session_destroy();
header("location:login.php");
?>

3 个答案:

答案 0 :(得分:2)

因为在输出已经发送(即你的HTML)之后你不能使用header()功能。在任何输出之前放置重定向逻辑。

答案 1 :(得分:0)

请在页面顶部写下<?php ob_start();?>。希望这会有所帮助。

答案 2 :(得分:-1)

  

我的登录页面

header("location:member.php")

而不是

header("location:login.php")