<?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 !=null)

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

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

if ($_SESSION['LoggedIn'] !=null)

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)

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";
}

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

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

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.