javascript 移动浏览器不支持会话变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12087759/
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
Mobile browsers doesn't support session variables?
提问by user123_456
I have a browser version of my web app and I have some values stored in cookies. Now I am making a mobile version and it seems that my cookies are not working. Is it possible to use cookies on mobile devices?
我有我的网络应用程序的浏览器版本,并且我在 cookie 中存储了一些值。现在我正在制作移动版本,但我的 cookie 似乎不起作用。是否可以在移动设备上使用 cookie?
I am trying to perform this code and my php is giving me an empty value:
我正在尝试执行此代码,而我的 php 给了我一个空值:
$(document).ready(function() {
var session_lang= '<?php if(isset($_SESSION['SESS_LANGUAGE']))
echo $_SESSION['SESS_LANGUAGE'];?>';
if (session_lang!='')
{
check_and_change(session_lang);
}
});
Is there any other way to store data in internal memory so it can work as cookie? Does mobile devices support session variables? I am looking on the firebug and I can see that I can create session variable but when I want to read it it is null. What could be a problem and how to solve this?
有没有其他方法可以将数据存储在内部存储器中,以便它可以用作 cookie?移动设备是否支持会话变量?我正在查看萤火虫,我可以看到我可以创建会话变量,但是当我想读取它时它为空。什么可能是问题以及如何解决这个问题?
EDIT: I will put you almost entire code so you can see what am I doing...btw. this code is working normally on PC browser.
编辑:我会给你几乎整个代码,所以你可以看到我在做什么......顺便说一句。此代码在 PC 浏览器上正常工作。
Javascript file:
Javascript文件:
$(document).ready(function() {
function languageChange()
{
var lang = $('#websites1 option:selected').val();
return lang;
}
$('#websites1').change(function(e) {
var lang = languageChange();
var dataString = 'lang=' + lang;
$.ajax({
type: "POST",
url: "pass_value.php",
data: dataString,
dataType: 'json',
cache: false,
success: function(response) {
check_and_change(response.message);
}
});
return false;
});
} );
function check_and_change(lang)
{
switch (lang)
{ //do something
}
}
Then this first part that I have put above is on the actual php site that I am looking and which is opening:
然后,我在上面放置的第一部分是在我正在查看的实际 php 站点上,该站点正在打开:
And the last thing is pass_value.php:
最后一件事是 pass_value.php:
<?php
session_start();
$_SESSION['SESS_LANGUAGE'] = $_POST['lang'];
print json_encode(array('message' => $_SESSION['SESS_LANGUAGE']));
die();
?>
I really don't know why this code doesn't work on the mobile phones.
我真的不知道为什么这段代码在手机上不起作用。
回答by Znarkus
Cookies definitelyworkon mobile browsers. You say "I am looking on the firebug" which makes me think you aren't really looking at it on a phone.
Cookie肯定适用于移动浏览器。你说“我在看萤火虫”这让我觉得你不是真的在手机上看它。
Have you started the session, and turned on error reporting?
回答by Stephen Behnfeldt
I had a similar situation and discovered that, before my tablet called the script I specified in the URL parameter of the .ajax( )
call, it was actually calling the index.php
script again.
我也遇到过类似的情况,发现在我的平板调用我在.ajax( )
调用的URL参数中指定的脚本之前,它实际上是在index.php
再次调用该脚本。
Same with my phone. Since I had initialized my session variables in index.php
, they kept getting re-set to their initial values (making it look like they were not being correctly stored between pages).
和我的手机一样。由于我已经在 中初始化了我的会话变量index.php
,它们不断地被重新设置为它们的初始值(看起来它们没有被正确地存储在页面之间)。
I never figured out why index.php
was getting re-called, but I added if !isset( )
checks around the initialization statements and that cleared it up.
我从来不知道为什么会index.php
被重新调用,但我if !isset( )
在初始化语句周围添加了检查并清除了它。
回答by Itlan
On Android, turning on "Data Saver" effectively destroyed my session. I have a login script which will redirect to the previous page when login succeeds. Worked until I turned on Data Saver. Turning it off made it work again. Google does state that Data Saver may affect secure pages which I guess includes my script, which is not on a HTTPS website.
在 Android 上,打开“数据保护程序”有效地破坏了我的会话。我有一个登录脚本,它会在登录成功时重定向到上一页。一直工作到我打开数据保护程序。关闭它使它再次工作。谷歌确实声明数据保护程序可能会影响安全页面,我猜其中包括我的脚本,它不在 HTTPS 网站上。