如何使用PHP删除所有浏览器cookie?

时间:2015-01-06 00:10:45

标签: php cookies logout

我有两个在同一个域上运行的应用程序实例,它们位于

  1. 主文件夹/
  2. 子文件夹/staging/
  3. 我正在"/staging"实例上测试一些代码。 当用户退出时,我想完全清除所有他/她的cookie。

    登录和注销在同一文件index.php.

    中处理

    要退出,用户必须点击带有她/他的链接index.php?action=logout

    我在这里遇到的问题是,当用户注销时,所有Cookie都会被删除。名为phone_call_timer的cookie不会被删除,除非我再次刷新注册(即index.php?action=logout)页面(2次),这对我来说没有,但这就是我所面对的!我不知道我的代码或PHP 5.5.12

    中的某个地方是否存在问题

    所以我第一次去index.php?action=logout注销,但仍会保存1个cookie(即phone_call_timer

    但是,当我刷新(F5)页面“再次执行注销代码”时,cookie就会消失。

    为什么第一次不删除phone_call_timer Cookie? 我该如何解决这个问题?

    注意:如果我导航到index.php或任何其他网址,则Cookie永远不会消失。

    这是第一次注销后的屏幕截图(正如您所看到的那样phone_call_timer cookie仍然存在,但此时应该已将其删除。 enter image description here

    这是我再次刷新退出页面后的屏幕截图(正如您可以看到phone_call_timer cookie现已消失!) enter image description here

    这是我的PHP代码index.php文件

    的内容
    <?php
    
        require 'requires/APP_configuration.php';
        require 'requires/PHP_generic_functions.php';
    
        require 'classes/Connection.php';
        require 'classes/Session_Manager.php';
    
        //define this page name
        define('pageTitle' , SYSTEM_NAME . " - Sign In");
        define('pageDescription', "Sign In");
        define('pageKeywords', "Sign In");
    
        define('ADD_GLOBAL_META_TAGS', '
            <script type="text/javascript" src="js/captcha/jquery.captcha.js"></script>
            <link type="text/css" href="js/captcha/captcha.css" rel="stylesheet">
            ');
    
    
    
        $session = new \classes\Session_Manager(SESSION_NAME, SESSION_MAX_AVAILABLE, SESSION_SAVE_PATH, SESSION_SAVE_URL, SESSION_HTTPS_ONLY);
        $ref = "none";
        $newURL = 'index_bridge.php';
        $redirectTo = '';
        $newURLAfterRefresh = 'index.php';
        $action = '';
        $loggedOut = 0;
    
        if(      isset($_GET['ref']) 
            && ! empty($_GET['ref']) ){
            $ref = $_GET['ref'];
        }
    
    
    
        if( isset($_GET['redirectTo']) && !empty($_GET['redirectTo']) ){
            $redirectTo = $_GET['redirectTo'];
            $newURL = $redirectTo;
        }
        if(!empty($redirectTo)){
            $newURLAfterRefresh = 'index.php?ref=' . $ref . '&redirectTo=' . $redirectTo;
        }
    
        $newURLshort = $newURL;
        if(strpos($newURL, '?') !== false){
            $newURLshort = substr($newURL, 0, strpos($newURL, '?') );
        }
    
        $file_found = file_exists($_SERVER['DOCUMENT_ROOT'] . $newURLshort);
    
        if(!$file_found){   
            $newURL = 'index_bridge.php';
        }
    
    
    
        /********** START CODE **********/
        //BEGIN current page's code
    
        if(isset($_GET['action'])){
            $action = $_GET['action'];
        }
    
        if(empty($action)){
            $session->resumeSession();
    
            if( $session->isAutherized() === true ){
                header('Location: index_bridge.php');
                exit();
            }   
        }
    
        if($action == 'logout' ){
    
            $cookies = array();
            $var = str_replace('/','', SESSION_SAVE_PATH);
            if (isset($_SERVER['HTTP_COOKIE'])) {
                $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
            }
    
            $session->destroy();    
            foreach($cookies as $cookie) {
    
                $parts = explode('=', $cookie);
                $name = trim($parts[0]);
    
                if($name == 'phone_call_timer' && $ref == 'auto'){
                    continue;
                }
    
                setcookie($name, null, time()-MAX_STORE_COOKIE);
                setcookie($name, null, time()-MAX_STORE_COOKIE, '/');
                setcookie($name, null, time()-MAX_STORE_COOKIE, $var);
                setcookie($name, null, time()-MAX_STORE_COOKIE, SESSION_SAVE_PATH);
    
                unset($_COOKIE[$name]); 
            }
    
            $loggedOut = 1; 
        }
    
    
    /**********************************************************
    ********************* START OUTPUT ************************
    **********************************************************/
        //This should always be the first output
        include 'includes/meta.php';
        ?>
    <script>
    $(function(){
    
        $('#user_field,#password_field').keydown(function (e){
            if(e.keyCode == 13)
                $('#sign_in').click();
    
        });
    
        $('#sign_in').on('click', function(e){
    
            e.preventDefault();
            $(this).html('Please wait...');
    
            if ($(this).prop("disabled"))
                return false;
    
            $(this).prop("disabled", true);
    
                $('#respond').removeClass('bg-danger').text('');
                $.ajax({    
                    type: 'POST',
                    url: 'ajax/handler-sign-in.php',        
                    data: $('#signinForm').serialize(),
                    dataType: 'json',
                    cache: false,
                    timeout: 10000,
                    success: function(data) { 
    
                        if(!data)
                            return;
    
    
                        if(data.success == true)                    
                            window.location.href = '<?php echo $newURL ?>';                 
                        else {
                            $('#respond').addClass('bg-danger').text(data.msg);
                            $('#sign_in').prop("disabled", false);
                            $('#sign_in').html('Sign In');
    
                            if(data.human_check_required == 1)
                                $("#humachCheck").show();
                            else 
                                $("#humachCheck").hide();
    
                        }
    
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $('#responseAddForm').removeClass().addClass('errorBox').html('<p>There was an<strong> ' + errorThrown + '</strong> error due to a<strong> ' + textStatus + '</strong> condition.</p>').fadeIn('fast');         
                    }
                });
        });
    
        $("#humachCheck").captcha({
            captchaDir: "js/captcha",
            formId: "signinForm",
            url: "ajax/set-correct-captcha.php"
        });
    
    });
    </script>
    
    <?php
    
    
    
    
        //Display the activity table
         echo '<div>';
         echo '<div class="Body">';
    
            if($loggedOut){
                echo '<div class="passBox"><p style="text-align: center;">You have successfully logged out<br><a href="'.$newURLAfterRefresh.'">Click Here</a> to login.</p></div>';
    
            } else {
    
                //login form
                echo '  <div style="width: 350px; margin: 0 auto;">
                            <div class="account-wall">
                                <img class="profile-system-logo" src="images/accordiLogoX200.png" alt="Logo">
                                <h1 class="text-center login-title">Sign in to continue to '.SYSTEM_NAME.'</h1>
                                <div id="respond" class="margin-lg-around center-block padding-md-around"></div>
                                <form class="form-signin" id="signinForm" method="post">
                                <input type="text" class="form-control" name="username" id="user_field" placeholder="User Name" required autofocus="autofocus">
                                <input type="password" class="form-control" id="password_field" name="password" placeholder="Password" required>
                                <div id="humachCheck" class="box-hidden" style="margin: 10px 0;">You must enable javaScript in order to use this system</div>
                                <div class="btn btn-lg btn-primary btn-block" id="sign_in">Sign in</div>
                                </form>
                            </div>
                        </div>';
    
    
            }
    
    
        //close Body DIV
        echo '</div>';
    
    
    
    
    
    include 'includes/menu_master_bottom.php';
    include 'includes/footer.php';
    
    
    
    /**********************************************************
    ********************* END OUTPUT ************************
    **********************************************************/
    
    /********** END CODE **********/
    
    
    ?>
    

0 个答案:

没有答案