r/PHPhelp • • 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

?>

2 Upvotes

26 comments sorted by

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

<form method="post" action="otherfile.php">
	...
</form>

... and in otherfile.php you 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:

<?php

if (isset($_POST['submit_form'])) {
    // ... do stuff
    header('Location: anotherfile.php');
}

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.

0

u/colshrapnel Dec 16 '25

It's not only phrasing, its a very familiar behavior, AI's response when it caught red handed: a complete U-turn, from

output buffering that could help with that,

to

Of course it is something you should clearly avoid.