[英]How to assign value to $_SESSION with AJAX?
Yes, I'm aware that this question has already been post, but ... I'm looking for a way to check if the Client has Javascript Enabled. 是的,我知道这个问题已经发布,但是...我正在寻找一种方法来检查客户端是否启用了Javascript。
Here is my idea : At every loading page, PHP initialize $_SESSION['is_js']
to 0
. 这是我的想法 :在每个加载页面,PHP都将
$_SESSION['is_js']
初始化为0
。 Then, I wish AJAX to ( via launching a script php ? ) try to set $_SESSION['is_js']
to 1
. 然后,我希望AJAX能够( 通过启动脚本php? )尝试将
$_SESSION['is_js']
为1
。 If JS is enabled, AJAX will succeed, if not, the value remains 0. 如果启用了JS,则AJAX将成功,否则,该值将保持为0。
I can't use jQuery or others libraries... I have to write this in pure AJAX ( I mean not using framework or librairies ), and I have no idea how to do this. 我不能使用jQuery或其他库...我必须用纯AJAX编写此代码( 我的意思是不使用framework或librairies ),而且我也不知道该怎么做。
Edit : I can't use cookie. 编辑 :我不能使用cookie。 I don't want to use
<noscript>
. 我不想使用
<noscript>
。
First you need to learn to do a ajax call in pure javascript, some website like w3schools or others 首先,您需要学习用纯JavaScript进行ajax调用,某些网站如w3schools或其他
And in the php files that the ajax call , you can set the session variable to 1. 在ajax调用的php文件中,您可以将会话变量设置为1。
But there's maybe better way to find if the client have javascript enable. 但是,也许有更好的方法来查找客户端是否启用了javascript。
EDIT: I suggest to check if your session variable is , if not, try the ajax call and set the variable at 0 . 编辑:我建议检查您的会话变量是否,如果不是,请尝试ajax调用并将变量设置为0。 If at page loading the variable is set and equal, that means the client doesn't have javascript/xmlhttprequest enable.
如果在页面加载时设置了变量且变量相等,则表示客户端没有启用javascript / xmlhttprequest。
And for a session, a cookie is store on the client side. 对于会话,cookie存储在客户端。 So if the client refuse to store any cookie, you will never haver information in your session variables.
因此,如果客户端拒绝存储任何cookie,您将永远不会在会话变量中拥有任何信息。
I would use a cookie. 我会使用Cookie。 It will function nearly identical --
它的功能几乎相同-
<?php
$_SESSION['js'] = (int) $_COOKIE['js']
setcookie('js', 0);
?>
<script type="text/javascript">
setCookie('js', 1);
</script>
You can use a set of cookie functions such as http://www.w3schools.com/js/js_cookies.asp 您可以使用一组Cookie函数,例如http://www.w3schools.com/js/js_cookies.asp
Good luck! 祝好运!
A rather unique (and IMO lame) approach is to use javascript to download a fake image: 一种相当独特的方法(也是IMO的me脚)是使用javascript下载伪造的图片:
<script type="text/javascript">
var fake = new Image();
fake.src = "/sess_js.php";
</script>
Doesn't use AJAX, jQuery, Cookies, etc. 不使用AJAX,jQuery,Cookie等
According to your idea and your requirements, if you can't use a library like jQuery or Prototype, and so on... you are enforced to use the "raw" XMLHttpRequest Object . 根据您的想法和要求,如果您不能使用jQuery或Prototype之类的库,依此类推...则必须使用“原始” XMLHttpRequest对象 。 The following code is an example.
以下代码是一个示例。
Create a file called ajax_js_enabled_test.php and put this code inside: 创建一个名为ajax_js_enabled_test.php的文件,并将此代码放入其中:
<?php
if(isset($_GET['PHPSESSID'])) session_id($_GET['PHPSESSID']);
session_start();
if(isset($_SESSION['user_js_support']) && $_SESSION['user_js_support']) echo "Javascript enabled";
else $_SESSION['user_js_support'] = FALSE;
?>
<script type="text/javascript">
var XHR;
if (window.XMLHttpRequest){
// The code for IE7+, Firefox, Chrome, Opera, Safari
XHR=new XMLHttpRequest();
}else{
// The code for IE6, IE5
XHR=new ActiveXObject("Microsoft.XMLHTTP");
}
XHR.open("GET","http://127.0.0.1/check_js_user_support.php<?php echo '?PHPSESSID='.session_id(); ?>",true);
XHR.send();
</script>
<p>
<a href="<?php echo '?PHPSESSID='.session_id(); ?>">
Refresh
</a>
</p>
Create another file called check_js_user_support.php and put this code inside: 创建另一个名为check_js_user_support.php的文件,并将此代码放入其中:
<?php
if(isset($_GET['PHPSESSID'])) session_id($_GET['PHPSESSID']);
session_start();
$_SESSION['user_js_support'] = TRUE;
?>
Copy both files in your local web server root. 将两个文件复制到本地Web服务器的根目录中。 Open the page ajax_js_enabled_test.php from your browser.
从浏览器打开页面ajax_js_enabled_test.php。 The AJAX request starts and the page check_js_user_support.php will set the session param called user_js_support to TRUE.
AJAX请求启动,页面check_js_user_support.php将名为user_js_support的会话参数设置为TRUE。 So by pushing the Refresh button the page will be reloaded, the $_SESSION['user_js_support'] will be TRUE and the script will write: "Javascript enabled"
因此,通过按“刷新”按钮,将重新加载页面, $ _ SESSION ['user_js_support']将为TRUE ,脚本将写为:“ Javascript enabled”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.