megz90 Posted April 24, 2008 Share Posted April 24, 2008 hi all im having a little problem with sessions .... i have at the top of all my pages.... <?php if (!isset($_SESSION["sess_loggedon"])) { session_start(); } else header ('location: index.html'); //Database Information include('../db_connection.php'); // this then goes on to show the rest of the page html and other php ?> what i want to do is on each of the pages check if a session is active or not, if not then show the index page what is happening is this... if the user loads a page without logging in then the page still displays all the normal links and stuff shows it just doesnt show any personal information... any suggestions on what ive got wrong ? Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/ Share on other sites More sharing options...
947740 Posted April 24, 2008 Share Posted April 24, 2008 You have to start your session before checking if there is a session variable. It will always start the session, no matter what. Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/#findComment-526212 Share on other sites More sharing options...
DarkWater Posted April 24, 2008 Share Posted April 24, 2008 You need session_start at the top of every page, not in a conditional. Obviously $_SESSION['sess_loggedin'] won't be equal to anything because you didn't start the session. Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/#findComment-526214 Share on other sites More sharing options...
The Little Guy Posted April 24, 2008 Share Posted April 24, 2008 <?php session_start(); if (!$_SESSION["sess_loggedon"]) { /* use like this if sess_loggedon is boolean otherwise use if (!isset($_SESSION["sess_loggedon"])) { */ header ('location: index.html'); exit; } //Database Information include('../db_connecation.php'); // this then goes on to show the rest of the page html and other php ?> Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/#findComment-526221 Share on other sites More sharing options...
megz90 Posted April 24, 2008 Author Share Posted April 24, 2008 i thought it was that but it didnt work <?php session_start(); // admin page include('../db_connection.php'); if (!isset($_SESSION["sess_loggedon"])) { session_start(); } else header ('location: index.html'); ?> im sure ive tried that before. it still loads the page but again doesnt show any specific user info. sorry for the trouble but any ideas ? Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/#findComment-526225 Share on other sites More sharing options...
The Little Guy Posted April 24, 2008 Share Posted April 24, 2008 You don't need to start the session 2 times. try the code I posted above. Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/#findComment-526232 Share on other sites More sharing options...
DarkWater Posted April 24, 2008 Share Posted April 24, 2008 Asdkjf. Stop starting the session twice. You want to redirect them if they're NOT logged in, right? <?php session_start(); // admin page include('../db_connection.php'); if (!isset($_SESSION["sess_loggedon"])) { header ('location: index.html'); exit(); } //Then do what you want here. ?> Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/#findComment-526235 Share on other sites More sharing options...
megz90 Posted April 24, 2008 Author Share Posted April 24, 2008 You don't need to start the session 2 times. try the code I posted above. yes thanks i didnt get your reply until i had posted. it worked great. which is good cus i now have time to go through each and every page to change the code. i knew i should of included that bit of info just like i did the db connection. thanks. .... can i/you mark this as solved ? Link to comment https://forums.phpfreaks.com/topic/102744-session-doesnt-quite-work-properly/#findComment-526243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.