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
。看见