Re: Cannot Get Session Variables nor Global variables to work

From: Date: Sat, 22 May 2004 00:23:35 +0000
Subject: Re: Cannot Get Session Variables nor Global variables to work
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-186737@lists.php.net to get a copy of this message
You need to place session_start() at the top of test2.php. If you use session_start, you should not use using session_register(), just use $_SESSION. From Manual: "If you are using $_SESSION and disable register_globals, do not use session_register(), session_is_registered() and session_unregister()" Globals are limited to the scope of current script and do not carry the values betwen pages. You cannot pass values betwen scripts using globals. Use $_SESSION or cookies if you do not want to use get or post variables. Take a look at: http://www.php.net/manual/en/function.session-start.php http://www.php.net/manual/en/ref.session.php http://www.php.net/manual/en/language.variables.scope.php Regards, Jordi. Joe Carr wrote:
Help. I am missing a MAJOR aspect of using either Session variables or global variables. I am trying to set one variable on one page, and read it on the other _without_ sending any information in the query string. Here is the code for the two pages : test1.php : <?php session_start(); // Different methods of setting a SESSION variable : if (!isset($_SESSION["count"])) {
    $_SESSION["count"] = 0;
} else {
    $_SESSION["count"]++;
} echo $_SESSION["count"]; session_register('var2'); $var2="testing"; // Trying to use a global variable global $help; $help = "howdy"; $GLOBALS['help2']= "howdy again"; print_r($GLOBALS); ?> <a href="test2.php">step 2</a> and test2.php looks like this: <?php //$GLOBALS['help']= $_REQUEST['id']; print_r($_SESSION); print_r($GLOBALS); echo "COUNT=". $_SESSION["count"]; echo "<HR>"; echo "HELP=". $GLOBALS["help"]; echo "HELP2=". $GLOBALS["help2"]; ?> On test2.php, each of the echo statements indicate that the variables have not been defined. I've read many posts which indicate that I should pass the PHPSESSID through the query string, but when I look at the PHPSESSID in the global array printed it is the same on both pages. I cannot seem to have access to either session nor global variables outside the context of one page. Any assistance would be fascinatingly appreciated. Thanks...


« previous php.general (#186737) next »