Solved

Session variables problem in functions

Posted on 2003-12-05
4
423 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP populating an array. 4 23
write screen output to text file 21 47
PHP AJAX Wordpress 9 19
Php recording post 4 37
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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