Login Form
<form method="POST" name="regform" action="loginSession.php">
username:<input type="text" name="username">
password:<input type="text" name="password">
<input type="submit">
</form>
Login Session Page
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
include("config.php");
$username=$_POST["username"];
$password=$_POST["password"];
$sql = "SELECT * FROM regform WHERE username = '$username' And password='$password'";
$result = $conn->query($sql);
$_SESSION['username'] = $username;
if(isset($_SESSION['username'])){
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//$_SESSION['valid'] = true;
//$_SESSION['timeout'] = time();
$_SESSION['username'] = $username;
echo "<br> username: ". $row["username"]." - Mobile: ". $row["mobile"]."<br>";
}
} else {
echo "0 results";
}
}
else{
echo "Please login to continue";
}
?>
<br>
<a href = "logoutcopy.php" title = "Logout"> Click here to Logout or Clean Session.</a>
<br>
<a href = "welcome.php" title = "Welcome">Go to Welcome Page</a><br>
<a href = "loginFormSession.php" title="login">Log in </a>
</body>
</html>
Welcome Page
<?php
session_start();
if(!isset($_SESSION['username']))
{
// not logged in
header('Location: loginFormSession.php');
exit();
}
else {echo "Welcome ". $_SESSION['username'];}
?>
<!DOCTYPE html>
<html>
<body>
<?php
include("config.php");
$username=$_SESSION['username'];
$sql = "SELECT * FROM regform WHERE username = '$username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> username: ". $row["username"]." - Mobile: ". $row["mobile"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<br>
<a href = "logoutcopy.php" tite = "Logout"> Click here to Logout or Clean Session.</a>
<br>
<a href = "welcome.php" tite = "Logout">Go to Welcome Page</a>
</body>
</html>
Log out Page
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
echo 'You have cleaned session';
header('Refresh: 2; URL = loginFormSession.php');
?>