Jump to content

Ending a session and deleting cookies


Logical1

Recommended Posts

I am trying to make a log in/log off for my site I have problems with logging off.

I want to set cookies (username) for duration of session and clear it when they log off.

I can resigster a session and set cookie by this code:

 

session_start();

session_name("H1000");

session_register("LUN");

setcookie("LUN",$_POST["N1"],0, "/","www.123xyz.com",0);

 

So far so good.  Problem is that no matter what I try I can not get rid of the username cookie when I try to log off (I can still see the name on pages after I try to log off).

To log off this is the code:

 

<?php

session_start();

$_SESSION = array();

setcookie(session_name(H1000), '$LUN', time()-42000, '/');

session_destroy();

header('Location:index.php');

?>

 

Can somebody tell me what am I doing wrong?

Thanks in advance.

L1

Link to comment
Share on other sites

Because of how you're setting the cookies, it looks as if they'll never be set long enough to be deleted.

 

setcookie("HCC","1",0,  "/","www.123.com",0);

 

The third parameter here is the date of expiration as a UNIX timestamp. You have 0, so, if I'm not mistaken, the cookie is deleted as it's set.

 

Try this:

 

setcookie("HCC","1", time()+42000,  "/","www.123.com",0);

Link to comment
Share on other sites

Shouldn't killing it be more like this

<?php
session_start();
$_SESSION = array();
setcookie(session_name("H1000"), '', time()-42000, '/');
setcookie(session_name("LUN"), '', time()-42000, '/');
session_destroy();
header('Location:index.php');
?>

Link to comment
Share on other sites

I don't follow you.  If the cookie was deleted then after logging in I should not be able to see username (which I do).

(Also I was editing the question when you answered please look at it now).

Can somebody write or point to an example code where session is registed and one cookie is set and then in a secondary page session is ended and cookie cleared?

I have been going over php.net and many forums to see examples and tried at least a dozen of them.  Bless their hearts, often they don't try their code before posting it.

Thanks

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.