登录成功后如何重定向?[PHP]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24476899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect after successful login? [PHP]
提问by Praneeth
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 : </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> </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)发送5/sattapesatta.com/httpd.www/login.php 第 6 行警告:session_start():无法发送会话缓存限制器 - 已发送标头(输出开始于 /customers/7/e/5/sattapesatta.com/httpd。 www/login.php:5) in /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 行
回答by Johnny Dew
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.
(编辑)
我也在评论中看到尝试添加完整路径,这不是使其工作所必需的。
回答by Golu
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. 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. The same problem exists when using a single PHP/HTML file.
请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。使用 include 或 require 函数或其他文件访问函数读取代码,并且在调用 header() 之前输出空格或空行,这是一个非常常见的错误。使用单个 PHP/HTML 文件时存在同样的问题。
回答by Ahmed KhaShaba
The Most easy way:
最简单的方法:
Put this before any html in your page (not even a single space should be before this code)
<? php ob_start(); ?>
Use this -It's case sensitive-
<?php header("Location: youraddress.html");?>
- Close it when you finish
<?php ob_end_flush(); ?>
将此放在页面中的任何 html 之前(在此代码之前甚至不应有一个空格)
<? php ob_start(); ?>
使用这个 - 区分大小写 -
<?php header("Location: youraddress.html");?>
- 完成后关闭它
<?php ob_end_flush(); ?>
回答by no_juan
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());