Jump to content

session doesnt quite work properly


megz90

Recommended Posts

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

<?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
?>

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 ?

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.

?>

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 ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.