Solved

Logout button that deletes a cookie?

Posted on 2004-04-12
5
1,568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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