User Redirection

If I type the code you have given I just get a blank page with no errors but it doesn’t redirect.

If I echo the username and password it does return the correct value.

Okay, one last try before throwing this laptop out the window! LOL…
Current version with some added displays to debug this!
[php]
$qry = “SELECT id, redirect
FROM
users
WHERE
username = '”.$user_name."’ AND
password = ‘".$user_password."’";
echo “query=
” . $qry . “

”;
$result = mysql_query($qry);
echo “result of query:
”;
print_r($result);
echo “
end of query…

”;
$count = (int)mysql_num_rows($result);
echo “count of rows: " . $count . “

”;
if($count != 0) {
if(isset($_SESSION[‘loggedIn’])) {
die(“got here, redirect=” . $row[‘redirect’]);
header(”‘Location: " . $row[‘redirect’] . "’");
} else {
die(“Not Logged In!”);
}
} else {
echo "No results found for query: " . $qry;
}
[/php]
Send us back the results…

Along with ernies adjustments throw in an “exit;” after the header line, just to make sure we aren’t accidentally sending anything after it, also make sure there’s nothing after closing the script ex: "?> " best I can do right now as I only have my phone to look at

So far as I know there is nothing else on his page. He said that was the entire file…

Is that correct Robert? As we all know, you can not use an HTML if you are using PHP’s redirect!

While this goes against many principles, and even contradictory in some functions… If after what I posted you still got a blank page it left only 2 possibilities… “$row[‘redirect’]” specifically was a problem or the problem lies in the entire line of “header(”‘Location: " . $row[‘redirect’] . "’");" You say login.php works… I’m going to suggest reverting this line back to [php]header('Location: '.$row[‘redirect’]);[/php](as it is in login.php), I understand things were adjusted after this change and its possible that this needn’t it while the others did… Try this if it doesn’t work go with Ernie’s super debug code and let us know what is returned and hopefully this thing can be put to rest.

Sorry for the late reply. My internet went off last night and as I’m in the UK the time was 12:30am and wasn’t sure when it would get back online.

Thanks for the reply ErnieAlex. Here are the results back from the debugging (including adding the exit; after the header):

[b]query=
SELECT id, redirect FROM users WHERE username = ‘robert’ AND password = ‘123’

result of query:
Resource id #3
end of query…

count of rows: 0

No results found for query: SELECT id, redirect FROM users WHERE username = ‘robert’ AND password = ‘123’[/b]

Yes that is correct. There is nothing after the ?> - No php or html.

Yes when the user logs in for the first time using the login.php script the header works and redirects.

I’ve changed the header and with the debugging in place it returns the same results.

Sorry, but, it is NOT the header it is the query! You just displayed the result and the query is bad.

First, although a bit basic, log into the database in your control panel and please verify these items:

Name of table: users (no caps)

A row of data is in your table “users” that has robert (no caps) 123 and a redirect value.
The data in the row MUST be spelled exactly that way.

Oh, one other problem that could cause this. You didn’t show any code before creating the query.
So, check the connection to the database and make sure that code is the same as the other log-in section.

Lastly, you can add this line right after your line: $result = mysql_query($qry);
It will print out any error that MySQL finds in the query! Good luck!
[php]echo mysql_errno($link) . ": " . mysql_error($link) . “\n”;[/php]
Let me know…

Another option… One I don’t know why we didn’t bring up much earlier… Instead of building this query and running it again… Wile at the login.php, add the redirect field into a session variable and call it that way rather than build a query to the db again every time the user tries to go back to this page for the same data we already have gotten but failed to consider caching

Sabmin, I did mention that idea a long while ago. I suggested putting it into a session variable so it was available on other pages too. But, somehow we went this way… Programming is programming… Customer is always right. LOL… But, if he can’t get this query working, there must be a flaw in his programming so it is good to find out why now so it doesn’t happen again in another page. (At least that is why I was sticking to this so much!)

But, thanks, I agree that is a good fix…

Ok I didn’t see that came in late, also a little frustrated I didn’t think of it sooner :slight_smile:

If we go back to login.php and echo $qry how does it differ from the one that is built in account.php?

If I remember, the queries looked the same. I think it is in his connection area or something else.
He did not show the entire page, he started at creating the query.
I guess, now we wait for him to respond and see where to head next…

back on page 3 when he caught me up he posted I believe the whole of the files… both of which had require config.php that held housed his connection

What’s more if he’s getting resource id#3 he has a connection, he’s jut not getting any rows, which leads me to think there has to be something that differs from the 2 queries

Yep, that’s what I keep telling him… Well, we will know when he stops by…
Thanks, got to fix a computer, will check in later…

Again sorry for the late reply but I am working the night shift for the next couple of days, I have about 1 hour so I thought I would reply.

When I echo $qry in my login script it shows the same as the account.php - however when I echo $row[‘redirect’] in the login script it DOES show the correct redirection url. If I do echo $row[‘redirect’] in the account.php I get a black page.

a black page? is this live where I can see it in motion?

Sorry I meant to say ‘blank’ page. It is only on my PC at the moment but I can upload it to a friends website?

ok in account.php what is displayed if you do print_r($row);

Again it’s just a blank page. However if I do the same on login.php I get:

Array ( [id] => 1 [redirect] => enter.php )

that’s right sorry I forgot where we were… the query is the issue sorry I’m leaving work I’ll think it over some more and get back to it later.

Sponsor our Newsletter | Privacy Policy | Terms of Service