简体   繁体   English

从php会话注销用户

[英]Log out user from php session

When a user is logged in, I want them to have a button to log out and be redirected to the page they are on, however, with a few additional features, which are called. 当用户登录时,我希望他们有一个按钮可以注销并重定向到他们所在的页面,但是,它具有一些附加功能,称为。 Unfortunately, nothing happens when the log out button is pressed. 不幸的是,当按下注销按钮时,什么也没有发生。

This is the code for the logout.php file. 这是logout.php文件的代码。

<input type="submit" type="submit" name="submit" value="Log out">
<?php
    if (isset($_POST['submit'])){
       session_start();
       $_SESSION = array();
       if (ini_get("session.use_cookies")) {
       $yesterday = time() - (24 * 60 * 60); $params = session_get_cookie_params();            
       setcookie(session_name(), '', $yesterday,
       $params["path"], $params["domain"],
       $params["secure"], $params["httponly"] );
     }
     session_destroy();
     header('Location: '.$_SERVER['PHP_SELF']);
   }
 ?>
  1. Redirect user to a logout php script (logout.php) 将用户重定向到注销php脚本(logout.php)
  2. Continue the session by calling session_start() 通过调用session_start()继续会话
  3. Check if $_SESSION['uname'] is empty or not... if empty then destroy the session by calling session_destroy() 检查$_SESSION['uname']是否为空...如果为空,则通过调用session_destroy()销毁会话
  4. Redirect the user back to the home page header("/") 将用户重定向回首页header("/")

//HTML // HTML

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

-or- -要么-

<input type="button" value="Logout" onclick="window.location.href = 'logout.php';">

-or- -要么-

<button onclick="window.location.href = 'logout.php';">Logout</button>

//LOGOUT.PHP //注销

<?php
//continue current session
session_start();
//check to see if session variable (uname) is set
//if set destroy the session
if(!empty($_SESSION['uname'])){
    session_destroy();
}
//redirect the user to the home page
header("Location: /");
?>

Just save it as " login.php " and access from the browser. 只需将其另存为“ login.php ”并从浏览器访问即可。 UserId is " user " and Password is " pass ". UserId是“ user ”,密码是“ pass ”。

<?php session_start(); ?>
<?php if ( array_key_exists( 'uid', $_SESSION ) ): ?>
    <?php /* if $_SESSION['uid'] is set, user is already logged in */ ?>
    <?php if ( array_key_exists( 'cmd', $_GET ) && $_GET['cmd']==='logout' ): ?>
        <?php
            unset( $_SESSION[ 'uid' ] );
            unset( $_SESSION[ 'error' ] );
            session_destroy();
        ?>
        <h1>See you soon!</h1>
        Click 
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?r=<?php echo md5(uniqid(rand())); ?>">here</a>
        if you don&#39;t get redirected automatically within 5 seconds.
        <script type="text/javascript">
            setTimeout( function() { 
                location.href = "<?php echo addslashes( $_SERVER['PHP_SELF'] ); ?>?r=<?php echo md5(uniqid(rand())); ?>"; 
              }, 5000 );
        </script>
    <?php else: ?>
        <h1>You are logged in</h1>
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?cmd=logout&amp;r=<?php echo md5(uniqid(rand())); ?>">Log Out</a> | 
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?r=<?php echo md5(uniqid(rand())); ?>">Refresh</a>
    <?php endif; ?>
<?php else: ?>
    <?php /* user is not logged in */ ?>
    <?php if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ): ?>
        <?php
            // login (POST) request, let's check if credentials 
            // are correct
            unset( $_SESSION['error'] );
            if ( array_key_exists( 'userid', $_POST ) && array_key_exists( 'passwd', $_POST ) )
                {
                    $userid = trim( $_POST['userid'] );
                    $passwd = trim( $_POST['passwd'] );
                    if ( $userid === '' )
                        {
                            $_SESSION['error'] = 'No userid supplied';
                        }
                    elseif ( $passwd === '' )
                        {
                            $_SESSION['error'] = 'No password supplied';
                        }
                    elseif ( $userid !== 'user' || $passwd !== 'pass' )
                        {
                            $_SESSION['error'] = 'Wrong userid or password';
                        }
                    else
                        {
                            $_SESSION['uid'] = $userid;
                            // from now on, the user is logged in
                        }
                }
            else
                {
                    $_SESSION['error'] = 'Missing userid or password';
                }

            // redirect the user anyways
            // this gets rid of posted data if this was a POST,
            // so when user reloads the page it doesn't try to
            // re-authenticate
            // Can be a different URL if user is logged in 
            // successfully
            header( 'Location: ' . $_SERVER['PHP_SELF'] . '?r=' . md5(uniqid(rand())) );
            exit;
        ?>
    <?php else: ?>
        <?php /* user not logged in, let's display login form */ ?>
        <h1>Log In</h1>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <label for="user-id">User Id</label>
            <input type="text" name="userid" id="user-id" />
            <label for="user-pwd">Password</label>
            <input type="password" name="passwd" id="user-pwd" />
            <input type="submit" value="Log In" />
        </form>
        <?php /* in case any errors occured, show them */ ?>
        <?php if ( array_key_exists( 'error', $_SESSION ) && $_SESSION['error'] ): ?>
            <div class="error-msg"><?php echo $_SESSION['error']; ?></div>
        <?php endif; ?>
    <?php endif; ?>
<?php endif; ?>

In any other (PHP) page of your site, you can then restrict access to authenticated users by checking: 然后,在您网站的任何其他(PHP)页面中,可以通过检查以下内容来限制对经过身份验证的用户的访问:

<?php
    if ( array_key_exists( 'uid', $_SESSION ) )
        {
            /* user is logged in, do whatever */
        }
    else
        {
            /* user is NOT logged in, do whatever else */
        }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM