Solved

Session variables problem in functions

Posted on 2003-12-05
4
426 Views
Last Modified: 2008-03-06
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
Comment
Question by:aayushpuri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:rjdown
ID: 9884473
Use the super global variable $_SESSION["message"] instead
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9884510


$message =& $_SESSION['message'];
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9885050
$message = $_SESSION['message'];
0
 
LVL 6

Accepted Solution

by:
aolXFT earned 100 total points
ID: 9885170
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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question