I set up a form with username and password, and was told that I could use the header function to direct them to another page on successful login. However, from my understanding the header function has to come before any HTML tags so I cannot put it into the form. I've copied the code I've used so far below, and any advice on how to link properly would be appreciated, thanks!: <?php $username = "merlin"; $password = "password"; if (isset($_POST["username"])) { if ($_POST["username"] == $username && $_POST["password"] == $password) { header("location: home.php"); } else { echo ("Incorrect username or password"); } } ?>
This vode must be placed at hte beginning of the php file which is targeted by the form action, i.e. form action="login.php", this code must be at the beginning of the login.php file
<?php $username = "merlin"; $password = "password"; if (isset($_POST["username"])) { if ($_POST["username"] == $username && $_POST["password"] == $password) { die(header("location: home.php")); // Die after sending header. } else { echo ("Incorrect username or password"); } } ?> That should work... PHP:
Hmm, tried but it just gave me an error: "Warning: Cannot modify header information - headers already sent by (output started at D:\Applications\xampp\htdocs\test\login_form.php:21) in D:\Applications\xampp\htdocs\test\login_form.php on line 53" Guess I will have to ask the class tutor this one!
ah solved it in a moment of inspiration - i think i understand now what u meant cbn81; i pasted the entire script before my html tags and it worked - thanks!