最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to connect between the sessionStorage in PHP to JS - Stack Overflow

programmeradmin1浏览0评论

I am trying to transfer data in my PHP (html) page, befor the HTML is loaded, so I will be able to deal with it after the page is loaded in my JS file. my php look like this:

<?php
/*some php code*/
$dataFromDataBase = "..";
session_start();
$_SESSION['data'] = $dataFromDataBase;

if(isset($_SESSION['data']))
    echo $_SESSION['data'];
else
    echo "<br>False";       
?>
<html>
<!--My Html code..-->
</html>

the "if" condition is there now to check if the session was created in the php. now it echo me the data, so it's meen the session exist. my JS look like that:

if(sessionStorage.getItem("data")){
//do some thing..
}else{
alert("the data isn't set.");
}

and the data isn't exist... I've tride to alert the data, and it send me "null".. they offered me to output into JS directly like that:

<script>
var fromserver="<?php
/*some code.. */
echo "**BIG DATA**";
?>";

but it is just untidy code.. and the data is pretty big so I can't use coockies

I am trying to transfer data in my PHP (html) page, befor the HTML is loaded, so I will be able to deal with it after the page is loaded in my JS file. my php look like this:

<?php
/*some php code*/
$dataFromDataBase = "..";
session_start();
$_SESSION['data'] = $dataFromDataBase;

if(isset($_SESSION['data']))
    echo $_SESSION['data'];
else
    echo "<br>False";       
?>
<html>
<!--My Html code..-->
</html>

the "if" condition is there now to check if the session was created in the php. now it echo me the data, so it's meen the session exist. my JS look like that:

if(sessionStorage.getItem("data")){
//do some thing..
}else{
alert("the data isn't set.");
}

and the data isn't exist... I've tride to alert the data, and it send me "null".. they offered me to output into JS directly like that:

<script>
var fromserver="<?php
/*some code.. */
echo "**BIG DATA**";
?>";

but it is just untidy code.. and the data is pretty big so I can't use coockies

Share Improve this question asked Jul 20, 2017 at 9:53 Yehonathan JacobYehonathan Jacob 631 gold badge1 silver badge5 bronze badges 6
  • by the way I'm useing Jquery in the same JS file if it make any change.. – Yehonathan Jacob Commented Jul 20, 2017 at 9:54
  • 2 $_SESSION is very different from sessionStorage, despite the similar name they are not the same. – Maerlyn Commented Jul 20, 2017 at 9:56
  • $_SESSION exists on the server while sessionStorage is on the client. They are very separate and cannot communicate directly. To get something from the $_SESSION in to your JS code you would be best to make an AJAX request to retrieve it – Rory McCrossan Commented Jul 20, 2017 at 9:59
  • Yes, or use PHP to echo out $_SESSION variables on your webpage? You could echo "<script> var user = ". $_SESSION['user'] ." ;</script> – Laurens Mäkel Commented Jul 20, 2017 at 10:05
  • why not use ajax to pass the vars back and forth? – treyBake Commented Jul 20, 2017 at 11:08
 |  Show 1 more comment

3 Answers 3

Reset to default 8

$_SESSION and sessionStorage are completely different.

sessionStorage saves data like $_SESSION except a sessionStorage variable is set on the client's side. $_SESSION is like sessionStorage so I see your misunderstanding, however, this sets data on the server-side and does not cross over to sessionStorage.

If you really need that $_SESSION data in sessionStorage, you can echo a script which sets the sessionStorage to your $_SESSION data. Here's a very simple example below.

<?php session_start;

$_SESSION['data'] = "yourdata";

echo '<script> sessionStorage.setItem("data", "' . $_SESSION['data'] . '");</script>';

//Display 'data'.
echo '<script> alert(sessionStorage.getItem("data")); </script>';

EDIT: To make it more clearer, notice how $_SESSION is done in PHP (server-side) and sessionStorage has to be set in JavaScript with the <script> tags. Below is a solution to your problem.

<?php
/*some php code*/
$dataFromDataBase = "..";
session_start();
$_SESSION['data'] = $dataFromDataBase;

if(isset($_SESSION['data']))
{
   echo '<script> sessionStorage.setItem("data", "' .$_SESSION['data'] .'");</script>';
}
else
{
    echo "<br>False";   
}    
?>
<html>
<!--My Html code..-->
</html>

Creating executable JS code from user provided data is a major security issue, so you should avoid the statements like var someJsVar = '<?= $somePhpVar;?>;'. Injecting malicious code could be a simple as '; malicious-code-here //

https://www.owasp.org/index.php/Cross-site_Scripting_(XSS) https://www.owasp.org/index.php/XSS_Attacks

It would be more secure to have your page make an API call on page load, which would return your data for sessionStorage encoded as JSON. This way the browser will see a variable containing the user data, and not the raw data itself.

#Initial page
<body>
    <script>
         $.GET("/session-storage.php").success(function(response){
             sessionStorage.setItem("data", response);
         });
    </script>
</body>


#API Call (session-storage.php)
<?php
    $dataFromDataBase = "..";

    echo json_encode($dataFromDataBase);
?>

you can take data from php variable and use it in JS.. like this

var sessionData = <?php echo $_SESSION['data'];?>
if (typeof sessionData != "undefined" && sessionData == null){
//do some thing..
}else{
alert("the data isn't set.");
}
发布评论

评论列表(0)

  1. 暂无评论