dprichard Posted June 13, 2007 Share Posted June 13, 2007 Okay, I am taking the advice of a moderator and going to stop using Dreamweaver as a crutch for writing PHP apps. I have some questions about sessions and have been reading through some online tutorials, but have some questions that may seem simple stupid about sessions, but would appreciate some input. Do you have to put this at the top of each page on the site when using sessions to keep the sessions going from page to page or do you just put it on a page when you are starting a session or pulling info back from a session? <?php session_start(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/55405-session_start-and-using-sessions-in-php/ Share on other sites More sharing options...
GingerRobot Posted June 13, 2007 Share Posted June 13, 2007 Yes, you must put session_start() at the top of everypage that makes use of the $_SESSION superglobal array. So therefore, if you are checking if someone is logged in, for instance, then every page will need session_start. If the page does nothing at all with sessions, you dont need it. Quote Link to comment https://forums.phpfreaks.com/topic/55405-session_start-and-using-sessions-in-php/#findComment-273805 Share on other sites More sharing options...
TreeNode Posted June 13, 2007 Share Posted June 13, 2007 It's important that you know that the real rule is that no output can come before the session_start(), so, the very first thing must be the opened PHP tags, not even a space or newline can come before the <?php Quote Link to comment https://forums.phpfreaks.com/topic/55405-session_start-and-using-sessions-in-php/#findComment-273807 Share on other sites More sharing options...
flappy_warbucks Posted June 13, 2007 Share Posted June 13, 2007 What i normally do is create a file (say session.php for arguments sake) and include that file in every file you need it... for example... You would call the file when you have to pass session vairibles, but you wouldent call the file if you have a file that didnt need the sessions varibles... normally what i do is this: // some page we will call it members.php require "session.php"; if ($_SESSION['user'] != $_COOKIE['user']) // some basic validation... needs tweekeing obviously. { die("naughty person"); } else { //typicly function call } and then in the sessions fiel you would have somethign like this: session_start(); if (!isset($_SESSION['user'])) { header("Location: index.php") // if the session dont exist we send them to the home page. die(); } define("user",$_SESSION['user']); // and then you continue to define constants untill all your session varibles are assigned... etc Quote Link to comment https://forums.phpfreaks.com/topic/55405-session_start-and-using-sessions-in-php/#findComment-273813 Share on other sites More sharing options...
dprichard Posted June 13, 2007 Author Share Posted June 13, 2007 Kewl info. So if I use session_start it has to be the first thing on the page on the first thing in an include and the first include on the page? Also, if the user is going to be going to pages that I don't use the session on in between pages that I do use the session on do I need to still put the session_start at the top of the other pages to keep the session going? Quote Link to comment https://forums.phpfreaks.com/topic/55405-session_start-and-using-sessions-in-php/#findComment-273820 Share on other sites More sharing options...
TreeNode Posted June 15, 2007 Share Posted June 15, 2007 That depends on your server setup (php.ini) with session variables... most likely they will stay but its always best to include it (just in case!) Quote Link to comment https://forums.phpfreaks.com/topic/55405-session_start-and-using-sessions-in-php/#findComment-275510 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.