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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple session variable output 1

Status
Not open for further replies.

Streetdaddy

Programmer
Jun 10, 1999
161
AU
i'm *very* new to php, so please tell me if the following should work:

inc.php
<?php
function go() {
session_start();
session_register(&quot;test&quot;);
$test = &quot;hello world&quot;;
echo &quot;output from inc.php - $test<br>&quot;;
}
?>

file.php
<?php
include 'inc.php';
go();
echo &quot;output from file.php - $test<br>&quot;;
?>

file.php gives the output:

output from inc.php - hello world
output from file.php -

Is there some simple reason why file.php can't output $test? I mean, is it something simple in the code or is it a config issue? Any help appreciated...
Miles

Those Micros~1 guys sure know what they doing!
 
It may be because your session variable is being set in a subroutine. The main app can't get to the variable because it is in a lower scope.

Take a look at:

and see if it doesn't clarify things. Perfection in engineering does not happen when there is nothing more to add. Rather it happens when there is nothing more to take away.
 
...simultaneous posting here!

Also, with session variables, I recommend using the $_SESSION &quot;autoglobal&quot; array. In your case, you'd set it like this:
inc.php
Code:
<?php
    function go() {
        session_start();
        $_SESSION[&quot;test&quot;] = &quot;hello world&quot;;
        echo &quot;output from inc.php - &quot;.$_SESSION[&quot;test&quot;].&quot;<br>&quot;;
    }
?>
file.php
Code:
<?php
    include 'inc.php';
    go();
    echo &quot;output from file.php - &quot;.$_SESSION[&quot;test&quot;].&quot;<br>&quot;;
?>

When you call
Code:
session_start()
it will re-load the
Code:
[&quot;test&quot;]
key and the value it is set to.

This method also has much better security.
 
Thanks for the tip sonnysavage, you deserve a star.

I'm new to PHP and have been having trouble with some session code (session_register etc.) that works on my WIN2K box but not on my linux box. Its had me scratching my head for a bit but using the 'autoglobal' $_SESSION has made things work perfectly (as perfect as can be anyway :)

Thanks muchly. [smurf]
01101000011000010110010001110011
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top