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
?>
8
u/ColonelMustang90 Dec 16 '25
put a die("before header") function just before the header() function to check whether this text is sent to the browser or the code is unreachable.
As suggested by another user, sql code needs to updated.
12
u/HolyGonzo Dec 16 '25
I don't understand exactly what you're asking but I'll say that you really need to fix your database code. It is vulnerable to SQL injection.
Aside from that, are you saying that the header() line is being hit but the user is not being redirected?
6
u/colshrapnel Dec 16 '25 edited Dec 16 '25
Quite possible that redirect actually works, but phpForm/login.php contains its own header function that redirects to /login.php
Another possibility is you have your your seo friendly urls configuration screwed.
Edit: another possibility is that you fixed the url in the local code, but forgot/failed to update the actual code that runs, which still contains the old location to /login.php
7
u/Alexander-Wright Dec 16 '25
Why are you storing both the password and the password confirmation in the database?
You should start by checking passd and Cpassd are identical, and only if they are store the password hash in the database.
Never store clear text passwords!
2
u/CuAnnan Dec 16 '25
https://xkcd.com/327/
2
u/MateusAzevedo Dec 16 '25 edited Dec 16 '25
First, let's confirm your code is correct:
1- Go to
php.iniand configureerror_reporting = E_ALLanddisplay_errors = On. If for some reason you can't changephp.ini, adderror_reporting(E_ALL); ini_set('display_errors', 'On');at the top of your file.2- Temporarily comment the
header()line and add something likeecho 'Just after execute, redirecting here';.3- Submit your form and check for any output besides that
echo; If you have any error message, you need to fix them.If everything is OK, remove
echo, addheader()again and repeat the process. But this time with your browser's console open. Check the network tab and the requests/responses your browser got.Assuming everything is working as expected, you'll see a POST request, the redirect response and a new
GETrequest to the redirected URL. However, as u/colshrapnel mentioned, you may also find another - unexpected - redirect, giving you a clue about the issue.3
u/allen_jb Dec 16 '25
Side note: Please learn how to use prepared queries to correctly pass in (user submitted) data safely and correctly escaped. See the examples on https://www.php.net/manual/en/pdostatement.execute.php (specifically example #2)
1
u/StevenOBird Dec 16 '25
If you'd check your PHP error log you most likely would see a "headers already sent" message like this:
You cannot add/change headers as soon as PHP made some output. This is because header information must be sent before any content body in a request according to RFC 9112.
Because of this, PHP will process the POST request on the same page where the POST has been made (or the POST action in your HTML form points to) and wil NOT redirect to another location as you've tried in your code.
You could have an echo or printf call in some of your PHP files that were processed before your shown script, a whitespace heading the
<?phptag or a UTF-8 BOM header in one of your PHP files. There are methods like output buffering that could help with that, but it makes things a bit more complicated and need some management.Your best chance to debug that is to check your PHP error log which should show where PHP did some output before.