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

Pra*_*eth 3 php authentication

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

我的代码(页面中的 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.";
}
}

?>    
Run Code Online (Sandbox Code Playgroud)

页面中的 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>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

编辑:

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
?>
Run Code Online (Sandbox Code Playgroud)

错误:

警告:session_start():无法发送会话 cookie - 标头已由 /customers/7/e/ 中的(输出从 /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5 开始)发送5/sattapesatta.com/httpd.www/login.php 第 6 行警告:session_start():无法发送会话缓存限制器 - 标头已发送(输出从 /customers/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 行

Joh*_*Dew 5

当您想使用函数头进行重定向时,您必须正确编写它才能正常工作。

header('Location: ...');
Run Code Online (Sandbox Code Playgroud)

如果不使用大写字母和冒号后面的空格,则不起作用。请阅读php.net上的手册。

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