You are not logged in.

#1 2010-04-14 00:27:06

kourosh
Member
From: England
Registered: 2009-03-10
Posts: 241
Website

[SOLVED] PHP sessions problem

Since I don't often visit any other forum, someone here might be able to help smile

I have a *very* basic web 'application' written which uses PHP sessions. For some reason, the session variable only exists in one page. Change to another and it disappears. Surely session variables are suppose to avoid this problem?

Maybe I've got something entirely wrong. Any idea?

Last edited by kourosh (2010-04-14 03:50:55)

Offline

#2 2010-04-14 01:30:09

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: [SOLVED] PHP sessions problem

Can you post some sample code?


Matt

"It is very difficult to educate the educated."

Offline

#3 2010-04-14 01:42:45

kourosh
Member
From: England
Registered: 2009-03-10
Posts: 241
Website

Re: [SOLVED] PHP sessions problem

Well I've done a basic test:
test.php

<?php
session_save_path("/home/a7049782/public_html/films/tmp");
session_start();
$session_path = session_save_path();
$_SESSION['count']=1;
?>
<html>
<body>
<?php echo "Count=" . $_SESSION['count']; ?>
</body>
</html>

This sucessfully outputs 1

Then another page:
test2.php

<?php
if(isset($_SESSION['count']))
echo "Count=" . $_SESSION['count'];
else
echo "No variable.";
?>

Which outputs 'No variable.'

(I've also tried this without defining a path, still the same result)

Offline

#4 2010-04-14 02:09:13

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: [SOLVED] PHP sessions problem

You need:

session_start();

In the test2.php page before you use any session variables. For example:

<?php
session_start();
if(isset($_SESSION['count']))
echo "Count=" . $_SESSION['count'];
else
echo "No variable.";
?>

Matt

"It is very difficult to educate the educated."

Offline

#5 2010-04-14 02:31:02

kourosh
Member
From: England
Registered: 2009-03-10
Posts: 241
Website

Re: [SOLVED] PHP sessions problem

Right, I tried that, doesnt work :S

I thought I would only need to start the session once, and by setting a variable in test.php, that I would be able to access it in test2.php too?

Or have I got this entirely wrong?

Offline

#6 2010-04-14 03:32:30

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: [SOLVED] PHP sessions problem

You need the two session_start() statements. Perhaps the problem is with the cache of the browser. Also you can check a similar example in the Official Php page (Example #1 and #2).

Offline

#7 2010-04-14 03:39:10

kourosh
Member
From: England
Registered: 2009-03-10
Posts: 241
Website

Re: [SOLVED] PHP sessions problem

Thank you!
I removed the save paths and w/e to use defaults again and it worked.
Thanks again, this has been annoying me for weeks smile

Offline

Board footer

Powered by FluxBB