tags:

views:

60

answers:

2

Hi I am setting a session variable inside a function studNameDetails1() while I am trying to retrieve it in a function ViewMark(). These are my two functions but there is no result:

function studNameDetails1()
{
     $_SESSION['ATTsub']=$sub_id = $ID[5];
}
function ViewMark()
{
      echo $_SESSION['ATTsub'];
}

When I echo the value in viewMark(), there is no value.

+2  A: 

This is the way to handle sessions.

<?php // this starts the session session_start(); 
// echo variable from the session, we set this on our other page echo "Our color value 
is ".$_SESSION['color']; echo "Our size value is ".$_SESSION['size']; echo "Our shape    
value is ".$_SESSION['shape']; ?>

All of the values are stored in the $_SESSION array, which we access here. Another way to show this is to simply run this code:

<?php session_start(); Print_r ($_SESSION); ?> 
Rupeshit
+3  A: 

The $ID variable has not been declared inside the studNameDetails1 function, so when you try to access it, an undefined value. This will set the $_SESSION['ATTsub'] and $sub_id variables to undefined, and so they will show as being empty when you try to print them.

If these variables have been declared outside the scope of the function, use the global keyword:

function studNameDetails1()
{
    global $sub_id, $ID;

    $_SESSION['ATTsub']=$sub_id = $ID[5];
}

http://php.net/manual/en/language.variables.scope.php

tomit
+1 For not assuming that there's more code in the function, as I did.
George Marian