tags:

views:

148

answers:

5

hi

i am using xajax framework

i want redirect my url in for loop. every time its go to the home.php at last

my sample code is this

for($i=0;$i<4;$i++) {
    if($i == 1) {
        header("index.php")
    } else {
        header("home.php")
    }
}
+6  A: 

Well, since the start value of '$i' is 0, the else-block will be called the first time which points to home.php.

Why even have a loop to do this? It makes no sense.

Björn
+2  A: 

What exactly is the point of that code? You can only have one redirect in your script.

And, it should be written like this:

header('Location: home.php');
exit;

without the exit, the code keeps running.

Anti Veeranna
Actually, it depends on the system configuration whether you need to exit() the script before the redirect takes place.
Björn
ok, that is new for me, please tell me more, how can you affect that behaviour?
Anti Veeranna
I think it depends on the output_buffering configuration in php.ini. But I do as you do, explicit call to exit() or die() to ensure my script does not continue to run. Moreover a status-header should be placed before the location-header, otherwise Chrome won't allow a redirect. Best practice is to add the full path to the page.header("Status: 200");header("Location: http://domain.com/page.php");die();
Björn
A: 

It's a simple mistake that I fall into a lot myself, but header() can set plenty of different headers; you need to actually specify the Location header (with the page to redirect to):

header("Location: index.php")
Twisol
A: 

I agree with this post: http://stackoverflow.com/questions/1304890/redirect-problem-in-for-loop-in-php/1304896#1304896

But if you still want to do this, you can find some workaround by passing a variable defining that your loop has already been executed + you should end execution while redirecting page.

You can use this code and change in your way:

if(!isset($_GET['ex']) || $_GET['ex'] != '1') //can use ether isset or/and check its value  
{
    for($i=0;$i<4;$i++) 
    {
        if($i == 1)
        {
            header("index.php?ex=1");
            die(); //or exit();
        } else {
            header("home.php?ex=1");
            die(); //or exit();
        }
    }
}
George
It still makes no sense whatsoever, all it'll ever do is send you to `home.php`. If the header directive was fixed, that is.
deceze
Its a dummy loop. Maybe he has a foreach loop with different expression in if statemend. As I stated before he should change it in his way. Please reconsider vote down and read the statement before the code ;)
George
What's the point of a "dummy loop", what's the point of a loop at all? If you're going to post code, please post some that makes sense. Don't give bad advise.
deceze
I gave an advise and not an exact answer because the question itself was formed incorrectly. You should consider reading a post before copying the string like novice...
George
A: 

The point is that after a redirect, its a whole new page load. PHP starts everything a new, so your loop will be started new. That means on every page $i will start at 0 and loop to 3.

header("Location... does not automatically redirect you, you have send something to the user or stop the page (with exit or die) to send the headers. So your for loop will always redirect to home.php because it's the last header set in the for loop, not because its the first.

OIS