Jump to content

Redirect stoped by echo...


Bman900

Recommended Posts

Here is my code:

 

<?php

$question = $_POST['question'];

$firstname = $_POST['name_first'];

echo "you question is $question";

sleep(5);

if (strstr($question, "house")) {

Header("Location:page1.php");

} else {

echo "$question";

}

?>

 

I want echo "you question is $question"; to run for 5 seconds and than move on to my if statement. Well when I run this it just stops after the sleep and the if statement is not run. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/154704-redirect-stoped-by-echo/
Share on other sites

I see the problem now, i will probably need to redirect multiple times. This is how my code looks like now:

 

<?php

$question = $_POST['question'];

$firstname = $_POST['name_first'];

if (strstr($question, "house")) {

Header("page1.php");

} elseif (strstr($question, "foreclosure")) {

Header("page2.php");

} elseif (strstr($question, "bankruptcy")) {

Header("page3.php");

} else {

Header("default");

}

?>

 

Is there another function that will redirect to a url in an if statement?

no. well, you can echo out a regular html meta redirect if you want...

 

but you could rethink how you've got it setup in the first place to reduce those conditions.

 

You can - make the form values for $_POST['question'] the same as the page names you want to redirect to. Like, make the values page1, page2,page3 (or just 1,2,3) and do:

 

header("Location: " . $_POST['question'] . ".php");

or

header("Location: page" . $_POST['question'] . ".php");

 

depending on the value.

 

You can - make an array like so:

 

$pages = array('house' => page1.php, 'foreclosure' => page2.php, 'bankruptcy' => page3.php);

header($pages[$_POST['question']);

 

Point being is that you don't need to make a condition for every option.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.