PHP, Little Code Problem

Associate
Joined
29 May 2005
Posts
144
Hello. Im trying to achive a login script. If it detects im logged in, I would like it to include a members page. If it does not detect im logged in I would like it to include the login page. Ive tryed playing around with it, but cant get it working at all.

Code:
<?PHP
   error_reporting(0);
   $var = 0;
	session_start();
	   if(session_is_registered('username')){
		$var = 1;
		echo 'Welcome, you are still logged in.';
            }
	   else{
		$num = 0;
		echo 'Welcome, guest.';
	   }
?>

This bit works perfectly dispaying the echo, of the correct message to the user. The bit I cant do is the includes bit, I tryed the following.

Code:
<?PHP
if($var = ('1')){
      include("members.php");
}
else{
      include("login.php");
}
?>

Im a complete novice at PHP, go easy on me ;)
 
Hi,

the reason you will be experiencing these problems is the use of the = in the if() statement, = is an assignment operator, it assigns the value of the right to the left, so it will always equate to a boolean (true or false) and because anything but 0 is the numerical equivolent to TRUE, it will always surpass as sso. The operator you are looking for is == which is the comparison operator (left and right values are equal)

so change your if() statement to:
Code:
<?PHP
if($var == '1'){
      include("members.php");
}
else{
      include("login.php");
}
?>

There are some other areas can be 'cleaned up', particularly with regards to sessions (session_is_registered() is now discouraged, isset($_SESSION['var']) is preferred where 'var' is the name of the session variable you wish to check)

HTH :)
 
Back
Top Bottom