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 :)
Tomas source share