Solved

Logout button that deletes a cookie?

Posted on 2004-04-12
5
1,566 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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