Question
Set Session variable using javascript in PHP
Is it possible to set PHP session variables using Javascript?
45 150766
45
Question
Is it possible to set PHP session variables using Javascript?
Solution
In JavaScript:
jQuery('#div_session_write').load('session_write.php?session_name=new_value');
In session_write.php file:
<?
session_start();
if (isset($_GET['session_name'])) {$_SESSION['session_name'] = $_GET['session_name'];}
?>
In HTML:
<div id='div_session_write'> </div>
Solution
The session is stored server-side so you cannot add values to it from JavaScript. All that you get 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 .post()
method:
$.post('/setsessionvariable.php', { name: 'value' });
You should, of course, be cautious about exposing such script.