最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Mobile browsers doesn't support session variables? - Stack Overflow

programmeradmin6浏览0评论

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?

I am trying to perform this code and my php is giving me an empty value:

$(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?

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.

Javascript file:

$(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:

And the last thing is 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.

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?

I am trying to perform this code and my php is giving me an empty value:

$(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?

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.

Javascript file:

$(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:

And the last thing is 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.

Share Improve this question edited Aug 23, 2012 at 14:14 Markus Hedlund 24.3k22 gold badges81 silver badges112 bronze badges asked Aug 23, 2012 at 8:35 user123_456user123_456 5,81526 gold badges87 silver badges142 bronze badges 4
  • Take a look at HTML 5 Web Storage – james lewis Commented Aug 23, 2012 at 8:38
  • so instead of saving into session variables I am saving into web storage? – user123_456 Commented Aug 23, 2012 at 11:33
  • Session ID can be passed via cookies or via GET method, and from what I know - browsers at mobile devices do support cookies. Are you sure your code actually works? Have you even started the session? – N.B. Commented Aug 23, 2012 at 12:00
  • Bad idea since a lot of mobile devices don't have such capability. – Florent Commented Aug 23, 2012 at 12:05
Add a ment  | 

3 Answers 3

Reset to default 6

Cookies definitely work on mobile browsers. You say "I am looking on the firebug" which makes me think you aren't really looking at it on a phone.

Have you started the session, and turned on error reporting?

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.

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).

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.

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.

发布评论

评论列表(0)

  1. 暂无评论