question for $_GET and $_SESSION

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
athlhaz
Forum Newbie
Posts: 9
Joined: Fri Dec 23, 2011 12:00 am

question for $_GET and $_SESSION

Post by athlhaz »

can anyone know how to use the value $_GET and $_SESSION in javascript?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: question for $_GET and $_SESSION

Post by twinedev »

$_SESSION values are stored on the server, so javascript cannot directly read/write to them.

$_GET, a quick search found some sample code on this page to show how to extract values from the query string (which is what $_GET is) http://www.bloggingdeveloper.com/post/J ... cript.aspx

Now, if you mean you just want to use the actual values, you can in the middle of your code echo them out, example:

Code: Select all

<?php

    // CODE TO SET UP VARIABLES/DATA FOR USE ON THE PAGE AND ALSO TO VALIDATE/CLENSE $_SESSION/$_GET values

?><html>
<head>
    <title>Test Page</title>
    <script> 
        var sUserID = <?php echo (int)$_SESSION['userid']; ?>;
        var gSearch = "<?php echo addslashes($_GET['search']); ?>";
        
        // Now the rest of your JS that uses the variables above
    </script>
</head>
...
A couple of notes, remember, once it is output as part of Javascript, not only is it visible to the user, it is also CHANGEABLE by the user, so always re validate anything coming back into your site.

Also, if you are needed to write back to re-save a new value in $_SESSION, the JS will basically need to do an AJAX call back to a PHP script on your server which will then save the value to $_SESSION. (or be done via a form submission)

-Greg
Post Reply