[solved] Session Variables not working

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

[solved] Session Variables not working

Post by spacebiscuit »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys,

I am not sure if this is the right place to post this but.........

I have an installation of php running with my Apache server but it does not seem to pass session variables correctly. 

I have tested my scripts on another server fine with the session variables being passed and accessed as expected.

However on my server they do not, work I try to access them they appear to be empty.

I have tried turning register_globals in php.ini both on and off but it does not solve the problem.

This is the code I am using to test:

First Script:

Code: Select all

<?

session_start();

$_SESSION['test']="ROB";

echo"<a href=test2.php>TRESTING</a>";

?>
Second Script:

Code: Select all

<?

session_start();

echo"$_SESSION['test']";

?>
Any help would be appreciated.

Thanks in advance,

Rob.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

In second script

Code: Select all

echo"$_SESSION['test']";
should be

Code: Select all

echo $_SESSION['test'];
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

...makes no difference, changed the code as advised but same output - nothing!

Any other ideas?

Rob.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

It does make a difference, you can't directly access associative arrays when enclosed in quotations.

echo "$_SESSION['test']"; - Invalid
echo $_SESSION['test']; - Valid
echo "Session is {$_SESSION['test']}"; - Valid

try a var_export($_SESSION); in your second script.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Then why does it work fine on my other server, cut and paste the code and try it for yourself.

I will try your suggestion too.

Thx,

Rob.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

It doesn't run here, but it's probably your php INI settings. However, that doesn't solve the session problem anyway. What are your httpd.conf settings for sessions?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Check your php session settings by running a phpinfo() page and by looking at your php.ini. Also, try to echo out a session var immediately after setting it to see if it is getting set properly. Lastly, set your error reporting to E_ALL so if there are syntax errors or other errors that are being muted right now, you can see them. That may give you some more information as well.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Thanks for the feedback here are my results:

- Error Reporting:

In myphp.ini file it is set as follows:

Code: Select all

error_reporting  =  E_ALL
Output of session variable after setting:

I tried echoing the session variable after it has been set and before submitting the page and it output the correct value. So it seems it is losing the or not passing the variable once I click the link to the subsequent page/link.

PHPinfo output:

Here is my phpinfo output:

Code: Select all

session
Session Support  enabled  
Registered save handlers  files user  

Directive Local Value Master Value 
session.auto_start Off Off 
session.bug_compat_42 Off Off 
session.bug_compat_warn On On 
session.cache_expire 180 180 
session.cache_limiter nocache nocache 
session.cookie_domain no value no value 
session.cookie_lifetime 0 0 
session.cookie_path / / 
session.cookie_secure Off Off 
session.entropy_file no value no value 
session.entropy_length 0 0 
session.gc_divisor 1000 1000 
session.gc_maxlifetime 1440 1440 
session.gc_probability 1 1 
session.name PHPSESSID PHPSESSID 
session.referer_check no value no value 
session.save_handler files files 
session.save_path /tmp /tmp 
session.serialize_handler php php 
session.use_cookies On On 
session.use_only_cookies Off Off 
session.use_trans_sid Off Off
http.conf:

I checked my http.conf file and searched for the word "session" but it returned no results!

Any other ideas?

Rob.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

your "httpd.conf" should tell you a directory where sessions are stored at least.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Can you give me an example of the syntax, there defintely is no mention of the word "session" in my http.conf file.

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's not set in httpd.conf. session.save_path in your php.ini sets the default location for saving session data.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

OK, before you do anything else clear your broswer cache. Then check your scripts to make sure that session_start() is being called at the very beginning or close to the beginning of the script. Also, make sure no browser output has been sent by the script before session_start() is called. Then run your two scripts...

PAGE 1:

Code: Select all

<?php
session_start();
$_SESSION['test']="ROB";
echo $_SESSION['test'] . ' is the value set for "test".<br />';
echo 'Our current session id is ' . $_SESSION['PHPSESSID'] . '<br />';
echo '<a href="test2.php">Try the test</a>';
?>

PAGE 2:

Code: Select all

<?php
session_start();
echo $_SESSION['test'] . ' is the value set for "test".<br />';
echo 'Our current session id is ' . $_SESSION['PHPSESSID'] . '<br />';
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

session_id(), not $_SESSION['PHPSESSID'] ;)

Run the following in a new file and tell us the results if the script doesn't appear to be working right.

Code: Select all

<?php

$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
$rg = (in_array(strtolower(ini_get('register_globals')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$de = (in_array(strtolower(ini_get('display_errors')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$so = (in_array(strtolower(ini_get('short_open_tag')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$eol = (isset($_SERVER['HTTP_HOST']) ? "<br />\n" : "\n");

$ec = array(
   'E_STRICT' => 2048,
   'E_ALL' => 2047,
   'E_USER_NOTICE' => 1024,
   'E_USER_WARNING' => 512,
   'E_USER_ERROR' => 256,
   'E_COMPILE_WARNING' => 128,
   'E_COMPILE_ERROR' => 64,
   'E_CORE_WARNING' => 32,
   'E_CORE_ERROR' => 16,
   'E_NOTICE' => 8,
   'E_PARSE' => 4,
   'E_WARNING' => 2,
   'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
   if (($t & $v) == $v)
   {
      $e[] = $n;
      $t ^= $v;
   }
}
$er = $er . ' (' . implode(' | ', $e) . ')';

echo 'PHP Version: ' . $ve . $eol;
echo 'PHP OS: ' . $os . $eol;
echo 'Error Reporting: ' . $er . $eol;
echo 'Register Globals: ' . $rg . $eol;
echo 'Short Tags: ' . $so . $eol;
echo 'Display Errors: ' . $de . $eol;

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

feyd wrote:session_id(), not $_SESSION['PHPSESSID'] ;)
Sorry, got a little confused. I think you can reference the Session ID with the constant SID after starting the session. PHPSESSID is the name of the var added to forms the first time a session is called. It is available (on the first form load only) as a $_POST var with the index of PHPSESSID.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You'll get better results with http://www.php.net/session_name than hoping it's PHPSESSID ;)
Post Reply