Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php头命令是';行不通_Php_Header - Fatal编程技术网

php头命令是';行不通

php头命令是';行不通,php,header,Php,Header,我有一个问题,让这个代码在我想很快推出的网站上工作。特别是当我登录时,成功登录后标题将不会重定向。我以前使用过很多次这段代码,从来没有遇到过问题。现在唯一的区别是我使用的是不同的服务器和数据库。下面是给我带来麻烦的代码: <?php /*set all the variables*/ $email = $_POST['email']; $password = sha1($_POST['password']); /* hash the password*/ $conn = mysql

我有一个问题,让这个代码在我想很快推出的网站上工作。特别是当我登录时,成功登录后标题将不会重定向。我以前使用过很多次这段代码,从来没有遇到过问题。现在唯一的区别是我使用的是不同的服务器和数据库。下面是给我带来麻烦的代码:

<?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) {
echo 'Logged in Successfully.';

$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);

?>

您不能在
标题
语句之前回显任何内容

echo 'Logged in Successfully.';
这会导致
标题
调用不起作用

if ($count == 1) {
echo 'Logged in Successfully.';
//this statement is creating problem
$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');


}
这是因为你在回音 您应该在文档的开头使用
ob\u start()
,在文档的结尾使用
ob\u end\u flush()


或者不要在
头()之前回显。
。因为我们发现您没有打开错误。所以请打开它。

您不能在
回显后发布
。。。如果这真的有效,你将永远看不到文本(它只是重定向)。(修复删除/注释掉
echo
行)

此外,标头还需要绝对/完整URL(尽管许多浏览器似乎都处理相对URL)

如果您想这样做(在手边显示某种状态),请使用几秒钟后触发的HTML或Javascript重定向

HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Logged in Successfully.</title>
<meta http-equiv="REFRESH" 
  content="5;url=http://www.example.com/users_table.php"></HEAD>
<BODY>
Logged in Successfully.
</BODY>
</HTML>

已成功登录。
已成功登录。
Javascript

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Logged in Successfully.</title>
</HEAD>
<BODY onLoad="setTimeout(function() {
  window.location='http://www.example.com/users_table.php'},5000)">
Logged in Successfully.
</BODY>
</HTML>

已成功登录。
已成功登录。

更好的方法是,允许
users\u table.php
页面显示成功的登录消息,并使用标题位置重定向。

尝试在重定向后添加
exit
。看见