Problem with 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
competent
Forum Newbie
Posts: 8
Joined: Mon May 17, 2004 5:37 pm

Problem with passing session variables

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$_SESSION['var1'];
You never actually give it a value?
Eg. $_SESSION['var1'] = 'somevalue';
competent
Forum Newbie
Posts: 8
Joined: Mon May 17, 2004 5:37 pm

Post 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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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---
competent
Forum Newbie
Posts: 8
Joined: Mon May 17, 2004 5:37 pm

I solved the problem

Post by competent »

Thank you VERY VERRRRRY MUCH :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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()); 
?>
Post Reply