1

How do I make the code below redirect to another page if there are no errors?

// If there are no errors, send the email
if (!$errName && !$errEmail) {
    if (mail ($to, $subject, $body, $from)) {
        $result= 'http://www.example.com';
            } else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }

At the moment because the url is in between tick marks it writes this to the page, I dont know what symbols to use to make is actually work.

Rosa
  • 642
  • 6
  • 20
Frank Smith
  • 119
  • 1
  • 8
  • Are you looking for [header](http://php.net/manual/en/function.header.php) ?? – Saty Nov 18 '15 at 06:02
  • 1
    Possible duplicate of http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php?rq=1 – Thamilhan Nov 18 '15 at 06:08
  • Be careful with the answer that you accepted. It does not use a die() or exit() function. This can be a security risk as explained in my answer. – kojow7 Nov 18 '15 at 06:22

4 Answers4

2

Use the following after your URL:

header('Location: ' . $result, true);
exit;

It will redirect to your desired page.

rajausman haider
  • 570
  • 2
  • 10
  • 20
1

Use the PHP header function as follows:

// If there are no errors, send the email
if (!$errName && !$errEmail) {
    if (mail ($to, $subject, $body, $from)) {
        header('Location: http://www.example.com');
        die();
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }

The header function must come before any echo, print, or other output statements.

Make sure to use die() after you redirect otherwise it could be a security issue. Without the die() command, PHP will continue sending the rest of the PHP file to the web client. Users can capture this information by blocking any redirects. Of course if there are no other statements in the file after this you do not need to use die(), but it is definitely a good habit to get in to every time you have a redirect and you never know if you might add some more code later.

kojow7
  • 10,308
  • 17
  • 80
  • 135
0

You can simple use location.href

if (!$errName && !$errEmail) {
    if (mail ($to, $subject, $body, $from)) {
        $result= 'http://www.example.com';
        echo "<script>location.href='$result'</script>";
    } else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}

Or PHP's header function

if (!$errName && !$errEmail) {
        if (mail ($to, $subject, $body, $from)) {
            $result= 'http://www.example.com';
            header("Location: $result"); 
        } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}

But when you use header function:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

Simple Redirection

To redirect the visitor to another page (particularly useful in a conditional loop), simply use the following code:

<?php    
  header('Location: http://www.example.com');    
?>

If ever the target page is on another server, you include the full

<?php    
  header('Location: http://www.example.com/mypage');    
?>

HTTP Headers

Temporary/permanent redirections

By default, the type of redirection presented above is a temporary one. This means that search engines such as Google will not take into account for indexation. So if you want to notify the search engines that the page has been permanently moved to another location:

<?php 
  header('Status: 301 Moved Permanently', false, 301);    
  header('Location: http://www.example.com');    
?>

Interpretation of PHP code

The PHP code located after the header() will be interpreted by the server, even if the visitor move to the address specified in the redirection, which means that in most cases you need a method of follow the header() function of the exit() function, in order to decrease the load of the server.

<?php
  header('Status: 301 Moved Permanently', false, 301);    
  header('Location: http://www.example.com');    
?> 
Santosh
  • 393
  • 2
  • 11