Reputation: 23
I have a problem that the session expires between the pages(in third page) or when I refresh , that's the first page
<?php
session_start();
if(empty($_POST['username']) || empty($_POST['password']))
{
$this->HandleError("Missing username or password");
return false;
}
$username = stripslashes($_POST['username']);
$password = stripslashes($_POST['password']);
$con = mysqli_connect('127.0.0.1' , 'root' , '' , 'Mini');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = " SELECT * FROM User WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($con , $query);
$count = mysqli_num_rows($result);
if($count == 1)
{
$_SESSION["username"] = $username;
$_SESSION["login"] = 1;
header("location:HomePage.php");
} else {
session_destroy();
header("location:Welcome.php");
echo "Wrong username or password";
}
mysqli_close($con);
?>
second one :
<?php
session_start();
if (!(isset($_SESSION["login"])))
{
header ("Location: Welcome.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="Welcome.css"/>
<title>eShop Hompage</title>
</head>
<body>
<header>
<div id="right-corner">
<img id="logo" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQWgR8v763Veku3oLPsw7mqU7bKpSVu2nSXfMPZA8RrxEiDoRuF_Q">
<p id="p1">eShop</p>
<p id="p2">your virtual shop</p>
</div>
<div id="welcome">
<h3>Welcome <?php echo $_SESSION["username"]; ?> </h3>
</div>
<a href="Edit.php">Edit info</a>
<a href="Welcome.php">Logout! <?php session_destroy(); ?></a>
</header>
<hr style="margin-top:45px;">
<div class="items">
<?php
$con = mysqli_connect('127.0.0.1' , 'root' , '' , 'Mini');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM Item";
$result = mysqli_query($con , $query);
while ($row = mysqli_fetch_array($result))
{
echo "<img src=".$row['pic']."/>";
echo "<p>".$row['name']."</p>";
echo "<p>".$row['price']."$"."</p>";
if($row['quantity'] > 0)
{
$id = $row['id'];
$link = "Buy.php?item=".$id;
echo "<a href=".$link.">Buy</a>";
} else {
echo "Sold out!";
}
echo "<br>";
echo "<br>";
echo "<br>";
}
?>
</div>
</body>
</html>
The problem comes when I go to a third page , it acts if I'm logged out and session expired :
<?php
session_start();
if (!(isset($_SESSION["login"])))
{
header ("Location: Welcome.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="Welcome.css"/>
<title>eShop Hompage</title>
</head>
<body>
<header>
<div id="right-corner">
<img id="logo" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQWgR8v763Veku3oLPsw7mqU7bKpSVu2nSXfMPZA8RrxEiDoRuF_Q">
<p id="p1">eShop</p>
<p id="p2">your virtual shop</p>
</div>
<div id="welcome">
<h3>Welcome <?php echo $_SESSION["username"]; ?> </h3>
</div>
<a href="Edit.php">Edit info</a>
<a href="Welcome.php">Logout! <?php session_destroy(); ?></a>
</header>
<h5>Are you sure you want to buy?</h5>
<?php
$con = mysqli_connect('127.0.0.1' , 'root' , '' , 'Mini');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['item'];
$query = "SELECT * FROM Item WHERE id = '$id'";
$result = mysqli_query($con , $query);
$row = mysqli_fetch_array($result);
$name = $row['name'];
echo "<p>".$name."</p>";
echo "<img src=".$row['pic']."/>";
?>
<a href="HomePage.php">Yes
<?php
$con = mysqli_connect('127.0.0.1' , 'root' , '' , 'Mini');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['item'];
$quantity = $_GET['quantity'];
$query = "UPDATE Item SET quantity = quantity - 1 WHERE id = '$id'";
mysqli_query($con , $query);
?>
</a>
<a href="HomePage.php">Cancel</a>
</body>
</html>
Upvotes: 1
Views: 97
Reputation: 2707
The issue is in your code @ second page:
<a href="Welcome.php">Logout! <?php session_destroy(); ?></a>
You are destroying the session, so by the time the third run goes in the session doesn't exist.
I think what you want is an actual fourth page (call it logout.php) which will contain the session_destory()
part and update your second and third pages logout to:
<a href="logout.php">Logout!</a>
Upvotes: 2