如何使用Jquery/Javascript清除PHP会话? [英] How to clear a PHP session using Jquery/Javascript?

查看:73
本文介绍了如何使用Jquery/Javascript清除PHP会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只想问一下是否可以使用jquery或javascript进程清除PHP会话?因为我要做的是使用对话框清除PHP会话.如果用户单击确定"按钮,它将处理未设置的功能.在我的页面中,有一个结帐过程,用户单击确定"按钮后,将提示一个对话框,如果用户单击确定"按钮,它将取消PHP会话,并将其重定向到另一个页面.

这是我的简单代码:

<i class="fa fa-check"></i> <span id="save_registry" class="cursor: pointer">OK</span>

<div id="save_alert" style="display: none">
    <?php unset($this->session->data['cart']);
</div>


....

$('#save_registry').click(function(){

        $('#save_alert').dialog({
            modal: true,
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                    location = 'index.php?route=common/home'; 
                }
            }
        });

});

解决方案

简单地说:您不能直接 .

PHP是一种服务器端语言,而javascript是客户端语言,这意味着它们之间除了您接收和交互的文档外没有其他连接.和你.会话由服务器存储和管理.

但是,正如War10ck所建议的,您可以调用清除会话的服务器端脚本.像这样:

PHP:

<?php
    /* 
     * session_start();
     * Starting a new session before clearing it
     * assures you all $_SESSION vars are cleared 
     * correctly, but it's not strictly necessary.
     */
    session_destroy();
    session_unset();
    header('Location: continue.php'); 
    /* Or whatever document you want to show afterwards */
?>

HTML/javascript:

<script type="text/javascript">
    function logout() {
        document.location = 'logout.php';
    }
    LogoutButton.addEventListener('click', logout, false);
</script>

<button id="LogoutButton">Log out</button>

甚至执行异步调用:

function logout() {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        document.location = 'continue.php';
    }
    xhr.open('GET', 'logout.php', true);
    xhr.send();
}

Just want to ask if is it possible to clear a PHP session using a jquery or javascript process? Because what I want to do is to clear the PHP session using a dialog box. If the user click the 'OK' button it will process the unset function. In my page there's a checkout process and after the user click the button 'OK' it will prompt a dialog box and if the user click the 'OK' button it will unset the PHP session and it will be redirected to another page.

Here's my simple code:

<i class="fa fa-check"></i> <span id="save_registry" class="cursor: pointer">OK</span>

<div id="save_alert" style="display: none">
    <?php unset($this->session->data['cart']);
</div>


....

$('#save_registry').click(function(){

        $('#save_alert').dialog({
            modal: true,
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                    location = 'index.php?route=common/home'; 
                }
            }
        });

});

解决方案

Simply put: You can't, directly.

PHP is a server-side language while javascript is a client-side one, which means that there's no other connection between them other than the document you receive and interacts with you. The sessions are stored and managed by the server.

You can, however, as War10ck suggests, call to a server-side script that clears the session. Something like this:

PHP:

<?php
    /* 
     * session_start();
     * Starting a new session before clearing it
     * assures you all $_SESSION vars are cleared 
     * correctly, but it's not strictly necessary.
     */
    session_destroy();
    session_unset();
    header('Location: continue.php'); 
    /* Or whatever document you want to show afterwards */
?>

HTML/javascript:

<script type="text/javascript">
    function logout() {
        document.location = 'logout.php';
    }
    LogoutButton.addEventListener('click', logout, false);
</script>

<button id="LogoutButton">Log out</button>

Or even performing an asynchronous call:

function logout() {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        document.location = 'continue.php';
    }
    xhr.open('GET', 'logout.php', true);
    xhr.send();
}

这篇关于如何使用Jquery/Javascript清除PHP会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆