开发者

PHP: Redirect to the same page, changing $_GET

开发者 https://www.devze.com 2023-01-01 05:33 出处:网络
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 th

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..


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');
}


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.


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.


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;


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消