Reputation: 53
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
Upvotes: 1
Views: 580
Reputation: 9427
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)
Upvotes: 0
Reputation: 1088
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;
Upvotes: 1
Reputation: 8301
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 );
Upvotes: 0
Reputation: 95161
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");
Upvotes: 0