Intisar Kassab Intisar Kassab - 3 months ago 15
PHP Question

php redirect isn't working right

I have a problem with my redirect in php. The problem is that it redirects the user when they first load the page, I'm trying to make it redirect them when they submit the form... my code:

<?php
header("Location: example.com");
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$address = $_POST['address'];
?>

<!DOCTYPE html>
<h1>Example HTML</h1>


It may be my host cause I used a different host in the past and this php code worked :/ do you know any way I can get this php code working on my current host? It'd be really great if you helped

Answer

Even if you put header to if statement you should stop script execution because of sometimes PHP might throw an exception that HEADERS ALREADY SENT. So you should do something like this :

<?php
if($_POST){
    header('Location: blabla');
    return false;
}
?>
<!DOCTYPE html>
Comments