IF statement header redirect error...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

IF statement header redirect error...

Post by sn202 »

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.

Code: Select all

<?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( "linuxproj", "***", "****" ) 
        or die( "could not connect" ); 
#select the specified database 
$rs = @mysql_select_db ( "db_sn202", $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: http://www.ecs.soton.ac.uk/~sn202/admin.php");   
    header("Connection: close"); 
} 
if ($role == "tech") { 
    header("HTTP/1.1 301 Moved Permanently"); 
    header ("Location: http://www.ecs.soton.ac.uk/~sn202/tech.php"); 
    header("Connection: close"); 
} 
if ($role == "student") { 
    header("HTTP/1.1 301 Moved Permanently"); 
    header ("Location: http://www.ecs.soton.ac.uk/~sn202/student.php"); 
    header("Connection: close"); 
} 
if ($role == "test") { 
    header("HTTP/1.1 301 Moved Permanently"); 
    header ("Location: http://www.ecs.soton.ac.uk/~sn202/test.php"); 
    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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

yeah my brain does this stuff too after 14 hrs lol

i noticed your mispelling some variables

$refer vs $referer

whenever your debugging or even just developing, its always very helpfull to turn error_reporting way up. youll see all undefined variables and undefined indices etc...

try it, you would have found this little bug instantly

error_reporting(E_ALL);
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

Post by sn202 »

Hi yeah, cheers for that, sorted out the variables and turned error reporting on, however its still not doing anything. I added an echo to make sure it was finding user role and it does echo the role, but still doesn't redirect.... Any ideas?

Si.
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

Post by sn202 »

ok now i've added $row = mysql_fetch_object($rs); But i'm getting "undefined varible" and "undefined property" errors script below:

Code: Select all

<?php
                	$username = $_POST['username'];
		$self =		$_SERVER['PHP_SELF'];
		$refer =	$_SERVER['HTTP_REFERER'];
		
error_reporting(E_ALL);
#if either form field is empty return to the login page
if( ( !$username )  )
	  { header( "Location:$refer" ); exit(); }
#connect to MYSQL
$conn = @mysql_connect( "linuxproj", "sn202", "e1420518" )
				or die( "could not connect" );
#select the specified database
$rs = @mysql_select_db ( "db_sn202", $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() );
$row = mysql_fetch_object($rs);
#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:$refer" ); exit(); }
#write data
if ($row->$role == "admin") { 
	header("HTTP/1.1 301 Moved Permanently"); 
	header ("Location: http://www.ecs.soton.ac.uk/~sn202/admin.php");  
	header("Connection: close"); 
} 
if ($row->$role == "tech") {
	header("HTTP/1.1 301 Moved Permanently"); 
	header ("Location: http://www.ecs.soton.ac.uk/~sn202/tech.php"); 
	header("Connection: close"); 
} 
if ($row->$role == "student") {
	header("HTTP/1.1 301 Moved Permanently"); 
	header ("Location: http://www.ecs.soton.ac.uk/~sn202/student.php"); 
	header("Connection: close"); 
} 
if ($row->$role == "test") {
	header("HTTP/1.1 301 Moved Permanently"); 
	header ("Location: http://www.ecs.soton.ac.uk/~sn202/test.php"); 
	header("Connection: close"); 
} 		
?>
Please help, haven't got long left to do this!

Si.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Before assigning data to variables from get, post etc you need to make sure they exist:

Code: Select all

if(isset($_POST['username'])){        
   $username = $_POST['username'];
}
Also $row->$role doesnt need the second $, it should just be $row->role

etc.
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

Post by sn202 »

Cheers! it was the extra $. Thanks a lot.

Si.
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

Post by sn202 »

Cheers! it was the extra $. Thanks a lot.

Si.
Post Reply