0
<?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';

4
Contributors
5
Replies
8
Views
5 Years
Discussion Span
Last Post by ko ko
0

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

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

Edited by urtrivedi: n/a

0

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)

Edited by davy_yg: n/a

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

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.