sumit
sumit

Reputation: 11257

Checking PHP session using Javascript

I am creating a PHP session using the following code:

<?php
     session_start();
     $_SESSION['loggedin'] = true;
?>

How can I access the variable loggedin in Javascript? In other words, I want to check in Javascript if the variable 'loggedin' is set or not.

Upvotes: 0

Views: 2762

Answers (2)

Matt Ball
Matt Ball

Reputation: 359786

You can't — at least not directly.

PHP runs on the server, but JavaScript runs in the browser.

Upvotes: 1

Dan Short
Dan Short

Reputation: 9616

The only way to do this is to write the value into a script block on the page as a JS variable, and reference it that way, or to make an AJAX request to a page that then returns the logged in status. There's no way for your client to directly access the session state in any server language.

Upvotes: 5

Related Questions