php 如何在php中注销用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17949713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:42:40  来源:igfitidea点击:

How to logout a user in php?

phpsessionlogout

提问by Zippylicious

I am making a login system and I just got it to work, and now I am having difficulty making a logout feature for my website. Its not actually hosted yet, so security will come later. I have tried various uses of session_destroy and unset, but i cannot get it to work. Any help would be appreciated.

我正在制作一个登录系统,我刚刚让它工作,现在我在为我的网站制作注销功能时遇到了困难。它实际上尚未托管,因此安全性将在稍后提供。我尝试了 session_destroy 和 unset 的各种用途,但我无法让它工作。任何帮助,将不胜感激。

My PHP

我的 PHP

<?php
session_start();
/*This is the equivalent of login.php*/
$database = "forum";  // the name of the database.
$server = "localhost";  // server to connect to.
$db_user = "root";  // mysql username to access the database with.
$db_pass = "";  // mysql password to access the database with.
$table = "members";    // the table that this script will set up and use.
$link = mysql_connect($server, $db_user, $db_pass);
mysql_select_db($database,$link);

if (isset($_POST['fsubmitted'])) {
// Get the data passed from the form
$username = $_POST['username'];
$pass = $_POST['pass'];

// Do some basic sanitizing
$username = stripslashes($username);
$pass = stripslashes($pass);
$encryptedpass = md5($pass);

$sql = "SELECT * from members where username = '$username' and password = '$encryptedpass'";
$result = mysql_query($sql);


$count = 0;

$count = mysql_num_rows($result);

if ($count == 1) {
     $_SESSION['loggedIn'] = "true";
    header("Location: index.php"); // This is wherever you want to redirect the user to
    exit();

} else {
     $_SESSION['loggedIn'] = "false";
     echo '<div class="errormsgbox">Your username and password combo was incorrect!</div>';
     var_dump($result);
    echo $sql;
}
}

if ($_SESSION['loggedIn'] = "true") {
    echo '<div class="success">You are now logged in!</div>';
}

if (isset($_SESSION['loggedin']) && (time() - $_SESSION['loggedin'] > 1800)) {
    // last request was more than 30 minutes ago
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();   // destroy session data in storage
}
$_SESSION['loggedin'] = time(); // update last activity time stamp

?>

回答by Ben Fortune

Use:

用:

session_start();

With either of the following:

具有以下任一项:

session_destroy();
session_unset();
unset($_SESSION["loggedin"]);
$_SESSION = array();

回答by pattyd

I can see two errors in your code:

我可以在您的代码中看到两个错误:

  • You have not started the session
  • You have not told the code which session to unset/destroy
  • 您还没有开始会话
  • 您没有告诉代码要取消设置/销毁哪个会话

Starting the session:

开始会话:

Put this as the first line of your PHP code: session_start();

将其作为 PHP 代码的第一行: session_start();

Logging out

注销

Lets take a closer look at this block of code:

让我们仔细看看这段代码:

if (isset($_SESSION['loggedin']) && (time() - $_SESSION['loggedin'] > 1800)) {
    // last request was more than 30 minutes ago
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();   // destroy session data in storage
}

You have not told the code which session to unsetor destroy!

你没有告诉代码哪个会话unsetdestroy

To do that, you must include a session variable inside of the parentheses.

为此,您必须在括号内包含一个会话变量。

Try this code:

试试这个代码:

if (isset($_SESSION['loggedin']) && (time() - $_SESSION['loggedin'] > 1800)) {
// last request was more than 30 minutes ago
session_unset($_SESSION['loggedin']);     // unset $_SESSION variable for the run-time 
session_destroy($_SESSION['loggedin']);   // destroy session data in storage
} 

All i did was, told the code which session to unsetand destroy

我所做的就是告诉代码哪个会话unsetdestroy

UPDATE
Try this instead if that didn't quite work for you.

更新
如果这对您不起作用,请尝试使用此方法。

if (isset($_SESSION['loggedin']) && (time() - $_SESSION['loggedin'] > 1800)) {
// last request was more than 30 minutes ago
unset($_SESSION['loggedin']);     // unset $_SESSION variable for the run-time 
$_SESSION['loggedin'] = "false";
} 

Another good thing to take a look at is having a logout button. This is explained here: Logout button php

另一个值得关注的好处是有一个注销按钮。这在这里解释:Logout button php

I hope this helped you out, and let me know if I can be of further help!

我希望这对您有所帮助,如果我能提供进一步的帮助,请告诉我!

回答by bizzehdee

In your script to log someone out, simply have:

在您将某人注销的脚本中,只需:

session_start();
unset($_SESSION);
session_destroy();

You should also call session_start()at some point in your login.php you have shown above.

您还应该session_start()在上面显示的 login.php 中的某个点调用。

回答by Hunter WebDev

I would try using this function:

我会尝试使用这个功能:

function logout(){
    if(session_id() == '') { // start session if none found
        session_start();
    }

    session_unset();         
    session_destroy();
    unset($_SESSION['loggedIn']);
}

All you need to do to use this function is call logout(); where ever you want to have the person logged out.

使用这个函数你需要做的就是调用 logout(); 你想让这个人登出的地方。