问题描述
我有一个问题,让我的代码在我很快就要推出的网站上工作。特别是当我登录标题后,登录成功后不会重定向。我多次使用过这些代码,而且我从来没有遇到过任何问题。现在唯一的区别是我正在使用不同的服务器和不同的数据库。这是给我带来麻烦的代码:
<?php
/ *设置所有变量* /
$ email = $ _POST ['email'];
$ password = sha1($ _ POST ['password']); / *散列密码* /
$ conn = mysqli_connect('servername','username','password','databasename')或die('Error connecting to MySQL server');
/ *从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){
echo'登录成功。';
$ row = mysqli_fetch_array($ result);
session_start();
$ _SESSION ['user_id'] = $ row ['id'];
/ *如果真正转入用户表* /
header('location:users_table.php');
如果无效提示他们调整以前的记录* /
else {
echo'< h2>无效登录< h2>< br />';
echo'< h2>点击< a href =javascript:history.go(-1)>这里< / a>返回并调整您的输入。< / h2>';
}
mysqli_close($ conn);
?>
这不是一个正确连接的问题,因为我得到'成功登录'的消息, t重定向。
感谢所有的答案,我尝试删除回显,但我现在得到的只是一个空格页面,我想也许这是我使用的浏览器,所以我切换到另一个,我仍然只是得到一个空白页面,任何其他建议?
标题
语句之前回应任何内容。 echo'登录成功'。
这导致头
工作。
I'm having a problem getting this code to work on the website that I'd like to launch soon. In particular when I sign in the header won't redirect after a successful login. I have used this code before many times and I've never had a problem with it. The only difference now is that I'm using a different server and a different database. Here's the code that is giving me trouble:
<?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);
?>
It's not a matter of it connecting properly because I get the message 'successful Login' but it won't redirect at all.
Thanks for all the answers, I tried removing the echo but all I get now is a blank page, I thought maybe it was the browser I was using so I switched to another and I still just get a blank page, any other suggestions?
You cannot echo anything before your header
statement.
echo 'Logged in Successfully.';
This is causing the header
call to not work.
这篇关于PHP头部命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!