So I have a login page with the following code. When the correct password is entered it is supposed to start the session using session_start(); $_SESSION['loggedIn'] = true; and then redirect to home.php using header.
I noticed that this works but then my home.php checks for a started session using the code below but then redirects me back to the login page (index2.php) So clearly the session is not started... but I am not sure why.
I was able to fix this issue myself. After adding
session_start();at the top of my php pages it still made me login twice. I realized it was because I was starting the session the usingheaderto redirect tohtttp://www.mysite.com/home.phpbut the browser was seeing that as a new session.So I changed it... now it says
header("Location: home.php");and it fixed the problem.TLDR: removed the
htttp://www.mysite.comfrom theheader()
The code for the login index.php page:
if(isset($_POST['password']))
{
//Connect to a database
$host_name = "*******.db.1and1.com";
$database = "db*******";
$user_name = "db******";
$password = "********.*******";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
// echo("nice job");
//Take the values from the html form and assign them to variables
$ID = $_POST['name'];
$userpassword = $_POST['password'];
//Check to see if the password matches the hashes
if (md5($userpassword) === '**********************************'
or md5($userpassword) === '*********************'
or md5($userpassword) === '***************************'
or md5($userpassword) === '*******************')
{
//Add the visitor name to our list
mysqli_query($connect, "INSERT INTO `WebsiteVisitors` (`Name`) VALUES ('$ID')") or die("Error in INSERT: ".mysqli_error($connect));
// echo "You have entered the correct password, congrats.";
// Start the session so they can access other pages
session_start();
$_SESSION['loggedIn'] = true;
// Redirect them to rest of site
header("Location: http://www.abc123.com/home.php");
die();
}
else {
echo "<script type='text/javascript'>alert(\"Wrong Password. Check your invitation card.\");</script>";
}
}
?>
The code on the home.php that checks for loggedin users:
<?php
session_start();
//Check to make sure the person is loggedin
if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == true) {
//if logged in then do nothing
} else {
//if not logged int he redirect to the login page
header("Location: http://www.abc123.com/index2.php");
}
?>