r/PHPhelp • u/recluzeMe • Dec 16 '25
Solved header() function in php
<?php
if(isset($_POST["submitted"]))
{
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$passd = $_POST["passd"];
$confirmPassword = $_POST["Cpassd"];
$conn = new PDO("mysql:hostname=localhost;dbname=signlogin;","root","");
$sqlQuery = "INSERT INTO signup(firstname,lastname,email,PASSWORD,confirmPassword) values('$firstname','$lastname','$email','$passd','$confirmPassword')";
$stmt = $conn->prepare($sqlQuery);
$stmt->execute();
header('Location: http://localhost/phpForm/login.php');
exit();
}
page doesn't redirect to login page hence file login.php is in same folder
http://localhost/login.php
instead of:
http://localhost/phpForm/login.php
?>
View all comments
Show parent comment
0
u/StevenOBird Dec 16 '25
Of course everything phrased in a certain way is AI... not everyone uses ChatGPT for everything, mate. That includes my answers here.
If you don't put an action to a form element in HTML, it will make the request to the same url the client is in. You cannot redirect a POST request with a header instruction and that's not something I've ever suggested in that way. But you can clearly change the target path for the POST request with the action like that:
index.php... and in
otherfile.phpyou can indeed redirect the client to yet another page. Using that post-redirect-get pattern you also remove the danger of forms being re-submitted:Nothing wrong here, aside from some misunderstanding that could appear.
Like I've mentioned, there is not enough context to be 100% sure. It could also be a massive single file that includes forms for registration and login - we just don't know at this point.