954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Session variable

<?php

session_start(); 


 // makes an array 
 $colors=array('red', 'yellow', 'blue'); 
 // adds it to our session 
 $_SESSION['color']=$colors; 
 $_SESSION['size']='small'; 
 $_SESSION['shape']='round';
 $_SESSION['diameter']='10cm';
   
 $_SESSION['LoggedIn']='user';  
   
 print "Done";

 function fnCheckSession(){
 
 if ($_Session['LoggedIn'] !=null)
 		echo "Session variable exist.";
	else	
 		echo "Session variable does not exist.";
 } 

fnCheckSession();


?>

The following errors appears:

Done
Notice: Undefined variable: _Session in C:\xampp\htdocs\php_exercise\exercise2_2.php on line 20
Session variable does not exist.

Line 20 is: if ($_Session['LoggedIn'] !=null)

Why Session variable does not exist? I thought I already declares session variables above:
$_SESSION['LoggedIn']='user';

davy_yg
Posting Whiz
377 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

php is case sensitive language so use all capital letters for SESSION

if ($_SESSION['LoggedIn'] !=null)
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

Now it works! but I would like to modify my code so that I could check for each session whether it exist or not.

<?php

session_start(); 


 // makes an array 
 $colors=array('red', 'yellow', 'blue'); 
 // adds it to our session 
 $_SESSION['color']=$colors; 
 $_SESSION['size']='small'; 
 $_SESSION['shape']='round';
 $_SESSION['diameter']='10cm';
 $_SESSION['LoggedIn']='user';  
   
 
 function fnCheckSession($svar){
 
 if ($_SESSION[$sesvar] !=null)
 		echo "Session variable exist.";
	else	
 		echo "Session variable does not exist.";
 } 

fnCheckSession(LoggedIn);

session_destroy();

?>


Notice: Use of undefined constant LoggedIn - assumed 'LoggedIn' in C:\xampp\htdocs\php_exercise\exercise2_2.php on line 24

Notice: Undefined variable: sesvar in C:\xampp\htdocs\php_exercise\exercise2_2.php on line 18

Notice: Undefined index: in C:\xampp\htdocs\php_exercise\exercise2_2.php on line 18
Session variable does not exist.

Lots of error. Do you get my point? I am trying to pass the session variable to the fnCheckSession function to check it existance or not.

line 18: if ($_SESSION[$sesvar] !=null)

davy_yg
Posting Whiz
377 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

try this under your $_SESSION lines:

// this is setting the session as a variable
$color = $_SESSION['color'];
// check if the session is not registered
if($color=="")
{
// echo a message saying it isnt
echo"No session registered with the name color.";
}
// check if the session is registered
if(!$color=="")
{
// echo a message saying it is
echo"The session registered with the name color has a value of $color";
}
gotboots
Junior Poster in Training
54 posts since Jun 2011
Reputation Points: 12
Solved Threads: 4
 

am I able to use function ? I must use function to accomplish this task called fnCheckSession.

davy_yg
Posting Whiz
377 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

fnCheckSession(LoggedIn) >>> fnCheckSession('LoggedIn'). Use single quote for string argument.

Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: