raytri Posted March 4, 2009 Share Posted March 4, 2009 I'm trying to get sessions to work, and having no luck. Here's the code for the test pages I built: Page 1 (test.php): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?php session_start(); $_SESSION['username']= "testusername"; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php echo '<p>This is a test of the Sessions function. The username is '.$_SESSION['username'].'.</p><a href="test2.php">Go to page 2</a>'; ?> </body> </html> Page 2 (test2.php) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?php session_start(); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php echo '<p>This is a test of the Sessions function. The username is '.$_SESSION['username'].'.</p><a href="test.php">Go to page 1</a>'; ?> </body> </html> The output for the first page is what I would expect: This is a test of the Sessions function. The username is testusername. But the output for the second page loses the variable: This is a test of the Sessions function. The username is . When I echo the SID for each page, it shows that it's generating a new session for each page. Suggestions? In case it matters, this is running on an Apache server, I'm testing in Firefox and Safari, and the site is not in the top-level folder in the domain; it's in the "test" folder in my domain (where I build things prior to transferring them to the client's domain). Link to comment https://forums.phpfreaks.com/topic/147965-solved-sessions-not-working-at-all/ Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 extract from manual. Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser. try putting session_start() at the start of page, move your DOCTYPE line after PHP block Link to comment https://forums.phpfreaks.com/topic/147965-solved-sessions-not-working-at-all/#findComment-776595 Share on other sites More sharing options...
raytri Posted March 4, 2009 Author Share Posted March 4, 2009 Doh. It's always something ridiculously simple. Thanks! Link to comment https://forums.phpfreaks.com/topic/147965-solved-sessions-not-working-at-all/#findComment-776601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.