php标题根本没有重定向
Ok so I asked this question before and got a lot of good answers unfortunately none of them changed the outcome. I'm trying to redirect after a 'login' page on the site I'm working on. I'm gathering the id in a session and trying to redirect. This works fine in a lot of the sites I use, the one difference with this site is that it doesn't use localhost, it has a server name. At first I had an echpo before the header and everyone told me that was the problem, I removed it to fix it but nothing changed. Here's the code:
<?php
/*set all the variables*/
$email = $_POST['email'];
$password = sha1($_POST['password']); /* hash the password*/
$conn = mysqli_connect ('servername', 'username', 'password', 'databasename') or die('Error connecting to MySQL server');
/*select the id from the users table that match the conditions*/
$sql = "SELECT id FROM users WHERE email = '$email' AND password = '$password'";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
$count = mysqli_num_rows($result);
if ($count == 1) {
$row = mysqli_fetch_array($result);
session_start();
$_SESSION['user_id'] = $row['id'];
/*If true head over to the users table*/
header('location: users_table.php');
}
/*If invalid prompt them to adjust the previous entry*/
else {
echo '<h2>Invalid Login</h2><br />';
echo '<h2>Click <a href="javascript:history.go(-1)">HERE</a> to go back and adjust your entry.</h2>';
}
mysqli_close($conn);
?>
The only other code on the page are the HTML HEAD and BODY tags. I've even asked one of my old college teachers for help and he said the code looks fine, he can't find the problem. so I'm asking again. Is there maybe a way I can use an HTML or SCRIPT redirect and stay in the session?
Thanks
好的所以我之前问了这个问题并得到了很多好的答案,不幸的是他们都没有改变结果。 我正在尝试在我正在处理的网站上的“登录”页面后重定向。 我在会话中收集id并尝试重定向。 这在我使用的很多站点中工作正常,与此站点的一个区别是它不使用localhost,它有一个服务器名称。 起初我在标题之前有一个echpo,每个人都告诉我这是问题,我删除它来修复它但没有改变。 这是代码: p>
&lt;?php
/ *设置所有变量* /
$ email = $ _POST ['email'];
$ password = sha1($ _ POST ['password']); / *哈希密码* /
$ conn = mysqli_connect('servername','username','password','databasename')或die('连接MySQL服务器时出错');
/ *从users表中选择符合条件的id * /
$ sql =“SELECT id FROM users WHERE email ='$ email'AND password ='$ password'”;
$ result = mysqli_query($ conn,$ sql)或die('查询数据库错误。');
$ count = mysqli_num_rows($ result);
if($ count == 1){
$ row = mysqli_fetch_array($ result);
session_start();
$ _SESSION ['user_id'] = $ row ['id'];
/ *如果为true,请转到users表* /
header('location:users_table.php');
}
/ *如果无效,则提示他们调整上一个条目* /
else {
echo'&lt; h2&gt;无效登录&lt; / h2&gt;&lt; br /&gt;';
echo'&lt; h2&gt;点击&lt; a href =“javascript:history.go(-1)”&gt; HERE&lt; / a&gt; 返回并调整您的条目。&lt; / h2&gt;';
}
mysqli_close($ conn);
&GT?;
code> pre>
页面上唯一的其他代码是HTML HEAD和BODY标记。 我甚至要求我的一位大学老师寻求帮助,他说代码看起来不错,他找不到问题。 所以我再问一遍。 有没有办法可以使用HTML或SCRIPT重定向并留在会话中? p>
谢谢 p>
div>
check for these problems
do you have header already sent? if yes then this is what making it not run. check for php errors or blank lines before
exit;
after header function like
header('Location: users_table.php');
exit;
The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. The field value consists of a single absolute URI.
Try using absolute URI
header("Location: http://www.xxx.com/users_table.php");
You can also add the following on top of your page in case there was an error
error_reporting(E_ALL);
ini_set("display_errors","On");
There are several things that could be in error:
First, you say that you have other html on this page. This will cause an error if your redirect follows the html. For example:
http://php.net/manual/en/function.header.php
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>
Second, you could be colliding sessions. Replace your session start with:
if( !isset( $_SESSION ) ){
session_start();
}
Third, you need to capitalize the Location in your redirect:
/*If true head over to the users table*/
header('location: users_table.php');
should be
/*If true head over to the users table*/
header('Location: /users_table.php');
Last, but not least, turn on error reporting! At the very top of your script before any html, put this code in:
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
if I were you, I would do the following:
1) Looking for Errors: Set error_reporting(E_ALL);
on top of your page to make sure you will see everything. Also you can take a look at php error logs to investigate if you can see any thing wrong there. The place of log files varies depend on your machine setting and configuration.
2) Delete Everything: Remove or comment all the other unnecessary codes, simply use header
and die
function, to see if you they works properly.
<?php
header("Location: http://www.google.com/");
die();
?>
If it works, the continue to number 3, else go to the last step.
3) Check if code executing: Just comment the header
function and then write echo 'ok';
just one line before that. If you DID NOT see that ok
on your rendered-page, then your code is not executing at all. Continue to number 4, else, if you see that ok
, then go to the last step.
4) Look at suspicious places: Try to comment your SQL Query
part, and change your if
statement from if ($count == 1) {
to if (true) {
. If your page redirected, then you have error on grabbing data from your MySQL
tables. If header
didn't worked again, then go to the last step.
5) LAST STEP: Test on the other machines: I would test the same code, on the other machines, including other development environments and/or different hosting servers. If it's working on the other machines, with different configuration, then Problem is Your Machine!
Try to install a fresh server and start over to see if problem exists or not. But if you're problem is still unsolved even on the other machines, try to change your source code Encoding
to UTF-8 without BOM
. It could works at the last moment!
If any of above steps didn't worked for you, just simply leave it to be there, go and buy some beers, try to watch some cooking programs on TV, write a few sms messages and make 1-2 phone calls, when you passed at least 2 hours, then come back here and start over from step 1. Sure you will solve it finally! Never give up! :)
Update: Classic Mistakes! ... check you do not have any new line
or white space
before your PHP opening tages <?php
. Check this out, before doing any other things! :)
(By the way, I add the update after the problem was solved, I took that from the accepted answer)