Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1572
  • Last Modified:

Logout button that deletes a cookie?

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
tab26
Asked:
tab26
2 Solutions
 
techtonikCommented:
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
 
tab26Author Commented:
Could you help me on how I would put that on a button?

Thanks!!
0
 
FataquiCommented:
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
 
techtonikCommented:
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
 
trunks222Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now