简体   繁体   English

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

[英]How to redirect after successful login? [PHP]

I've created a login system. 我已经创建了一个登录系统。 The registration page works like a charm. 注册页面的工作原理很像。 But there's some problem with the login page. 但是登录页面存在一些问题。 When I try to login, it logs me in, but it stays on the same login page. 当我尝试登录时,它会登录我,但仍保留在同一登录页面上。 I mean there is no visible change in the login page even after logging in. I want it to redirect after a successful login, I've tried headers but for some strange reason it didnt work. 我的意思是即使登录后登录页面也没有可见的变化。我希望登录成功后进行重定向,我尝试过标题,但出于某些奇怪的原因,它没有用。

My code (PHP in the page): 我的代码(页面中的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 in the page: 页面中的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>

What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

Redirectafterlogin.php code (Its basically a guestbook): 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
?>

Error: 错误:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 6 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 6 1 Warning: Cannot modify header information - headers already sent by (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 20 警告:session_start():无法发送会话cookie-/ customers / 7 / e /中已经由发送的标头(输出从/customers/7/e/5/sattapesatta.com/httpd.www/login.php:5开始)第6行的5 / sattapesatta.com / httpd.www / login.php警告:session_start():无法发送会话缓存限制器-标头已发送(输出从/customers/7/e/5/sattapesatta.com/httpd开始。第6行/customers/7/e/5/sattapesatta.com/httpd.www/login.php中的www / login.php:5)1警告:无法修改标头信息-已发送的标头(输出始于/ customers /customers/7/e/5/sattapesatta.com/httpd.www/login.php中的/7/e/5/sattapesatta.com/httpd.www/login.php:5)。

When you want to do a redirection with the function header, you have to write it properly for it to work. 当您想使用函数头进行重定向时,必须正确编写它才能起作用。

header('Location: ...');

If you don't use the capital letter and the space after the colon, it will not work. 如果您不使用大写字母和冒号后的空格,则它将不起作用。 Please read the manual on php.net . 请阅读php.net上的手册。

(Edit) (编辑)
I also saw in the comments to try to add the complete path, this is not necessary to make it work. 我还在注释中看到尝试添加完整的路径,这不是使其正常工作所必需的。

Try this 尝试这个

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

Also from the PHP docs 同样来自PHP文档

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. 请记住,必须先通过常规HTML标记,文件中的空白行或从PHP发送任何实际输出,然后调用header()。 It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. 读取包含,或要求函数或另一个文件访问函数的代码,并在调用header()之前输出空格或空行,这是一个非常常见的错误。 The same problem exists when using a single PHP/HTML file. 使用单个PHP / HTML文件时,存在相同的问题。

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

The Most easy way: 最简单的方法:

  1. Put this before any html in your page (not even a single space should be before this code) <? php ob_start(); ?> 在你的页面的任何HTML之前将这个(甚至不是一个单一的空间应该是这样的代码之前) <? php ob_start(); ?> <? php ob_start(); ?>

  2. Use this -It's case sensitive- <?php header("Location: youraddress.html");?> 使用它-区分大小写- <?php header("Location: youraddress.html");?>

  3. Close it when you finish <?php ob_end_flush(); ?> 完成<?php ob_end_flush(); ?>时将其关闭<?php ob_end_flush(); ?> <?php ob_end_flush(); ?>

Remove white spaces and newlines before header: 删除标题前的空白和换行符:

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

This should be: 应该是:

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

And your: 和你的:

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

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

Should be just: 应该只是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM