Jump to content

Weird session problem


etonB

Recommended Posts

I'm making a small website where an admin has special functions. I've managed to make my login/logout scripts with sessions and able to show the admin's special functions only to him.

 

The problem is when logging out and back in, it gets really weird. This is how I can come accross the error:

 

1. Enter my website

2. Log-in as 'admin'

3. Click on Add News (the special functions)

4. At this point, things work perfect, I can add news and go back to main page.

5. Log-out

6. Log-in as 'admin' again

7. Click on Add News

8. At this point, I get logged out and can't add news obviously until I log in again.

 

This is how I perform log-out

 

<p class="textoIngresar">Welcome, <?php echo $_SESSION["loggedUser"];?> ( <a href="index.php?action=logout">Log-out</a> )</p>

 

This is the actual log out function

 

if ( $_GET["action"] == "logout") 
	{
		session_unset();
		session_destroy();
	}

 

Am I doing the logout wrong? I need to know if that part is ok so I can look for errors elsewhere.

 

Thanks in advance,

etonB.

Link to comment
Share on other sites

sounds more like a problem with your session settings in your php.ini.

 

i used to get random logouts and it was related to the webhost's garbage collection process, which wiped my sessions out even if they weren't expired.

 

also probably unrelated but good to know, sessions at www.yoursite.com and yoursite.com are NOT the same.  i force the www to be there.

Link to comment
Share on other sites

I'm going to look into that (hosting @ ipage) later on because I don't actually have access to php.ini

 

But the thing is, the logouts aren't random as they always happen when I do the steps shown.

 

Is my logout done correctly? I've read other advice like doing $SESSION = array(); instead of unset_session();  but I'm clueless.

Link to comment
Share on other sites

foreach ($_SESSION as $key) { unset($key); }
session_destroy();

That should unset all the session variables you have set and terminate the session. Its really just an alternative to the code your already using

 

And try putting error_reporting(E_ALL) at the top of your script. It helps sometimes

 

also, just incase, check that you do have a session_start() at the top of your page. Sometimes we forget it

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.