是否可以使用Javascript读取PHP会话? [英] Is it possible to read a PHP session using Javascript?

查看:80
本文介绍了是否可以使用Javascript读取PHP会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cakePHP 1.26.
在控制器中,我得到了一个包含以下代码行的函数:

I am using cakePHP 1.26.
In a controller, I got a function which contains these lines of code:

$this->Session->write('testing', $user);
$this->Session->read('testing');

现在,系统编写了一个会话并将其存储在服务器上. 是否可以使用Javascript或Jquery读取名为测试"的会话?

Now the system wrote a session and stored on the server. Is it possible to use Javascript or Jquery to read the session named 'testing' ?

推荐答案

PHP会话存储在Web服务器的内存中,而JavaScript在WebClient(Web浏览器)中运行.在现实世界中,它们是两个物理上分离且独立的机器.他们通常只能使用HTTP协议通过网络相互通信.

The PHP session is stored in webserver's memory while JavaScript runs in the webclient (webbrowser). Those are in real world two physically separate and independent machines. They usually can only communicate with each other over network using HTTP protocol.

您有2个选择:

  1. 让PHP以类似于JS变量的方式打印会话数据:

  1. Let PHP print the session data as if it's a JS variable:

<script>var data = '<?= $_SESSION['data'] ?>';</script>

  • 让JS使用 Ajax 从服务器端请求它.这是一个基于 jQuery 的示例:

  • Let JS request it from the server end using Ajax. Here's a jQuery based example:

    <script>$.get('script.php', function(data) { /* .. */ });</script>
    

    基本上是在script.php中:

    <?php echo $_SESSION['data']; ?>
    

  • 不用说选项1是最简单直接的.

    Needless to say that option 1 is the most easy and straightforward.

    这篇关于是否可以使用Javascript读取PHP会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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