Set session variables in PHP using javascript
P粉191323236
P粉191323236 2024-03-27 15:52:11
0
2
431

Is it possible to set PHP session variables using Javascript?

P粉191323236
P粉191323236

reply all(2)
P粉276876663

Sessions are stored server-side, so you cannot add values ​​to them from JavaScript. All you get on the client side is the session cookie containing the id. One possibility is to send an AJAX request to a server-side script, which will set session variables. jQuery’s .post() method example:

$.post('/setsessionvariable.php', { name: 'value' });

Of course, you should be careful about exposing such scripts.

P粉811329034

In JavaScript:

jQuery('#div_session_write').load('session_write.php?session_name=new_value');

In the session_write.php file:

session_start();

if (isset($_GET['session_name'])) {$_SESSION['session_name'] = $_GET['session_name'];}
?>

In HTML:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!