0

how do I redirect with PHP script? How can I use a PHP script to redirect a user from the url they entered to a different web page/url?

That code doesn't work :/ header() tag doesn't work either.

if(/*do something, nevermind*/)) {
        mail($to, $subject, $message, $headers);
        echo "<p> Form sended </p>";
        header('<meta http-equiv = "refresh" content = "0; url = https://faunaservice.pl">');
        exit;
}
rafciol
  • 1
  • 1

2 Answers2

0

use this to echo your message and then redirect to another page after some seconds.

if(1 == 1 ) {
        mail($to, $subject, $message, $headers);
        echo "
          <p> Form sended </p>
           <script>
               setTimeout(function(){
                    window.location.href = 'https://example.com/';
               }, 5000);
           </script>
        ";
        exit;
}
0

You have two options among others:

Header redirect

header('Location: http://whatever.com');

Depending on your PHP config, you may not want to echo anything before this command, as headers might be sent to the browser as soon as you are echoing, and it will be too late to send new headers.

Refresh meta

echo '<meta http-equiv="refresh" content="0; url=http://whatever.com">';

This must be located in the <head> section of your html document

Note that in your code, sending a meta refresh header is not valid. It's either a redirect header, or a refresh meta in the html's head.

GuCier
  • 6,919
  • 1
  • 29
  • 36