2010-05-10 142 views
1

万一有人知道,我怎样才能使在PHP中的超链接...在PHP注销超链接?

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

,它不仅导航到第一页,还能清除cookies吗?

谢谢!

+0

我一直在关注您的所有问题,而且您确实需要阅读教程和/或PHP文档。如果您只是简单地学习了PHP,您就会不断提出基本问题,这些问题很容易解答 – TravisO 2010-05-10 13:28:07

回答

1

提交的参数在你的链接像index.php?logout=true,您在您的index.php参数如果设置,删除cookies:如果你设置了“终身”

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

(过期)一个cookie的(或者完全保留),它会在下一页的页面上被删除(做一个谷歌搜索“php delete cookie”来寻找帮助)。如果需要,强制页面重新加载。

您可能还想销毁用户的会话。

+0

谢谢,它的工作方式! – 2010-05-10 09:24:03

1

这是你的HTML链接

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

和你的PHP来处理,以注销

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

使用链接这样的:

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

而且index.php文件是:

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

和'session_destroy'一样,你也应该重定向用户,然后'exit()'。否则,用户将不会显示注销,直到他们重新加载页面或加载另一个页面。 – 2010-05-10 09:18:07

+0

是的,没错。 – sundowatch 2010-05-10 09:20:21

4

你可以让另一页它清除所有的cookies(即设置他们在过去的到期),然后重定向至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(); 
+1

+1。你可能也想使用'session_destroy()'。 – 2010-05-10 09:19:49

+0

好点,补充 – 2010-05-10 09:23:44

1

在导航菜单:

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

logout.php

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

杀死会话,看到考试请参阅PHP手册中的session_destroy()

1

注销链接:

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

logout.php

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

我通常使用由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(); 
?> 

剩下的唯一事情是header('Location: index.php');