User Redirection

ok, on that accounts page, are you using the post or session variables to run the query? to view $result, use print_r instead of echo, but even that tells you that the query is working.

I have changed the variables from POST to SESSIONS. When I echo $qry it does show the correct username & password:

SELECT id, redirect FROM users WHERE username = ‘robert’ AND password = ‘123’

When I print_r the $results I am still getting Resource id #3 and a blank page when I echo or print_r $row

Well, I meant:

echo $row['redirect'];

so it shows exactly what you are placing into the “header(location:…”

That Is also showing a blank page. I think because $row on it’s own is not displaying anything either.

Okay, I’m puzzled… LOL

So, we are now getting the query in correctly with username and password.
Next, this is the bad code:
[php]
$result = mysql_query($qry);
$count = (int)mysql_num_rows($result);
$row = mysql_fetch_assoc($result);

if(isset($_SESSION[‘loggedIn’])) {
header("‘Location: " . $row[‘redirect’] . "’");
} else {

}
[/php]
Let’s try this version and tell me what it displays:
[php]
$result = mysql_query($qry);
print_r ($result);
echo “
end of result variable

”;
$count = (int)mysql_num_rows($result);
if($count != 0) {
echo “count of rows=” . $count . “

”;
$row = mysql_fetch_assoc($result);
print_r($row);
echo “
end of row variable

”;
if(isset($_SESSION[‘loggedIn’])) {
die(“Got to: Logged-In section! redirect=***” . $row[‘redirect’] . “***”);
header("‘Location: " . $row[‘redirect’] . "’");
} else {
die(“Got to: Logged-In Failed section!”);
}
}
[/php]
This version prints out EVERYTHING that is part of this routine and will show us what is happening.
Let us know what it shows you! Thanks!

If your puzzled then make that x100 for me. Yourself and richei have been a great help so it should be me thanking you.

Ok when I entered your code I only get the following:

Resource id #3
end of result variable

Yes, Richei has been very helpful on this site! Matter of fact, I am going to Karma him up!

Well, your error says that either the connection to the database has errors or you query is bad.

So, another debug step: Some defaults on PHP servers do not show all of the errors.
Add these lines at the top of the PHP page and it might show further errors we are not seeing…
[php]
error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);
[/php]

After I insert your code I am still getting the same two results:

Resource id #3
end of result variable

OMG! I can’t believe I’m that brain-dead! It was staring at me so long!
I’m SORRY SORRY SORRY!!!

The problem is you are doing a query, then trying to access the data, then selecting rows to access.
Should be query, rows then access… How could I miss that! Anyway, try this version:
[php]
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
$count = mysql_num_rows($row);
if($count != 0) {
if(isset($_SESSION[‘loggedIn’])) {
header("‘Location: " . $row[‘redirect’] . "’");
} else {
die(“Not Logged In!”);
}
}
[/php]
Silly beginner mistake! I have looked at that type of code a million times!
(LOL Probably why I scanned over it!) Let me know!

There is definitely no need to apologise. I have replace the code and am getting:

mysql_num_rows() expects parameter 1 to be resource, boolean given on line 19

Line 19 is: $count = mysql_num_rows($row);

I’ll jump in on this too… there’s something wrong with the query so lets take at it
change
$result = mysql_query($qry);
to
if (!$result = mysql_query($qry)) {
echo (mysql_error() . $qry);
}

[php]
$result = mysql_query($qry);
$count = mysql_num_rows($result);
$row = mysql_fetch_assoc($result);
if($count != 0) {
if(isset($_SESSION[‘loggedIn’])) {
header("‘Location: " . $row[‘redirect’] . "’");
} else {
die(“Not Logged In!”);
}
}
[/php]
Sorry fast typing cuz we are soooo close to get’n er done!

Thanks for the reply Sabmin.

I am getting the same error (it’s just dropped down two lines because of the new code):

mysql_num_rows() expects parameter 1 to be resource, boolean given on line 21

Line 21 is: $count = mysql_num_rows($row);

Haha… No problem although I can also feel you’ve nearly cracked it!

Now I’m getting no errors but it just shows a blank page, any ideas?

2 questions… first, the obvious one and likely a stupid one… the page you are redirecting to is there anything on it? And may I see the the whole code in its most recent update please so I can get cought up and hopefully help move this along faster. :slight_smile:

Yes, repost the page with the error! I only got 3 hours sleep and could use A hand in seeing straighter! LOL…

And, Sabmin, yes, he has the redirect page in place, the username and password are valid andin the database and the value of “redirect” is inside the database and his login page switches to it without issue, but, I am missing something obvious in pulling it from the DB, I am sure! Glad for your help!

Yes there is just a line of text saying you are logged in. There is no other code on the page to which I am redirecting.

Here is the up-to-date version of the coding:

login.php:
[php]<?php
session_start();

require ‘config.php’;

$user_name = $_POST[‘username’];
$user_password = $_POST[‘password’];

$qry = “SELECT id, redirect
FROM
users
WHERE
username = '”.$user_name."’ AND
password = ‘".$user_password."’";

$result = mysql_query($qry);

$count = (int)mysql_num_rows($result);
if($count != 0) {

$row = mysql_fetch_assoc($result);
$_SESSION['uname'] = $_POST['username'];
$_SESSION['upass'] = $_POST['password'];
$_SESSION['loggedIn'] = true;
header('Location: '.$row['redirect']);

} else {

header('Location: index.php');

}

exit;
?>[/php]

account.php
[php]<?php

session_start();

require ‘config.php’;

$user_name = $_SESSION[‘uname’];
$user_password = $_SESSION[‘upass’];

$qry = “SELECT id, redirect
FROM
users
WHERE
username = '”.$user_name."’ AND
password = ‘".$user_password."’";

$result = mysql_query($qry);
$count = mysql_num_rows($result);
$row = mysql_fetch_assoc($result);
if($count != 0) {
if(isset($_SESSION[‘loggedIn’])) {
header("‘Location: " . $row[‘redirect’] . "’");
} else {
die(“Not Logged In!”);
}
}
?>[/php]

config.php
[php]<?php

$connect = mysql_connect(“","”,“") or die (“Could not connect”);
mysql_select_db("
”) or die (“Could not find database”);

?>[/php]

Also I forgot to mention that the page to which I am redirecting does have this peice of code at the top:

[php]<?php
session_start();

if(!$_SESSION[‘loggedIn’]) {
header(‘Location: homepage.php’);
exit();
}
?>[/php]

so if you change to:

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

We would recieve “No results” I assume… Is the query getting the information from the variables or are we getting something else?

Well, awhile ago, I posted a version that echo’d everything.
It was executing the query. But, something else is not working.
His input to the query was correct. It says it pulled data, but, doesn’t show it.
Print_r() just says resource#3…

Sponsor our Newsletter | Privacy Policy | Terms of Service