【问题标题】:Log out user from php session从 php 会话中注销用户
【发布时间】:2015-08-05 17:17:56
【问题描述】:

当用户登录时,我希望他们有一个退出按钮并被重定向到他们所在的页面,但是,还有一些额外的功能,这些功能被调用。不幸的是,按下注销按钮时没有任何反应。

这是 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']);
   }
 ?>

【问题讨论】:

    标签: php session


    【解决方案1】:
    1. 将用户重定向到注销 php 脚本 (logout.php)
    2. 通过调用session_start() 继续会话
    3. 检查$_SESSION['uname']是否为空...如果为空则调用session_destroy()销毁会话
    4. 将用户重定向回主页header("/")

    //HTML

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

    -或-

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

    -或-

    <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: /");
    ?>
    

    【讨论】:

    • @Jules 我以 $_SESSION['userid'] 为例......这就是我识别用户的方式......我猜你的有点不同。您使用什么会话变量来识别您的用户?
    • 我使用的会话变量是 $_SESSION['uname']
    • 现在是这个样子,是不是应该是这个样子?我猜不是。
    【解决方案2】:

    只需将其保存为“login.php”并从浏览器访问。 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; ?>
    

    在您网站的任何其他 (PHP) 页面中,您可以通过检查限制对经过身份验证的用户的访问:

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

    【讨论】:

    • 这是否是您的 logout.php 的完整代码?看起来您发布的那个缺少
      标签?
    • 此外,有时 setcookie 和 session_destroy 与 header:location 重定向一起使用时无法正常工作。尝试 &lt;meta name="Refresh" content="0;url=&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"/&gt; 而不是你的 header:location。
    猜你喜欢
    • 2012-05-11
    • 2019-09-30
    • 2017-01-26
    • 1970-01-01
    • 2015-11-04
    • 2021-11-04
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    相关资源
    最近更新 更多