views:

664

answers:

7

Hi, I have this PHP piece of code that gets $_GET['id'] (a number) and do some stuff with this. When its finished I need to increase that number ($_GET['id']) and redirect to the same page but with the new number (also using $_GET['id']).

I am doing something like this:

$ID = $_GET['id'];

// Some stuff here

// and then:

$newID = $ID++;

header('Location: http://localhost/something/something.php?id='.$newID);
exit;

The problem here is that the browser stop me from doing it and I get this error from the browser (Firefox) : "The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Some help here please!

NOTE: Some people suggested to do an internal loop, I mean a simple 'for' loop, but when i do this I get a time limit error (60 seconds) because the loop will take more time to finish..

+1  A: 

I think because the browser is in an endless loop.

If that code you posted is also on the something.php then it will always increment the number.

Also, there is no reason to do $newID = $ID++ because the ++ operator will increment that operand ($ID) automatically.

What you want is some form of logic to stop the loop. Perhaps...

if ($ID > 3) {
   // don't redirect
}

However I can't really see how redirecting as a loop is ever a good idea, unless you are trying to do something that may be better achieved by a background process or cron job.

alex
well ID points to the ID of an article I need to fetch from a website, I want to fetch all articles, so I want to make this loop.. from 1 to 9999 for example, to fetch all. But i think the browser will automatically stop the loop even if there is anotherif ($ID > 3) { // don't redirect}like you said..
Jonathan
@Jonathan Do you mean fetch by download it to your server? Just do it in a loop, or use a Cron... this is definitely *not* how you should do it.
alex
The problem when I use a simple for loop for example is that i get a time limit error (60 seconds).. And its a loop that i think will take an hour or two..
Jonathan
Use a Cron then... that is perfectly suited to its functionality.
alex
+3  A: 

It's an endless loop, each time the page loads, it changes the $_GET['id'] and redirects to itself again. You need a way to detect that the value is how you want it (final) and skip the redirect.

I suggest you add a second parameter after incrementing id, such as final so that when the page loads the second time, it skips the redirect because final was set.

i.e.:

if ($_GET['final'] != 1)
{
    header('Location: http://localhost/something/something.php?id=' . $newID . '&final=1');
}
JYelton
It doesnt help me, I need to redirect the page a lot of times...
Jonathan
You have to stop it at some point, otherwise the browser is going to complain about the endless loop.
JYelton
+1  A: 

I would suggest that you redesign what you are doing. Redirecting back to self should never happen more than once, without a user input required to continue a loop.

As soon as the browser see's even the slightest possibility of and endless loop it will crash it. Even if you have if ($ID == 100) {..STOP..} by the time you reach 100 the browser will have already broken your loop, simply because by industry standards, that is immediately considered bad design and that is why your browser is already programmed to stop such a thing to begin with.

Can you share what it is you are actually trying to do with this loop. I am sure someone has a way to achieve what you want without doing it that way.

UPDATE TO COMMENT

Then you may want to look into AJAX or PHP API. I see what you mean, and want to do, that needs to be done in a controlled environment as opposed to a loop. The browser will never allow that kind of loop. So, there are a few ways to do that. You can do it with a client-side method where php delivers you a page to ajaxify the content from one source to another source. Or strictly php using php API methods to fetch the content and bring it directly back to the server. However, neither case is a beginners concept. :) if you know what I mean.

Cory Cullers
well ID points to the ID of an article I need to fetch from a website, I want to fetch all articles, so I want to make this loop.. from 1 to 9999 for example, to fetch all.
Jonathan
A: 

hi Based on your code

ob_start();
session_start(); 


if (  $_SESSION["id_old"] == $_GET['id']){ return false;}

 $ID = $_GET['id'];
 $newID = ++$ID;

 $_SESSION["id_old"]= $newID;


 header('Location: http://localhost/something/something.php?id='.$newID);

exit;

test localhost/something/something.php?id=22 -> localhost/something/something.php?id=23 return false;

Zephiro
A: 

It looks like you're trying to scrape a large amount of data from an external source. Why don't you run it as a local script on your own computer instead of through the browser? Then you don't have to worry about your script timing out or the browser detecting infinite redirects.

erjiang
Sounds good, I'm doing locally using WAMP Server localhost but how can I do it without using a browser or the PHP time limit?
Jonathan
A: 

Well the answer is to do it locally in my localhost, using a 'for' loop and set_time_limit(0) to set the deafult 60 second limit of PHP to 0 (infinite) and not redirecting to the same page over and over again.

Jonathan
A: 

thanks for the info. i was looking for this.

usman