My file below checks if the user is ! logged in, and redirects to login page, or does nothing if user is logged in.

$sid= $_COOKIE['SID']; 	
$uid = $_COOKIE['User']; 	
$lastip = $_COOKIE['Cname']; 	 	

$check = mysql_query("SELECT * FROM users WHERE SID = '".$_POST['SID']."'")or die(mysql_error()); 	
while($info = mysql_fetch_array( $check )) 	{ 	
if ($uid != md5($info['id'])) {include 'inc/php/endsession.php'; } 		
if ($lastip != $info['LastIP']) {include 'inc/php/endsession.php'; } 
header( 'Location: login.php' ) ;

My problem is how to redirect to login.php if the cookie SID is set, but contains invalid data that does not exist in my database instead of calling mysql_error().

Recommended Answers

All 9 Replies

Remove the die() and check with if (!$check) { header(); }

$check = mysql_query("SELECT * FROM users WHERE SID = '".$_POST['SID']."'")
if (!$check) { header('Location:' )); }

That 1 gives me this error:
Parse error: syntax error, unexpected T_IF

$check = mysql_query("SELECT * FROM users WHERE SID = '".$_POST['SID']."'");
	if (!$check){

I now have the code above, and it doesn't give any errors, but neither does it redirect to login.php :'(

Put an exit(); after the header.

Still doesn't work! :(

Ok try this instead of assigning $sid to $_POST just leave it as $sid.

Then use this code

if(mysql_num_rows($check) == 0){
   header("Location: whatever.php");

It works! :) :-D

$check = mysql_query("SELECT * FROM users WHERE SID = '$sid'");
      if(mysql_num_rows($check) == 0){

:) Thanks everyone! 8)

Human eyes have no capacity to discern greatness while it abides with them.

Just for your knowledge. The reason why


Does not work is because MySql will return a 0 after you make the select call, hence $check will exists.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.