Skip to main content
Inspiring
December 10, 2007
Question

Session variables - what's up?

  • December 10, 2007
  • 29 replies
  • 1685 views
I have a page with this markup -

<?php

session_start();

$_SESSION['usertype'] = 2;

header("Location: s_support-repair-parts.php");
exit();

?>

The page redirects to a new page containing this markup -

<?php session_start(); ?>
...
<?php
echo "User is type: " . $_SESSION['usertype'];
...
?>

I never see a value echoed - what's up with that?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


This topic has been closed for replies.

29 replies

Inspiring
December 11, 2007
.oO(Murray *ACE*)

BTW: With the "Full URL please." from my first reply I just meant the
Location header - the HTTP spec requires a full URL there. But that's
just a side note.

>I'm not sure where to look for those, but a quick perusal doesn't find
>them....
>
> http://76.12.89.239/info.php

Down in the 'session' section:

session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid 0 0

This means that PHP will try to set a cookie. If the browser accepts it,
the session should work. But if you reject the cookie, the session won't
work. TransSID is disabled, so PHP will not automatically append the SID
to your links.

>Curiously, I have these two pages -
>
> http://76.12.89.239/test2-session1.php
> http://76.12.89.239/test2-session2.php
>
>(the code on the second page follows)
>[snipped]
>
>And those two pages work!

Hmm, not exactly. If I enter something, it's submitted to the second
page and shown there. The second page also wants to set the session
cookie, which I accept. But - if I now reload the second page without
submitting the POST data again, the session seems to be lost again,
while instead it should show my name again.

David gave a good hint. See if you get an error message somewhere.

Micha
Inspiring
December 11, 2007
Murray *ACE* wrote:
> And those two pages work!

Sessions obviously work. However, your server has display_errors turned
off. It's possible that there's a control character or whitespace before
the call to session_start(). Put this at the very top of both pages:

<?php ini_set('display_errors', '1'); ?>

See if you get any error messages.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
December 11, 2007
Micha:

I'm not sure where to look for those, but a quick perusal doesn't find
them....

http://76.12.89.239/info.php

Curiously, I have these two pages -

http://76.12.89.239/test2-session1.php
http://76.12.89.239/test2-session2.php

(the code on the second page follows)

<?php
// initiate session
session_start();
// check that form has been submitted and that name is not empty
if ($_POST && !empty($_POST['name'])) {
// set session variable
$_SESSION['name'] = $_POST['name'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Session test 2</title>
</head>

<body>
<?php
// check session variable is set
if (isset($_SESSION['name'])) {
//if set, greet by name
echo 'Hi, '.$_SESSION['name'].'. <a href="session3.php">Next</a>';
}
else {
// if not set, send back to login
echo 'Who are you? <a href="session1.php">Login</a>';
}
?>
</body>
</html>

And those two pages work!


--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:p2ctl3hc5i2o9rh88v83tbte2m6avb3hfv@4ax.com...
> .oO(Murray *ACE*)
>
>>Even when I change that initial page to a simple link rather than a
>>redirection, it still fails.
>
> What's the setting of 'session.use_trans_sid', 'session.use_cookies' and
> 'session.use_only_cookies' (check phpinfo())?
>
> With transSID enabled, PHP would automagically append the session ID to
> your link, but it seems as if it's disabled.
>
> Micha

Inspiring
December 11, 2007
.oO(Murray *ACE*)

>Even when I change that initial page to a simple link rather than a
>redirection, it still fails.

What's the setting of 'session.use_trans_sid', 'session.use_cookies' and
'session.use_only_cookies' (check phpinfo())?

With transSID enabled, PHP would automagically append the session ID to
your link, but it seems as if it's disabled.

Micha
Inspiring
December 11, 2007
Even when I change that initial page to a simple link rather than a
redirection, it still fails.

Full link is here -

http://76.12.89.239/test-session2.php

Markup =

<?php

session_start();

$_SESSION['usertype'] = 2;

//header("Location: test-session2b.php");

?>
<html>
<head>
</head>
<body>
<p><a href="test-session2b.php">Click</a></p>
</body>
</html>

LINKED PAGE MARKUP -

<?php

session_start();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<meta http-equiv="imagetoolbar" content="no" />
<meta name="MSSmartTagsPreventParsing" content="TRUE" />
<meta name="robots" content="index,follow" />
</head>

<body>
<p>The session variable has a value of - <?php echo $_SESSION['usertype'];
?></p>
</body>
</html>

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:55btl3hf2sta6saaifav84r4qse64qktth@4ax.com...
> .oO(Murray *ACE*)
>
>>I have a page with this markup -
>>
>><?php
>>
>>session_start();
>>
>>$_SESSION['usertype'] = 2;
>>
>>header("Location: s_support-repair-parts.php");
>
> Full URL please.
>
>>exit();
>>
>>?>
>>
>>The page redirects to a new page containing this markup -
>>
>><?php session_start(); ?>
>>...
>><?php
>>echo "User is type: " . $_SESSION['usertype'];
>>...
>>?>
>>
>>I never see a value echoed - what's up with that?
>
> Do you use session cookies? If not, the SID will be lost on the
> redirect. You would have to append the magic constant "SID" by hand to
> the redirection URL.
>
> Micha

Inspiring
December 11, 2007
.oO(Murray *ACE*)

>Excellent - that's how to do it! Thanks for the hints!

You're welcome.

Micha
Inspiring
December 11, 2007
.oO(Murray *ACE*)

>I have a page with this markup -
>
><?php
>
>session_start();
>
>$_SESSION['usertype'] = 2;
>
>header("Location: s_support-repair-parts.php");

Full URL please.

>exit();
>
>?>
>
>The page redirects to a new page containing this markup -
>
><?php session_start(); ?>
>...
><?php
>echo "User is type: " . $_SESSION['usertype'];
>...
>?>
>
>I never see a value echoed - what's up with that?

Do you use session cookies? If not, the SID will be lost on the
redirect. You would have to append the magic constant "SID" by hand to
the redirection URL.

Micha
Inspiring
December 11, 2007
Micha:

Do you have any ideas why that code would be failing?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:h8btl39nua9ri8ibjud1sh3f2k0p0pp2dq@4ax.com...
> .oO(Murray *ACE*)
>
>>Can you get to that site, Massimo? It just times out for me....
>
> There's also an online tool to check request and response headers:
>
> http://web-sniffer.net
>
> Micha

Inspiring
December 11, 2007
.oO(Massimo Foti)

>If I remember well, the session identifier cookie in PHP should be named:
>"PHPSESSID". The value for it must be the same accross different requests in
>order for the session to persist

The name can be changed to whatever you like with session_name().

Micha
Inspiring
December 11, 2007
.oO(Murray *ACE*)

>Can you get to that site, Massimo? It just times out for me....

There's also an online tool to check request and response headers:

http://web-sniffer.net

Micha
Inspiring
December 11, 2007
Can you get to that site, Massimo? It just times out for me....

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Massimo Foti" <massimo@massimocorner.com> wrote in message
news:fjkem0$40b$1@forums.macromedia.com...
> "Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
> news:fjkecd$3on$1@forums.macromedia.com...
>> Which FF tool will show me request headers?
>
> I use this:
> http://livehttpheaders.mozdev.org/
>
>
>
>> I am seeing a cookie for the session called PHPSESSID, and its value is
>> the same on all the pages involved, but I am not seeing any other
>> cookie....
>
> If the value for PHPSESSID is the same the session should persist.
>
> Is any session working on your test server?
> Have you tried the same code on another server?
>
> Massimo
>