Forum Moderators: coopster

Message Too Old, No Replies

IF statement header redirect error...

         

sn202

1:24 am on Dec 17, 2004 (gmt 0)

10+ Year Member



ok i'm using this code now to redirect the site to a certain page depending on the user's role which is calculated from the username using a SQL query.

PHP Code:
<?php $username = $_POST['username'];
$self = $_SERVER['PHP_SELF'];
$refer = $_SERVER['HTTP_REFERER'];
#if either form field is empty return to the login page
if( (!$username ) )
{ header( "Location:$referer" ); exit(); }
#connect to MYSQL
$conn = @mysql_connect( "localhost", "***", "****" )
or die( "could not connect" );
#select the specified database
$rs = @mysql_select_db ( "dbname", $conn )
or die( "could not select database" );
#create the sql query
$sql="select role from users where username='$username'";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( mysql_error() );
#get number of rows that match username and password
$num = mysql_numrows( $rs );
#if there is a match the login is authenticated
if( $num > 0 )
{ $msq = "Welcome $username - your log-in succeeded"; }
else #or return to login page
{ header( "Location:$referer" ); exit(); }
if ($role == "admin") {
header("HTTP/1.1 301 Moved Permanently");
header ("Location: [example.uk...]
header("Connection: close");
}
if ($role == "tech") {
header("HTTP/1.1 301 Moved Permanently");
header ("Location: [example.uk...]
header("Connection: close");
}
if ($role == "student") {
header("HTTP/1.1 301 Moved Permanently");
header ("Location: [example.uk...]
header("Connection: close");
}
if ($role == "test") {
header("HTTP/1.1 301 Moved Permanently");
header ("Location: [example.uk...]
header("Connection: close");
}
?>

But its doing nothing. Just sitting on roles.php. I'm thinking it may not know what $role is as it is a result of a SQL query and I can't think how to declare it as the result from the sql?

Too tired to think, been sat at a computer for about 14 hours straight now and my brains not working anymore, this is due in tomorrow though, so any help will be much appreciated! cheers.

Si.

[edited by: jatar_k at 4:38 am (utc) on Dec. 17, 2004]
[edit reason] no personal urls thanks [/edit]

dreamcatcher

2:01 am on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi sn202, welcome to WebmasterWorld.

Your $role variable has no value, which is your problem. You need to use one of the mysql fetch queries to get your data.

Directly beneath $rs = mysql_query( $sql, $conn ) add this:

$row = mysql_fetch_object($rs);

Then instead of $role, use $row->role.

if ($row->role == "tech")
{
rest of code;
}

Hope that helps. You might also want to check the mysql website on how to display data from a database. In this example I have used mysql_fetch_object, but there are other ways to fetch data.

dc

sn202

10:34 am on Dec 17, 2004 (gmt 0)

10+ Year Member



Hi yeah entered the code you suggested but am getting an undefined variable error for "role" and an undefined property. Tried getting rid of the role bit and just having if ($row == "student") { And this just displays the undefined property.? Any idea?

Si.

Salsa

12:42 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



Try adding the bolded lines to your originally posted code:

$rs = mysql_query( $sql, $conn )or die( mysql_error() ); 
[b]$query_data = mysql_fetch_array($rs);
$role = $query_data['role'];[/b]

Actually, dreamcatcher's suggestion should have worked--and saved a line of code--but requried a little more changing, and an object isn't necessary in this case. But, because you got the undefined propery message, that would probably mean that your query isn't returning a result, one way or the other. What do you get when you echo out $num?