Solved

Logout button that deletes a cookie?

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

708 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

14 Experts available now in Live!

Get 1:1 Help Now