PHP redirect help

Discussion in 'PHP' started by jojo57, Apr 5, 2011.

  1. #1
    I have the following php script. How can I redirect the person to another web site after the information is saved?

    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = $_POST['fname'] . ' ' . $_POST['lname'] . PHP_EOL;
    $data .= $_POST['email'] . PHP_EOL . PHP_EOL;
    $file = "list.txt";

    $fp = fopen($file, "a") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!");

    fclose($fp);
    echo "Request Successfully Received";
    }
    ?>
     
    jojo57, Apr 5, 2011 IP
  2. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #2
    Use header()
     
    Fracisc, Apr 5, 2011 IP
  3. phppro9x

    phppro9x Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can Use header('Location:'); But 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.
     
    phppro9x, Apr 5, 2011 IP
  4. jojo57

    jojo57 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So would "header(location ) go before opening the file $fp, or before the $_POST line?
     
    jojo57, Apr 5, 2011 IP
  5. phppro9x

    phppro9x Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you use after fclose($fp);
     
    phppro9x, Apr 5, 2011 IP
  6. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    after "fclose($fp)", Also keep in mind you need to remove the echo line. you can't have an output when you're using header() like that.
     
    Sepehr, Apr 5, 2011 IP
  7. phpsoft

    phpsoft Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    header("location:xxx.php?msg='Request Successfully Received'");

    Please remove the echo statement.
     
    phpsoft, Apr 6, 2011 IP
  8. jojo57

    jojo57 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you for all the input. I removed the echo statement, added the header() after the fclose($fp) and everything worked perfectly. As Hannibal used to say "I love it when a plan comes together."
     
    jojo57, Apr 6, 2011 IP
  9. sd3189541

    sd3189541 Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is simple but helpful example. Thank you.
     
    sd3189541, Apr 6, 2011 IP