douyousu9691 2012-10-12 17:07
浏览 15
已采纳

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

  • 写回答

4条回答 默认 最新

  • dongshi4773 2012-10-12 17:15
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?