• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Session variables problem in functions

I am having problems with initializing session variables inside functions
While writing PHP scripts I need to set some session variables inside some functions
Example:
             function some_function()
             {
                    ...........
                    session_register("message");
                    $message="You are NOT authorized to view this page";
                   ........
             }

The same code for setting session variables in working fine when I use in the main part of the PHP i.e. not inside any function.

Can anyone figure out my mistake in setting session variables??

Suggestions and solutions are welcome

-Aayush
0
aayushpuri
Asked:
aayushpuri
  • 2
1 Solution
 
rjdownCommented:
Use the super global variable $_SESSION["message"] instead
0
 
aolXFTCommented:


$message =& $_SESSION['message'];
0
 
shivsaCommented:
$message = $_SESSION['message'];
0
 
aolXFTCommented:
No Shivsa, put the & back in.

Otherwise the value will be lost, after he exits the function.
Alternatively

$message = &$_SESSION['message'];

could be used, the difference is that with my first one, you are using the copy by reference operator, where as in my second example you are copying a reference.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now