7

Is it possible to create and update a session variable in JavaScript? Just like in PHP as I start sessions and initialize session variable.

I'm using some JavaScript function with PHP.

I'm just a beginner in JavaScript so please also refer me some good books.

1
  • Next time, please... don't use... that... many... ellipses and exclamation marks. Moreover, ask one question at a time. For books, please search yourself. Commented Apr 28, 2011 at 13:00

5 Answers 5

7

The usual meaning of the term "Session variable" is: "Some data stored on the server, which is associated with a user's session via a token".

Assuming you are talking about client side JavaScript, then the short answer is "no" as it runs on the wrong computer.

You could use JS to make an HTTP request to the server, and have a server side programme store data in a session variable.

You could also use JS to store data in a cookie (and not set an expiry time so it expires at the end of the browser session)

Sign up to request clarification or add additional context in comments.

Comments

5

Well, taking a look at how sessions work, no, they cannot be created with javascript. You can, though, make an AJAX request to your PHP file to set one.

PHP:

<?php
session_start(); 
$_SESSION['mySession'] = 1;
?>

JS:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "session_maker.php", true);

xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
        alert("Done! Session created.");
    }
};

Comments

3

There are a number of ways of recording state in Javascript - none of which are particularly great / cross-browser, but there you go.

You can set and get cookies - http://www.quirksmode.org/js/cookies.html

Window.name storage (sessionvars) - http://www.thomasfrank.se/sessionvars.html - this is a very cool hack, but probably not a great idea to use for anything important

HTML5 local storage - http://diveintohtml5.ep.io/storage.html

Server-side is probably the way to go here, but client-side you have a few options.

Comments

1

No, session state is server-side. You can, however, create cookies that PHP can read.

Comments

0

You could use the js via the ajax way from/to load/post to a php script and write your session control method into this php script file.

You could try the jQuery's $.ajax() method, I guess it is the easy way to made it on the front-end script.

5 Comments

“use the js via the ajax way to load/post a php script and write your session control method into this php script file.” → Wow, that would be very dangerous, as some malicious user might inject some arbitrary PHP code in your server.
um... how about add "role check" method for session control php script? role check and did the xss clean for the post var , I guess it should be ok :)
Oh, you meant one should only post a session hash key, not actual PHP code? Your answer looked like that.
Of course can't be "php code" , I am sorry for my express, English is not my native language. :p
Oh, yes, it can be! Just use eval! :-P

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.