There are two files, index.php and file.php. I set a session variable in file.php to 1. Then I check in index.php if this session variable is set to 1. If it is, it is supposed to echo that variable. But instead, my browser goes blank. It looks like all the html code vanishes as seen in the developer tools of the browser (no html content). If I comment out that and instead try to echo a string satisfying the if condition, then the page appears but does not echo the string.
file.php
<?php
session_start();
?>
<html>
<head>
<title>Password test, page 2</title>
<head>
<body>
.
.
.
<?php
if($name === $good_name && $pass === $good_pass){
echo "That is the correct log-in information";
}else{
$_SESSION["flag"] = 1; // setting the variable in file.php
header('Location: index.php'); exit(); //directing to that file
}
?>
</body>
</html>
index.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
.
.
.
<?php
if ($_SESSION["flag"] === 1)
{echo "invalid"; //this doesn't output the string
echo . $_SESSION["flag"]; //this gives me blank page
}
?>
Is my code syntactically wrong or if the logic is incorrect? Please point it out.
EDIT#1 file.php
<?php
session_start();
?>
<html>
<head>
<title>Password test, page 2</title>
<head>
<body>
<?php
$name = $_POST['username'];
$pass = $_POST['password'];
//read the contents of our password file.
$myFile = "pass/password.txt";
$fh = fopen($myFile, 'r');
$data = fgets($fh);
fclose($fh);
$data1 = trim($data);
//split the text into an array
$text = explode(":",$data1);
//assign the data to variables
$good_name = $text[0];
$good_pass = $text[1];
//compare the strings
if($name === $good_name && $pass === $good_pass){
echo "That is the correct log-in information";
}else{
$_SESSION["flag"] = 1;
header('Location: index.php'); exit();
}
?>
<br>
</body>
</html>