tags:

views:

98

answers:

7

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

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

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

Thanks!

A: 

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
}
ILMV
+1  A: 

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();
  }
?>
sundowatch
As well as `session_destroy`, you should also redirect the user and then `exit()`. Otherwise, the user won't appear logged out until they reload the page or load another.
Ben James
Yes that's right.
sundowatch
+1  A: 

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.

Select0r
Thanks, got it working that way!
+4  A: 

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();
kemp
+1. You might also want to use `session_destroy()`.
Ben James
Good point, added
kemp
+1  A: 

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.

Tgr
A: 

Logout Link:

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

logout.php

<?php
    session_start();
    session_destroy();
?>
Sarfraz
+1  A: 

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');

karim79