Facing some problem while accessing session variable in another web page
i have tried using jquery to refresh the page which create the session(data.php) also the database connection and sql query works fine, tested them independently.
test.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
</head>
<body>
<div id="show"> </div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function () {
$('#show').load('data.php') // reloades data.php repetedly
}, 1000);
});
</script>
<?php echo $_SESSION["id"]; ?>
</body>
</html>
data.php
<?php
session_start();
$conn = new mysqli("localhost","id6207501_datausername","123456789","id6207501_dataname");
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$result = $conn->query("SELECT status,id FROM logs order by id desc limit 1");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$_SESSION["id"]=$row['id'];
$_SESSION["status"]=$row['status'];
}
}
?>
i expect the test.php to print the value of session variable $_SESSION["id"]
, which it does not.
You have to call AJAX get method and set return your text in data.php
file.
test.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
setInterval(function () {
$.get("data.php", function(data, status){
$('#show').html(data);
});
}, 1000);
</script>
</head>
<body>
<div id="show"> <?php echo $_SESSION["id"]; ?> </div>
</body>
</html>
data.php
<?php
session_start();
// You can uncomment it and test
// $conn = new mysqli("localhost","id6207501_datausername","123456789","id6207501_dataname");
// if ($conn->connect_error) {
// die("Connection error: " . $conn->connect_error);
// }
// $result = $conn->query("SELECT status,id FROM logs order by id desc limit 1");
// if ($result->num_rows > 0) {
// while ($row = $result->fetch_assoc()) {
// echo $_SESSION["id"]=$row['id'];
// echo $_SESSION["status"]=$row['status'];
// }
// }
// Comment when you uncomment DB
// FOR example purpose
$_SESSION["id"]= rand(1,2000);
echo $_SESSION["id"];
?>
Firstly, check if session working on any page or not then
Add this line at the top
<?php
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));
session_start();
This works with both redirecting from PHP by using header('Location: ')
and with JavaScript window.location
.
You can also use localstorage. Set your variable value in the first page.
<script>localstroage.setItem("myItem","value");</script>
And get the value in the other page by using getItem
<script>var x = localstroage.getItem("myItem");</script>
Or cockies
<script type="text/javascript">document.cookie = "cookieName=cookieValue";</script>
And in php page
<?php $phpVar = $_COOKIE['cookieName'];echo $phpVar ;?>