Page 1 of 1

Problem with passing session variables

Posted: Mon May 17, 2004 5:37 pm
by competent
Hi!
I`m new to PHP and I have problem with passing session variables created with $_SESSION['variable']; on one page to another.
Here is the sample code of the first page, named page1.php:
<?php
session_start();
?>
<html>
<head><title>Page1</title></head>
<body>
<?php
$_SESSION['var1'];
header("Location: page.php?" . session_name() . "=" . session_id());
?>
</body>
</html>

and code of page 2, named page2.php:

<?php
session_start();
?>
<html>
<head><title>Page2</title></head>
<body>
<?php
echo $_SESSION['var1'];
?>
</body>
</html>
Problem is that the variable passed from page1.php in $_SESSION[] is not shown in page2.php. Session ID is passed correctly so I think it`s not a problem here for sure.

I would be very grateful for any suggestions, because it`s quite important for me to solve this problem asap (mayby I have sth wrong in php.ini - but it would be strange (I use php.ini-recomended from default windows php package).

I have PHP v4.3.4, glogals turned OFF and don`t have problem with passing header info(btw it`s interesting that even if I already sent sth info to web browser php doesn`t throw error or even warning that it cannot include header info because header information was already sent.

Posted: Mon May 17, 2004 5:44 pm
by markl999
$_SESSION['var1'];
You never actually give it a value?
Eg. $_SESSION['var1'] = 'somevalue';

Posted: Mon May 17, 2004 5:52 pm
by competent
Sorry I wrote it fast and I forgot to write it in this example, but in my project I have
$_SESSION['var'] = "value";
:)
So it`s not a problem. But thank you any way

Posted: Mon May 17, 2004 5:57 pm
by dull1554
make a php file and stick in it

Code: Select all

<?php
php_info();
?>
then look at it and see what it says about your server's session setup, some servers may have sessions set up differently depending on how they were configured when the server was set up.....

---good luck---

I solved the problem

Posted: Mon May 17, 2004 6:24 pm
by competent
Thank you VERY VERRRRRY MUCH :)

Posted: Mon May 17, 2004 6:39 pm
by feyd
and once you start getting the lovely:
headers already sent errors/warnings

move the header() call and it's requirements to just after the session_start() call.

Code: Select all

<?php 
session_start(); 
?> 
<html> 
<head><title>Page1</title></head> 
<body> 
<?php 
$_SESSION['var1']; 
header("Location: page.php?" . session_name() . "=" . session_id()); 
?>