Jump to content

Session destroys only a page later?


andr923

Recommended Posts

This is the code that I'm using to check if someone is logged in or not.  This code is on every page because there is a login or logout button on every page.

 

if (!isset($_SESSION['user'])){
	echo '<a href="login.php">LOGIN</a>';
	}else{
	echo '<a href="logout.php">LOGOUT</a>';
	}

 

In the flow of my php document this checking if the user is logged in happens after the actual login or logout script.  Because of this, I assumed that as soon as someone logs in, the code would reflect the change.  For some reason, on the page that logs me in, the code still thinks that the session is not set yet and therefore I still see the login option.  Same thing with logout.  It takes for me to click on another page for the code to finally change.  Any ideas what is going on?

 

Thanks guys!

Link to comment
Share on other sites

I fixed a problem with logging in.  Now as soon as you log in, the code shows the logout link.  It was an issue of making sure the right code happened in the right order (it was kind of confusing with the mixture of including files and functions).

 

The LOGOUT however is still an issue.  It is definitely not an issue with the order in which the code appears because right at the top of the page i have the session_destroy() command:

 

<?php
session_start();

$title = 'Logout';

// Logout Session

session_destroy();

 

but for some reason, later on in the page i have this code:

 

function logged() {

if (!isset($_SESSION['user'])){

		echo '<a href="login.php">LOGIN</a>';
		}else{
		echo '<a href="logout.php">LOGOUT</a>';
		}
}

 

this code returns the 2nd option with the LOGOUT link.  and yet again later I have:

 

echo "<center><p>You have successfully logged out! {$_SESSION['user']}</p></center>"

 

I put in the $_session['user'] variable just as a check to try and see if indeed the session info was still available.  It seems like the session only gets destroyed after I leave the page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.