Solved

Logout button that deletes a cookie?

Posted on 2004-04-12
5
1,563 Views
Last Modified: 2009-07-29
Hello,

Could someone help me out with the code on how to create a logout button or link in the upper right hand corner of a page?  So it will delete a cookie named "name" and send the user back to the login page.  

I have been able to make a login script but I'm having trouble with this.

Thanks for your help,

tab26
0
Comment
Question by:tab26
5 Comments
 
LVL 9

Expert Comment

by:techtonik
ID: 10809632
To delete cookie you need to set cookie date somewhere in the past. Like that:
setcookie("name","",time()-3600);

but first you may also wish to erase all session variables:
session_start();
$_SESSION = array();

and finally redirect user to your login page:
header("Location: http://yourserver.com/login.php");

or if you need relative redirection:
header("Location: http://".$_SERVER['HTTP_HOST']
                     .dirname($_SERVER['PHP_SELF'])
                     ."/".$relative_url);
0
 

Author Comment

by:tab26
ID: 10809654
Could you help me on how I would put that on a button?

Thanks!!
0
 
LVL 2

Assisted Solution

by:Fataqui
Fataqui earned 45 total points
ID: 10810796
Hi


link on your main page.......

<a ref='logout.php'>Log Out</a>


LOGOUT.PHP

<?

// I put session_start here, but you should have a centralized
// sessions handler that you would call here after calling your
// include file that would hold the sessions routine. putting logic
// into you script from the start will help you manage it later
// with much less hassle....

session_start ();

// now you can put any routine you want to call before you log
// this user out. Because this example does not centralize the
// sessions handler we must verify the user is logged in before
// doing anything else....

      if ( !empty ( $_SESSION['service_name']['user_id'] ) )
      {

      // now run any function or routine you need to run
      // for this user before logging them out....

            // example.... ( update user pref... )

            if( !empty ( $_SESSION['local']['pref'] ) )
            {

                  $sql->uppref ( s_array () );

            }

            // make the cookie unactive

            setcookie ( 'name', '', ( time () - 3600 ) );

            // empty the session array

            $_SESSION = array ();

            // remove the the session

            session_destroy ();

            // set the header location page...

            $page = 'index.php';

      }
      else
      {

            // we end up here, because no valid session
            // was found, so kick them over to the error
            // page because they should not be here...

            $page = 'error.php';

      }

      //done processing, send them where they need to go!

      header ( 'Location: ' . $page );

      // clean out the memory process

      exit ();

?>


F!

0
 
LVL 9

Accepted Solution

by:
techtonik earned 80 total points
ID: 10811858
The redirection above will not work. You need to specify absolute URI in $page variable. You can't put this action on a button since it is server-side PHP script, but you can direct browser to call this script when you press the button. And yes, thanks Fataqui for forgotten session_destroy();

To say the truth you don't need to destroy session variables if don't use Sessions (http://www.free2code.net/tutorials/programming/php/4/sessions.php). In this case you can delete lines with //1 comment. So, finally..
-----[logout.php]
<?php
// set this for yours
$login_page_full_uri = "http://yourserver.com/login.php";

session_start();                           //1
$_SESSION = array();                  //1

setcookie("name","",time()-3600);
setcookie( session_name() ,"",0,"/");  //1

session_destroy();                            //1
header("Location: ".$login_page_full_uri);
?>

-----[logout_button_to_include.html]
<form action="logout.php" method="post">
<input type="submit" value="Logout" />
</form>


Try this. CU.
0
 

Expert Comment

by:trunks222
ID: 11216417
This thing does not work at all for me.
I have a cookie, but it seems like the cookie doesnt get deleted.

here's the code: for the creation of the cookie


setcookie("rockadmin",$_POST[login],time()+100000,"/");

but i had

setcookie("rockadmin",$_POST[login],null,"/");

neither work.

can i have any help from here?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now