Skip to main content

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.





Here is my idea : At every loading page, PHP initialize $_SESSION['is_js'] to 0 . Then, I wish AJAX to ( via launching a script php ? ) try to set $_SESSION['is_js'] to 1 . If JS is enabled, AJAX will succeed, if not, the value remains 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.





Edit : I can't use cookie. I don't want to use <noscript> .


Comments

  1. I would use a 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

    Good luck!

    ReplyDelete
  2. A rather unique (and IMO lame) approach is to use javascript to download a fake image:

    <script type="text/javascript">
    var fake = new Image();
    fake.src = "/sess_js.php";
    </script>


    Doesn't use AJAX, jQuery, Cookies, etc.

    ReplyDelete
  3. First you need to learn to do a ajax call in pure javascript, some website like w3schools or others

    And in the php files that the ajax call , you can set the session variable to 1.

    But there's maybe better way to find if the client have javascript enable.

    EDIT: I suggest to check if your session variable is , if not, try the ajax call and set the variable at 0 . If at page loading the variable is set and equal, that means the client doesn't have javascript/xmlhttprequest enable.

    And for a session, a cookie is store on the client side. So if the client refuse to store any cookie, you will never haver information in your session variables.

    ReplyDelete
  4. 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. The following code is an example.

    Create a file called ajax_js_enabled_test.php and put this code inside:

    <?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:

    <?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. Open the page ajax_js_enabled_test.php from your browser. The AJAX request starts and the page check_js_user_support.php will set the session param called user_js_support to 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"

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex