Is it possible to set PHP session variables using Javascript?
In javascript:
In session_write.php file:
In html:
|
||||
|
The session is stored on the server side so you cannot add values to it from javascript. All that you get on the client side is the session cookie which contains an id. One possibility would be to send an AJAX request to a server side script which would set the session variable. Example with jQuery's
You should of course be cautious about exposing such script. |
|||
|
If you want to allow client-side manipulation of persistent data, then it's best to just use cookies. That's what cookies were designed for. |
|||
|
or by pure js, see also on StackOverflow : JavaScript post request like a form submit BUT WHY try to set $_session with js? any JS variable can be modified by a player with some 3rd party tools (firebug), thus any player can mod the $_session[]! And PHP cant give js any secret codes (or even [rolling] encrypted) to return, it is all visible. Jquery or AJAX can't help, it's all js in the end. This happens in online game design a lot. (Maybe a bit of Game Theory? forgive me, I have a masters and love to put theory to use :) ) Like in crimegameonline.com, I initialize a minigame puzzle with PHP, saving the initial board in $_SESSION['foo']. Then, I use php to [make html that] shows the initial puzzle start. Then, js takes over, watching buttons and modding element xy's as players make moves. I DONT want to play client-server (like WOW) and ask the server 'hey, my player want's to move to xy, what should I do?'. It's a lot of bandwidth, I don't want the server that involved. And I can just send POSTs each time the player makes an error (or dies). The player can block outgoing POSTs (and alter local JS vars to make it forget the out count) or simply modify outgoing POST data. YES, people will do this, especially if real money is involved. If the game is small, you could send post updates EACH move (button click), 1-way, with post vars of the last TWO moves. Then, the server sanity checks last and cats new in a $_SESSION['allMoves']. If the game is massive, you could just send a 'halfway' update of all preceeding moves, and see if it matches in the final update's list. Then, after a js thinks we have a win, add or mod a button to change pages:
Then the new page's PHP looks at $_SESSION['init'] and plays thru each of the $_SESSION['allMoves'] to see if it is really a winner. The server (PHP) must decide if it is really a winner, not the client (js). |
|||
|
You can't directly manipulate a session value from Javascript - they only exist on the server. You could let your Javascript get and set values in the session by using AJAX calls though. See also |
|||
|
Not possible.Becaue javascript is client side and session is server side.So, to do anything related to a PHP session,u 've to go to the server |
|||
|