PHP echo and redirect

19,594

Solution 1

Because you're sleeping in the code that generates the html for the page, not in the browser that displays it to the end user...

Look to pause before redirecting in the browser using a meta refresh. generate html that contains something like:

<html>
    <head>
        <meta http-equiv="refresh" content="3;url=http://www.somewhere.com/" />
    </head>
    <body>
        <h1>Redirecting in 3 seconds...</h1>
    </body>
</html>

EDIT

<?php
$dateiname = 'daten.txt';
$inhalt =$_POST['vorname'].' '.$_POST['nachname']."; ".$_POST['abteilung'].";".$_POST['strecke']."; ".$_POST['groesse'].' ; '.$_POST['tag'].'.'.$_POST['monat'].'.'.$_POST['jahr'].PHP_EOL."";

echo '<html>
    <head>
        <meta http-equiv="refresh" content="10;url=http://www.google.com/" />
    </head>
    <body>';

echo "Sie haben folgende Angaben gemacht:<br>";
echo "Name: $_POST[vorname] $_POST[nachname]
<br>Abteilung: $_POST[abteilung]
<br>Geburtsdatum: $_POST[tag].$_POST[monat].$_POST[jahr]
<br>Strecke: $_POST[strecke]
<br>Groesse: $_POST[groesse]<br> ";
echo "Vielen Dank!";
$handle = @fopen($dateiname, "ab+");
fwrite($handle, $inhalt);
fclose ($handle);
if (file_exists($dateiname) == true) {
@chmod ($dateiname, 0757);
}

echo '</body></html>';

Solution 2

You cannot redirect users from php AFTER you sent to the user some data. However you can achieve this by using javascript on your page:

<script>
     setTimeout(function(){
          windows.location = "next_page.html";
     }, 10000);
</script>

10000ms = 10 sec

Solution 3

PHP is server side programming language. Once it has been sent to the client, it can no longer update the page, and it returns the whole page generated as an HTML page. it get a request and generates the response as HMTL and sends it to the client(in this case browser), sleep(10), stops PHP script for 10 seconds before generating the response. It means it Does not echoes anything before finishing the request.

Solution 4

I did it very similar to @Mark Baker. Except for using the $var instead of the $_POST['name']

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent = "From: $name \n Message: $message";
$recipient = "[email protected]";
$subject = "My Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Son of a     biscuit, something went wrong!");
echo '<html>
<head>
    <meta http-equiv="refresh" content="4;url=http://www.mywebsite.com/" />

</head>
<body>
   <div>';
     echo "<h1>Thank you for the message: </h1><br>";
     echo "Name: $name <br>
           Email:  $email <br>
           Message: $message";
     echo '</div></body></html>';
?>
Share:
19,594
Patrick
Author by

Patrick

Applied CS @ Ruhr-University Bochum

Updated on September 07, 2022

Comments

  • Patrick
    Patrick over 1 year
    <?php
    $dateiname = 'daten.txt';
    $inhalt =$_POST['vorname'].' '.$_POST['nachname']."; ".$_POST['abteilung'].";".$_POST['strecke']."; ".$_POST['groesse'].' ; '.$_POST['tag'].'.'.$_POST['monat'].'.'.$_POST['jahr'].PHP_EOL."";
    echo "Sie haben folgende Angaben gemacht:<br>";
    echo "Name: $_POST[vorname] $_POST[nachname]
    <br>Abteilung: $_POST[abteilung]
    <br>Geburtsdatum: $_POST[tag].$_POST[monat].$_POST[jahr]
    <br>Strecke: $_POST[strecke]
    <br>Groesse: $_POST[groesse]<br> ";
    echo "Vielen Dank!";
    $handle = @fopen($dateiname, "ab+");
    fwrite($handle, $inhalt);
    fclose ($handle);
    if (file_exists($dateiname) == true) {
    @chmod ($dateiname, 0757);
    }
    sleep(10);
    header("location: http://google.com");
    ?>
    

    I try to do a php echo and then redirect after 10secs. But with this code, php echo is not displayed.

  • Mr. Alien
    Mr. Alien about 10 years
    JS will be disabled, and boom, fail... Consider using meta instead, read Mark's answer
  • Bobby
    Bobby about 10 years
    Yes, I agree with you :). However, not in all occasions is possible to change the page's metas (for ie. if you have access only to the contents of a website, but you can't change the template). Thegesuser didn't specify all the context, so I posted this alternative.
  • Mr. Alien
    Mr. Alien about 10 years
    Makes sense :) though he will have to feed the script in the template as well.. though +1
  • Mark Baker
    Mark Baker about 10 years
    Could also be done via the body onload: <body onload="timer=setTimeout(function(){ window.location='http://www.google.com';}, 3000)">
  • Patrick
    Patrick about 10 years
    So, i've got an input form and this input form is handled by a php. Is there a way to 'embed' this php into an HTML?
  • Mark Baker
    Mark Baker about 10 years
    This isn't PHP, this is HTML markup: you can use PHP to generate this HTMl markup...
  • Mark Baker
    Mark Baker about 10 years
    ... but if you put a timed redirect on a page that contains a form, you'll get a lot of annoyed users who've partly completed filling in the form suddenly being redirected
  • Patrick
    Patrick about 10 years
    Is it possible to grab the PHP echo to display it on a html page?
  • Mark Baker
    Mark Baker about 10 years
    I really don't understand what you're asking: it's your PHP code that is generating the HTML markup, it can echo whatever you want it to echo