tags:

views:

70

answers:

1

Hi All,

I am storing a session and a global variable in file1.php. However, when I try to access those from file2.php I get nothing. I am using php 5.1.6.

$_SESSION['abc'] = $a;
$GLOBALS['def'] = $b;

Any idea?

Thanks in advance.

+2  A: 
  • Do you have cookies enabled on your browser?
  • Are you remembering to call session_start at the top of BOTH pages?
  • Are you storing your session variables in $_SESSION ? Nothing else will store.

In regards to your edit: The variable stored in $GLOBALS is just a global to that script. You have to put the value in $_SESSION to use across pages.

Example:

// Page 1
session_start();
$_SESSION['abc'] = "hello world";
$GLOBALS['def']  = "More stuff.";

// Page 2
session_start();
echo $_SESSION['abc'];   // prints 'hello world'
echo $GLOBALS['def'];   // is not defined. Globals aren't session variables.
Erik
Yes, I have cookies enabled on my hostI am calling session_start on both files right after <?php tagsI am using $_SESSION['abc'] = $a syntax to store session variablesWhen developing my app on my windows machine it works fine with php 5.3.0 but it fails on Linux running PHP 5.1.6
ebtesting
Going by your example above, are you sure that there's a value in $a? Try giving your session variable a string value like `$_SESSION['abc'] = 'hello'` to see if its your session failing, or something else.
Erik
Could it be any Apache/PHP config problem?
ebtesting
If it works locally for you, but not on your webhost, it may indeed be the way they have PHP set up, as sessions can be disabled. There's some information here: http://support.qualityunit.com/knowledgebase/general-questions/how-to-enable-session-support-for-php.html about enabling sessions via php.ini if you have the ability to use local .ini files.
Erik