简体   繁体   English

PHP:重定向到同一页面,更改$ _GET

[英]PHP: Redirect to the same page, changing $_GET

I have this PHP piece of code that gets $_GET['id'] (a number) and do some stuff with this. 我有这段PHP代码,它获得$ _GET ['id'](一个数字)并为此做一些事情。 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']). 完成后,我需要增加该数字($ _GET ['id'])并重定向到同一页面,但使用新的数字(也使用$ _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." 这里的问题是浏览器阻止我执行此操作,并且从浏览器(Firefox)中收到此错误:“页面无法正确重定向。Firefox已检测到服务器正在以这种方式重定向对该地址的请求永远不会完成。”

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.. 注意:有人建议做一个内部循环,我的意思是一个简单的“ for”循环,但是当我这样做时,我得到一个时间限制错误(60秒),因为循环将花费更多的时间来完成。

It's an endless loop, each time the page loads, it changes the $_GET['id'] and redirects to itself again. 这是一个无休止的循环,每次页面加载时,它都会更改$_GET['id']并再次重定向至自身。 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. 我建议您在增加id之后添加第二个参数,例如final以便在第二次加载页面时,由于设置了final ,它将跳过重定向。

ie: 即:

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. 如果您发布的代码也位于something.php上,则它将始终增加该数字。

Also, there is no reason to do $newID = $ID++ because the ++ operator will increment that operand ( $ID ) automatically. 另外,没有理由执行$newID = $ID++因为++运算符将自动递增该操作数( $ID )。

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. 但是,除非您尝试做一些可以通过后台进程或cron作业更好地完成的工作,否则我真的看不到将循环重定向作为一个好主意。

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. 即使您在达到100时拥有if($ ID == 100){..STOP ..},浏览器也已经打破了循环,仅因为按照行业标准,这立即被认为是糟糕的设计,这就是为什么您的浏览器已经被编程为停止这种情况。

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. 然后,您可能需要研究AJAX或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. 您可以使用客户端方法来执行此操作,其中php为您提供了一个页面来将内容从一个来源扩展到另一个来源。 Or strictly php using php API methods to fetch the content and bring it directly back to the server. 或者使用PHP API方法严格使用php来获取内容并将其直接带回服务器。 However, neither case is a beginners concept. 但是,这两种情况都不是初学者的概念。 :) if you know what I mean. :) 如果你明白我的意思。

ob_start(); ob_start(); session_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; 测试localhost / something / something.php?id = 22-> localhost / something / something.php?id = 23返回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. 然后,您不必担心脚本超时或浏览器检测到无限重定向。

答案是在本地主机中本地执行此操作,使用“ for”循环和set_time_limit(0)将默认的60秒PHP限制设置为0(无限),而不是一遍又一遍地重定向到同一页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM