Problem passing session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aladdinsane
Forum Commoner
Posts: 38
Joined: Fri Sep 05, 2003 2:15 pm

Post by aladdinsane »

Hi,

I have a weird situation using sessions with one of the web hosts I use. Well I think it's weird maybe you can clarify?

php is set to use cookies for sessions and the trans_id is set to false.

Whenever I try to use sessions on this server if I simply use session_start() the variables are not available. I always have to pass the session id in a url of form otherwise it just won't work.

I have set my browser to 'allow all cookies' and tried this IE6, NS7.1 Mozilla 1.5 and always have this problem.

Here's a code example:

Page 1

Code: Select all

<?php
session_start();
$_SESSION&#1111;'variable1']="Hello my name is";
$_SESSION&#1111;'variable2']="Keith";
?>
html>
<head>
<title>Session testing</title>
</head>
<body>
<p><a href="session2.php">Click here to go to the next page where you should be able to access the session variables</a></p>
<p>The two session variables are</p>
<p>1)<?php echo $_SESSION&#1111;'variable1']."<br>2)".$_SESSION&#1111;'variable2'];?></p>
</body>
</html>
Page 2

Code: Select all

<?php
session_start();
?>
<html>
<head>
<title>Session testing</title>
</head>
<body>
<?php echo $_SESSION&#1111;'variable1']." ".$_SESSION&#1111;'variable2'];?>
</body>
</html>
On page 1 the session variable are displayed. On page2 they are empty.

On my testing server on my PC this code works. On another hosts server I use it also works.

I think this behaviour seems wrong as I didn't think I need to pass the sessionid when php.ini was set to use cookies.

Any ideas?

Keith
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Which version of PHP are you using?

Mac
aladdinsane
Forum Commoner
Posts: 38
Joined: Fri Sep 05, 2003 2:15 pm

Post by aladdinsane »

Hi,

I am using PHP Version 4.3.3

Keith
aladdinsane
Forum Commoner
Posts: 38
Joined: Fri Sep 05, 2003 2:15 pm

Post by aladdinsane »

Hi,

I'm still puzzled by this. Any ideas anybody?

Regards

Keith
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

I don't have the answer but I'd like to let you know that your code works perfectly well on my server. The only suggestion I have is that you may have some white space before your first line on the second page. Do you get an error message at all?
aladdinsane
Forum Commoner
Posts: 38
Joined: Fri Sep 05, 2003 2:15 pm

Post by aladdinsane »

Hi,

Thanks for your reply Paddy.

There's definately no white space and no error message. This is a cut down test I made when a larger project was exhibiting these exact same problems.

It's an odd one this. As I mentioned I did get the larger project working by passing the Session ID but it was a pain having to redo the code to get it to work!

Really I'm just wondering if this behaviour is normal for the settings they have set in php.ini for sessions, or if maybe they've got a problem on the server?

Cheers

Keith
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

If it works on a different server, as Paddy said it did, then my guess is that your server settings are the problem. If you can please post the contents of the session part of you PHP.ini.

BTW, here is a post I found on another forum:
The top two things that cause problems with PHP sessions are:

1) the session id is not getting passed.
2) your session save handler isn't working.

What are the values in the [Session] section of your php.ini file? Are
you using cookies to pass the session id (session.use_cookies = 1)? If
so, are cookies turned on in your test browser? Can you check your
browser and see if the session cookie is being set (should have a name
like PHPSESSID, or the value of session.name in php.ini)?

If not using cookies to pass the session id, then did you compile PHP
with the enable-trans-sid feature and is session.use_trans_sid = 1?
(trans_sid enables PHP to automatically rewrite all local links in your
pages to include the session id. Note that passing the session id via
the URL is a potential security risk, cookies are preferable).

If neither use_cookies nor use_trans_sid are on, then you need to pass
the session id manually from page to page in the GET string of every
link (including the header redirect). In fact, even if you are using
the trans_sid feature PHP probably won't catch that header redirect,
which might be your problem - try adding the session id to the link
manually (ie header("Location: products.php?" . SID);).

(the SID constant is always set to the value "name=id" where name is
your PHP session name (session.name, PHPSESSID by default) and id is the
session id (a random 32-character string).

Also, your call to session_register is unnecessary - adding an entry to
the $_SESSION array is all you need to do to register the session
value. In fact, the PHP manual specifically warns against trying to use
both session_register() and the $_SESSION array, so conceivably taking
out the call to session_register() could fix your problem.
Post Reply