Access session session variables from JavaScript

Is it possible to access session variables made from JavaScript, and how can I replicate this PHP code in JavaScript?

if(empty($_SESSION['name'] { echo 'foo'; } 
+4
source share
7 answers

You can access the page through Ajax, which will return the value of the variable:

Using jQuery:

 $.get('myVariable.php', function ( data ) { alert(data) }); 

And the PHP page (myVariable.php):

 <?php echo $_SESSION['user_id']; ?> 
+4
source

Sorry, but you cannot :(

First, a little background:

Session support in PHP is to save certain data for later calls. This allows you to create more customizable applications and increase the attractiveness of your website.

The visitor accessing your website is given a unique identifier, the so-called session identifier. This is either stored in a cookie on the user side or distributed in a URL.

Session support allows you to register an arbitrary number of variables that must be stored in different requests. When a visitor accesses your site, PHP will automatically check (if session.auto_start is set to 1) or your request (explicitly through session_start () or implicitly through session_register ()) to see if a specific session identifier has been sent with the request. If so, the previous saved environment is recreated. From http://www.php.net/manual/en/intro.session.php

Most sessions include storing the server side of the data and storing the cookie-side identifier on the client. This means that JS can theoretically access the cookie, but then you will have access to the PHPSESSID (session identifier) ​​variable.

To achieve what you are trying to do, you can use ajax. (Contrary to a common misconception) ajax requests still provide the server with all the information it needs to access cookies and sessions.

So just make the server side of the script as getName.php , which contains:

 if(!empty($_SESSION['name'])) { echo $_SESSION['name']; } 

And use ajax to get the answer :)

+3
source

If you configure javascript for analysis in php, you can access session vars the same way you can in php:

 // Test.php <?php session_start(); ?> <?php $_SESSION['foo'] = 'bar' ?> <script type="text/javascript"> alert('<?php echo $_SESSION['foo'] ?>'); </scrip> <?php var_dump($_SESSION['foo']) ?> 
+3
source

ok ... ajax is good and that’s it, but if you really want to, you can just type them as variables in your tag in your head and access them via js this way. I never did this with session variables, but I always printed values ​​from php (mysql) all the time

+1
source

is it possible to access session variables made in php from javascript

No. They are in the memory of your web server, which, fortunately, javascript does not have access to.

0
source

Session variables are stored on the server. The only way to access them using javascript (client side) is to make a server request from AJAX to the php page, which will then return you the value.

0
source

No, you can’t. Session variables are stored on the server, not on the client. The closest you can do is check the session variable in php script and call it with ajax.

0
source

Source: https://habr.com/ru/post/1381368/


All Articles