Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem accessing session variables within functions 3

Status
Not open for further replies.

lasombra

Programmer
Mar 22, 2002
26
CH
How the heck can I access session variables within a function? I regsiter a variable "var1" with $_SESSION[var1].

This variable I can access within another php-file which starts like
Code:
<?
 session_start();
 echo($var1);
 ...
?>

But when I call a function, the variable is not accessible:
Code:
<?
 test();

 function test() {
    echo($var1);
 }
?>

Why?
 
i forgot to write a line in the script below
Code:
<?
 test();

 function test() {
    session_start();
    echo($var1);
 }
?>
 
Since your code implies that register_globals is on, you'd need to define $var1 as global with
Code:
function test()
{
    global $var1;
    echo $var1;
}
//Daniel
 
So after working much with PHP, the solution with $_SESSION['variable'] is even better and works without poblems (PHP4+)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top