Click to See Complete Forum and Search --> : Simple session var


GreyArea
11-07-2003, 04:05 PM
I'm trying to learn php for a site that I'm doing. All I want to do is create a session variable. I've searched the forums and I can create a session var and print it all on one page. However, as soon as I send them to another page the variable no longer prints. I'm passing the session ID in the link so I have no idea why this won't work.

Page A.php prints fine

<?PHP

session_start();
$_SESSION['name'] = 'John';
printf("MY NAME IS %s",$_SESSION['name']);

<a href="B.php?PHPSESSID=<?php echo $PHPSESSID ?>">Home</a>
?>

prints MY NAME IS John w/ a link to page B.php

Page B.php called if link above is pressed passing session id

Hello <?php printf("MY NAME IS %s",$_SESSION['name']);?>

just prints MY NAME IS

Am I missing something here ?????

TIA

pyro
11-07-2003, 04:41 PM
Yes, you forgot to start the session on the following page. Sessions must be started before the can be used. Just add this to the script before you try to read the sessions variable:

session_start();

GreyArea
11-07-2003, 04:46 PM
thanks for the respons pyro, however I've tried that and when I do I get the following error:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /public_html/temp/resellers/b.php:6) in /public_html/temp/resellers/b.php on line 127

thanks again

pyro
11-07-2003, 04:49 PM
You cannot set headers (which includes sessions and cookies) after any output has been sent to the browser.

GreyArea
11-07-2003, 05:00 PM
OK...
sorry if this is a stupid question, but I've always used CF and now have to use php for a project I just started this week so I know little to nothing about php. Therefore, how do I get a session var to print in a table cell ? Since the table tag has to be output to the browser before a cell can be created.

thanks again for all the help

pyro
11-07-2003, 05:03 PM
Try:

<?PHP
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table>
<tr>
<td><?PHP echo $_SESSION['var']; ?></td>
</tr>
</table>
</body>
</html>

GreyArea
11-07-2003, 05:10 PM
wow...so I had a carriage return before my session_start() php block that was causing my problem.

Thanks for the help !!

pyro
11-09-2003, 06:03 PM
A bit late (was gone for the weekend) but you are very welcome... :)