I made a simple login application that gets the user's email and password. No matter what their credentials are, the application is just echoing the credentials in the homepage. Now, I've encountered a problem regarding the session when it comes to mobile. This is the code from the login page:
<?php
$username = 'rb';
$password = 'password';
if(isset($_POST['enter'])){
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
header('Location: home.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="#" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="enter" />
</form>
</body>
</html>
And this is the home page:
<?php
echo 'Home';
session_start();
if(isset($_SESSION['username'])){
echo $_SESSION['username'];
echo $_SESSION['password'];
}else{
echo 'Error';
}
if(isset($_POST['logout'])){
session_destroy();
header('Location: login.php');
die();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="#" method="POST">
<input type="submit" name="logout" value="Logout"/>
</form>
</body>
</html>
The problem is that session variables don't persist when redirected to the homepage. In desktop, it works fine but in mobile, it shows Undefined index error which means the session variable doesn't exist. What might be the problem? As I've tested, this code works and session persists:
<?php
echo 'Home';
session_start();
if(isset($_SESSION['username'])){
echo $_SESSION['username'];
echo $_SESSION['password'];
}else{
$_SESSION['username'] = 'This is the username';
$_SESSION['password'] = 'This is the password';
echo $_SESSION['username'];
echo $_SESSION['password'];
echo 'Error';
}
if(isset($_POST['logout'])){
session_destroy();
header('Location: login.php');
die();
}
?>
Edit: I've tried session_start() at the beginning of each php file and it also didn't work.
Edit 2: I've already solved it. The problem is that my connection is too secure that my session id is changing on mobile every instance of a webpage. I managed to get a unique and static session id for a certain period of time. Thanks to those people who participated to answer.