My intention here is to create a page which initially displays a basic form consisting of a text field where a password is entered, and a submit button. Upon submitting, the form will send the value back to the same page, where it is checked. This happens in the PHP file, not in a database. If the password is correct, the relevant HTML will be shown. This is what I have:
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
if(isset($_POST["logout"])){
session_destroy();
}elseif(isset($_POST["auth"])){
if($_POST["auth"]=="password"){
session_start();
$_SESSION["admin"]="true";
}
}
if(!isset($_SESSION["admin"])){
?>
<form action="/admin.php" method="post">
<input type="text" name="auth"><input type="submit">
</form>
<?php
}elseif($_SESSION["admin"]="true"){
?>
<section>
<!--HTML goes here-->
</section>
<section>
<form action="/admin.php" method="post">
<input type="hidden" name="logout" value="logout">
<input type="submit">
</form>
</section>
<?php }else{echo "Incorrect password";}?>
Now this partially works. Entering the correct password will show the HTML that the person should see. What doesn't happen is when an incorrect password is entered, the echo on the final line will not be shown. It just goes back to the form, as if opening the page for the first time.
Also, after successfully entering the password, I get a couple of warnings thrown up:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at D:\xampp\htdocs\top.php:28) in D:\xampp\htdocs\admin.php on line 13
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at D:\xampp\htdocs\top.php:28) in D:\xampp\htdocs\admin.php on line 13
...where line 13 is simply session_start();
While I have read that there are security issues with storing a password in the PHP file, it didn't seem like a major concern since no more than two people will be using this page containing tools for the manipulation of things that aren't a major issue if messed with, or deleted. However, if this is a much bigger security issue than I think it is, please let me know.