使用 jQuery 设置 PHP $_SESSION['var']

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/607673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:15:00  来源:igfitidea点击:

Setting a PHP $_SESSION['var'] using jQuery

phpjquerysession-variables

提问by mmundiff

I need to set a PHP $_SESSION variable using the jQuery. IF the user clicks on an image I want to save a piece of information associated with that image as a session variable in php.

我需要使用 jQuery 设置一个 PHP $_SESSION 变量。如果用户单击图像,我想将与该图像关联的一条信息保存为 php 中的会话变量。

I think I can do this by calling a php page or function and appending that piece of info to the query string.

我想我可以通过调用 php 页面或函数并将该信息附加到查询字符串来做到这一点。

Any ideas. I have found little help through google.

有任何想法吗。我通过谷歌几乎没有找到帮助。

thanks mike

谢谢迈克

回答by Luke Dennis

You can't do it through jQuery alone; you'll need a combination of Ajax (which you can do with jQuery) and a PHP back-end. A very simple version might look like this:

你不能单独通过 jQuery 做到这一点;您将需要 Ajax(您可以使用 jQuery)和 PHP 后端的组合。一个非常简单的版本可能如下所示:

HTML:

HTML:

<img class="foo" src="img.jpg" />
<img class="foo" src="img2.jpg" />
<img class="foo" src="img3.jpg" />

Javascript:

Javascript:

$("img.foo").onclick(function()
{
    // Get the src of the image
    var src = $(this).attr("src");

    // Send Ajax request to backend.php, with src set as "img" in the POST data
    $.post("/backend.php", {"img": src});
});

PHP (backend.php):

PHP(后端.php):

<?php
    // do any authentication first, then add POST variable to session
    $_SESSION['imgsrc'] = $_POST['img'];
?>

回答by Bryan A

Might want to try putting the PHP function on another PHP page, and use an AJAX call to set the variable.

可能想尝试将 PHP 函数放在另一个 PHP 页面上,并使用 AJAX 调用来设置变量。

回答by Hawk Kroeger

Whats you are looking for is jQuery Ajax. And then just setup a php page to process the request.

您正在寻找的是jQuery Ajax。然后只需设置一个 php 页面来处理请求。

回答by Nolte

A lot of responses on here are addressing the how but not the why. PHP $_SESSION key/value pairs are stored on the server. This differs from a cookie, which is stored on the browser. This is why you are able to access values in a cookie from both PHP and JavaScript. To make matters worse, AJAX requests from the browser do not include any of the cookies you have set for the website. So, you will have to make JavaScript pull the Session ID cookie and include it in every AJAX request for the server to be able to make heads or tails of it. On the bright side, PHP Sessions are designed to fail-over to a HTTP GET or POST variable if cookies are not sent along with the HTTP headers. I would look into some of the principles of RESTful web applications and use of of the design patterns that are common with those kinds of applications instead of trying to mangle with the session handler.

这里的很多回复都在解决如何而不是为什么。PHP $_SESSION 键/值对存储在服务器上。这与存储在浏览器上的 cookie 不同。这就是为什么您可以从 PHP 和 JavaScript 访问 cookie 中的值。更糟糕的是,来自浏览器的 AJAX 请求不包括您为网站设置的任何 cookie。因此,您必须让 JavaScript 提取会话 ID cookie 并将其包含在每个 AJAX 请求中,以便服务器能够对其进行正面或反面处理。从好的方面来说,如果 cookie 没有与 HTTP 标头一起发送,PHP 会话被设计为故障转移到 HTTP GET 或 POST 变量。

回答by antioch1st

in (backend.php) be sure to include include

在 (backend.php) 中一定要包含 include

session_start();

session_start();

-Taylor http://www.hawkessolutions.com

-泰勒 http://www.hawkessolutions.com

回答by Isaac

I also designed a "php session value setter" solution by myself (similar to Luke Dennis' solution. No big deal here), but after setting my session value, my needs were "jumping onto another .php file". Ok, I did it, inside my jquery code... But something didn't quite work...

我还自己设计了一个“php session value setter”解决方案(类似于 Luke Dennis 的解决方案。这里没什么大不了的),但是在设置我的 session 值之后,我的需求是“跳转到另一个 .php 文件”。好的,我做到了,在我的 jquery 代码中......但有些东西不太好......

My problem was kind of easy:

我的问题很简单:

-After you "$.post" your values onto the small .php file, you should wait for some "success/failure" return value, and ONLY AFTER READING THIS SUCCESS VALUE, perform the jump. If you just immediately jump onto the next big .php file, your session value might have not become set onto the php sessions runtime engine, and will you probably read "empty" when doing $_SESSION["my_var"]; from the destination .php file.

- 在您将您的值“$.post”到小 .php 文件后,您应该等待一些“成功/失败”返回值,并且只有在阅读此成功值后,才执行跳转。如果您立即跳转到下一个大的 .php 文件,您的会话值可能尚未设置到 php 会话运行时引擎上,并且您在执行 $_SESSION["my_var"] 时可能会读取 "empty"; 从目标 .php 文件。

In my case, to correct that situation, I changed my jQuery $.post code this way:

就我而言,为了纠正这种情况,我以这种方式更改了 jQuery $.post 代码:

$.post('set_session_value.php', { key: 'keyname', value: 'myvalue'}, function(ret){
    if(ret==0){
        window.alert("success!");
        location.replace("next_page.php");
    }
    else{
        window.alert("error!");
    }
});

Of course, your "set_session_value.php" file, should return 'echo "0"; ' or 'echo "1"; ' (or whatever success values you might need).

当然,你的“set_session_value.php”文件应该返回'echo“0”;'或'回声“1”;'(或您可能需要的任何成功值)。

Greetings.

你好。

回答by Waltur Buerk

Similar to Luke's answer, but with GET.

类似于卢克的回答,但使用 GET。

Place this php-code in a php-page, ex. getpage.php:

将此 php 代码放在 php 页面中,例如。获取页面.php:

<?php
$_SESSION['size'] = $_GET['size'];
?>

Then call it with a jQuery script like this:

然后用这样的 jQuery 脚本调用它:

$.get( "/getpage.php?size=1");

Agree with Federico. In some cases you may run into problems using POST, although not sure if it can be browser related.

同意费德里科。在某些情况下,您可能会在使用 POST 时遇到问题,但不确定它是否与浏览器相关。

回答by Petko Doichev

It works on firefox, if you change onClick() to click() in javascript part.

它适用于 Firefox,如果您在 javascript 部分将 onClick() 更改为 click()。

$("img.foo").click(function()
{
    // Get the src of the image
    var src = $(this).attr("src");

    // Send Ajax request to backend.php, with src set as "img" in the POST data
    $.post("/backend.php", {"img": src});
});