Get Session value through JQuery

Posted on 2009-12-23
Last Modified: 2012-05-08
Is there any way to get Session value through JQuery?
Question by:rpkhare
    LVL 2

    Accepted Solution

    Yes there is, but as the browser doesnt have the Session values (i guess you mean PHP $_SESSION) you will need to do an Ajax call  using jQuery to a php script,

    What i recommend you is to use JSON format to exchange info between js and the server, so you will need to do the following,

    JS side
                alert(data);  //do whatever you want with it...

    Server Side - script http://yourserver/sessionVarGet.php

    $varname = $_GET['name'];
    $out = $_SESSION['$varname];

    echo json_encode($out);

    And thats it !!

    LVL 8

    Author Comment

    I've never used JSON. Do I need to include any .js or other library file to make it work?
    LVL 2

    Expert Comment

    No, you don't need any library, JSON is a standard javascript object notation

    LVL 8

    Author Comment

    @nergik: You mean, instead of JQuery, I need to use JSON? The code that you illustrated above will do?
    LVL 8

    Author Closing Comment

    Thanks. Excellent solution. It worked seemlessly.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
    Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now