成功登录后如何重定向? [PHP]

时间:2014-06-29 14:18:31

标签: php login

我已经创建了一个登录系统。注册页面就像一个魅力。但登录页面存在一些问题。当我尝试登录时,它会将我登录,但它会保留在同一个登录页面上。我的意思是即使在登录后登录页面也没有明显的变化。我希望它在成功登录后重定向,我已经尝试过标题但是由于一些奇怪的原因它没有用。

我的代码(页面中的PHP):

<?php  //Start the Session
session_start();
 require('connect.php');
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//3.1.1 Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";

$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count == 1){
$_SESSION['username'] = $username;
header("location:redirectafterlogin.php"); //header to redirect, but doesnt work
}else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
echo "Invalid Login Credentials.";
}
}

?>    

页面中的HTML:

<title> Login</title>
<link rel="stylesheet" type="text/css" href="usr_style.css" />
</head>
<body>
<!-- Form for logging in the users -->

<div class="register-form">

<p>Please login to continue</p>

<h1>Login</h1>
<form action="login.php" method="POST">
    <p><label>User Name : </label>
    <input id="username" type="text" name="username" required placeholder="username" /></p>

     <p><label>Password&nbsp;&nbsp; : </label>
     <input id="password" type="password" name="password" required placeholder="password" /></p>

    <a class="btn" href="register.php">Register</a>
    <input class="btn" type="submit" name="submit" value="Login" />
    </form>
</div>
</body>
</html>

我做错了什么?

编辑:

Redirectafterlogin.php代码(基本上是留言簿):

<body>
<div class="main">
<br>


        <div id="title"><h2>Post</h2></div>
        <br>
    <div id="f">
    <form id="form1" name="form1" method="post" action="addpost.php">

    <table id="g">

        <td>Post</td>
        <td><textarea name="comment" placeholder="Please type your post here...." rows=10 cols=50 id="comment"></textarea></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input id="btn" type="submit" name="Submit" value="Post" /> <input id="btn" type="reset" name="Submit2" value="Reset" /></td>
    </tr>
    </table>

    </form>

</div>
<hr>



<?php

$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY ID DESC LIMIT 50";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>


<div id="post_info">
<br>


<div id="author">
Name:
<?php echo htmlspecialchars($rows['name']); ?> 
</div>

<div id="time">
Time: <?php echo htmlspecialchars($rows['datetime']); ?>
</div>

<br>

<div id="post">
<pre><?php echo htmlspecialchars($rows['comment']); ?></pre>
</div>

</div>
<br>

<?php
}
mysql_close(); //close database
?>

错误:

  

警告:session_start():无法发送会话cookie - 已在/ customers / 7中发送的(已从/customers/7/e/5/sattapesatta.com/httpd.www/login.php:5开始的输出)第6行/e/5/sattapesatta.com/httpd.www/login.php警告:session_start():无法发送会话缓存限制器 - 已发送的标头(输出从/comersomers/7/e/5/sattapesatta.com开始/httpd.www/login.php:5)在/customers/7/e/5/sattapesatta.com/httpd.www/login.php第6行1警告:无法修改标题信息 - 已经发送的标题(输出已开始)在/customers/7/e/5/sattapesatta.com/httpd.www/login.php:5)在/customers/7/e/5/sattapesatta.com/httpd.www/login.php第20行

4 个答案:

答案 0 :(得分:1)

如果要使用函数头进行重定向,则必须正确编写才能使其正常工作。

header('Location: ...');

如果你不使用冒号后的大写字母和空格,它将无效。请阅读php.net上的手册。

(编辑)
我还在评论中看到尝试添加完整路径,这不是必要的,以使其工作。

答案 1 :(得分:1)

试试这个


    header("Location: redirectafterlogin.php");
    exit;

同样来自PHP文档

  

请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。使用include或require,函数或其他文件访问函数读取代码是一个非常常见的错误,并且在调用header()之前输出空格或空行。使用单个PHP / HTML文件时存在同样的问题。

http://www.php.net/manual/en/function.header.php

答案 2 :(得分:0)

最简单的方法:

  1. 在页面中的任何html之前放置此内容(在此代码之前,甚至不应该只有一个空格)<? php ob_start(); ?>

  2. 使用此选项 - 区分大小写 - <?php header("Location: youraddress.html");?>

  3. 完成<?php ob_end_flush(); ?>
  4. 后关闭它

答案 3 :(得分:-1)

删除标题前的空格和换行符:

session_start();
 require('connect.php');

这应该是:

session_start();
require('connect.php'); // whitespace removed

你的:

$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";

$result = mysql_query($query) or die(mysql_error());

应该只是:

$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());