1

Is there any way to read Session value through JQuery?

Edited:

I am calling a .php file using JQuery. The .php file stores some column values in a session.

What would be the right approach to return those column values to the calling JQuery function?

1
  • What kind of session? If it's part of the URL you can, otherwise you can't Commented Dec 23, 2009 at 12:45

6 Answers 6

3

In PHP, something like (from memory as I've not PHP'd for some time...)

<input type="hidden" id="SessionValue" value="<?php echo $MySessionValue ?>">

Then in jQuery

$("#SessionValue").val();
Sign up to request clarification or add additional context in comments.

Comments

1

Sure you can set up a service which serves the Session values in JSON, and then use $.getJSON. But to read it directly is impossible.

Comments

0

Session values are stored on the server, JQuery is a client-side library that runs in the browser. Unless you send the session value down to the client, JQuery won't be able to read it.

Comments

0

I assume that you're referring to a server-side session in ASP.Net or PHP.

Not directly.

However, you could make an AJAX call to server-side code that returns something from the session.

If you do, beware of information disclosure.

Comments

0

Session values can only be read on server-side. Still if you are really hard-bent on what you want to do, you can write a Ajax enabled web-method in your code-behind that responds with session value for the given key as argument. You can call this webmethod from JQuery and retrieve the session value!

Comments

0

The ways sessions are associated with a client is by using cookies. That's where maybe the confusion takes place.

But the session data themselves are stored on the server.

Your backend need to send your session values somehow to your page for jQuery to pick it up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.