16

I'm trying to create an image uploading website. In this, when a user logs in, I set a $_SESSION['id'] variable in php. Now, I wish to check if $_SESSION['id'] is set in my javascript file (general.js) in order to carry out certain functions. How should I go about doing it?

0

4 Answers 4

28
<?php $session_value=(isset($_SESSION['id']))?$_SESSION['id']:''; ?>
    <html>
    <head>
    <script type="text/javascript">
    var myvar='<?php echo $session_value;?>';
    </script>
    <script type="text/javascript" src="general.js"></script>
    </head>
    <body>

    </body>
    </html>

In above code snippet I have initialized global variable myvar with value stored into php session variable into script file before general.js file is referenced in script.It will be accessible in general.js file

Sign up to request clarification or add additional context in comments.

8 Comments

I'm not able to access myvar in general.js
So the problem now is, if $_SESSION['id'] is set, then the code in general.js works fine...if it is not set, then my console shows the error "unterminated string literal" ..what's going wrong?
@eagleAtlantis Please find modified code snippet in above answer
Those answers are terrible since you usualy put js in separate file. A legit solution woud be to set a <input type="hidden" id="session_something" style="display:none" value="desired_value"> and to get is with $('#id) or document.getElementbyId.value
@Dice What do you mean by "usually put js in separate file"? You still can, only that one line needs to be in <script>
|
8

A simple example please try this

<?php 
session_start();
$_SESSION['id'] = 12;

?>

<script>
alert(<?php echo $_SESSION['id']; ?>);
</script>

You can even use php variable and array as variable in js like below

<?php
    $id= $_SESSION['id'];
    $data= array('one', 'two', 'three');
?>
<script type="text/javascript">
    var idr = '<?php echo $id; ?>';
    var datar = <?php echo json_encode($data); ?>;
</script>

5 Comments

If I put <?php session_start(); ?> in the beginning of my general.js, everything stops working..what should I do?
@meagler Why would you be putting that in a .js file to begin with? Both of these are .html
Left something very important piece of information. The extension of this file must be .php not .html. If this file is kept with .html extension, this will not work.
I don't want to sound rude, but the occurrence of <?php has already emphasised that it has to be a PHP file. Although, there are also ways to keep your file extension .html, and this still works. But that's totally a different topic.
@AliYousuf - Sorry. You are right. I came across html extension and php extension code with this kind of hybrid code. So I just want to emphasize the importance of the extension.
5

Here PHP is server-Side execution and JavaScript is Client side execution. and $_SESSION is a server-side construct.So probably you can not access that directly using JavaScript You would need to store that variable in $_COOKIE to be able to access it client-side.

You can get that Session using PHP, and then use that for the JavaScript like this

<?php
$variablephp = $_SESSION['category'];
?>

<script>
var variablejs = "<?php echo $variablephp; ?>" ;
alert("category = " + variablejs);
</script>

Here you are getting Session like echo $variablephp using PHP, and the assign that value to JavaScript variable.

4 Comments

I think another option is saving the session inside text and read the text using document.getElementById("text_variable_name").value;
you don't need double quotes around if the variable is a number.
@Nguaial, I have already answered that for you in answers below :)
We can access _SESSION from javascript by writing the code as said. Saying that we can not access the _SESSION variable because php is server side and javascript is client side is very misleading. Yes, javascript is clientside but if the <script></script> is included within the .php file, the <?php ?> code within the javascript script will be executed.
2

The best way is to echo the session ID in a javascript variable.

<script>
  var sessionId = "<?php echo $_SESSION['id']; ?>";

  // Your javascript code goes here
</script>

4 Comments

there is no need to put double quotes if the value is a numerical value.
You never apply calculation on sessionId.. It wouldn't matter if it's a numerical value or string. So quote caters both cases.
I am getting this: JavaScript Error Handling: Unexpected Token: <. Running this is Chrome.
It is not supposed to be run in any browser directly. It's meant to be served by PHP server, which in turn fill up the <?php ... ?> segment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.