开发者

a log-out hyperlink in PHP?

In case somebody knows, how can I make a hyperlink in PHP...

<?php
  echo( '<a href="index.ph开发者_开发知识库p">Log-out</a>' );
?> 

that would not only to navigate to the first page, but also remove cookies?

Thanks!


You can make another page which clears all the cookies (i.e. sets them to expire in the past) and then redirects to index.php:

// page: clear.php
<?php
session_start();
$_SESSION = array();
session_destroy();

setcookie('cookie1', '', strtotime('-2 days'));
setcookie('cookie2', '', strtotime('-2 days'));
// etc.
header('Location: index.php');
exit();


I usually use the method prescribed by the manual:

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>

The only thing that remains is header('Location: index.php');


Submit a parameter in your link like index.php?logout=true, check for that parameter in your index.php and if set, delete cookies:

http://php.net/manual/de/function.setcookie.php

If you set the "lifetime" (expire) of a cookie to something in the past (or leave it out completely), it will be removed on the next pageload (do a Google search for "php delete cookie" to find help). Force a page reload, if needed.

You may also want to destroy the user's session.


Here's your HTML link

<a href="index.php?logout">Log-out</a>

And your PHP to handle to logging out

if(isset($_GET['logout'])) {
    // clear the session variable, display logged out message
}


Use link like that:

<?php
  echo( '<a href="index.php?link=logout">Log-out</a>' );
?> 

And index.php is:

<?php
  $link = $_GET["link"];
  if($link == "logout")
  {
     session_destroy();
  }
?>


In the navigation menu:

<a href="logout.php">Log out</a>

In logout.php:

<?php
// kill the session
header('Location: index.php');
exit();    

For killing the session, see the example at session_destroy() in the PHP manual.


Logout Link:

<a href="logout.php">Log Out</a>

logout.php

<?php
    session_start();
    session_destroy();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜