循环遍历 url

发布于 2024-12-08 07:16:46 字数 693 浏览 2 评论 0原文

我想做一个循环,通常是用 while do for 等完成的,但是当进程很大时,我想出了一个解决方案通过回显 JavaScript 来刷新页面以刷新下一个循环。

例如:

页面是http://localhost/index.php --> 使用 $i=1; 执行第一次迭代,

这将在脚本末尾

if (!$_GET['i']){
 $i = 1;
}else{
 $i = $_GET['i'];
}
if ($i<500){
 // proceed with $i = $_GET['i']
 //then redirect to  http://localhost/index.php?i=$i++
}else{
 echo "done";
}

它将被重定向到 http://localhost/index.php?i=$i++现在,考虑输入参数来自 FORM 到此脚本的情况。 (即$parameter1、$parameter2、$parameter3) 然后我每次都必须将它们传递到新的网址(下一次迭代)。

在正常工作中,我可以将它们作为 GET 变量传递给新的 url,但是如果我不希望用户能够看到 url 中的参数值,我该如何传递它们呢?

I want to do a loop, normally it is done with while do for etc but when the process is big I came up with a solution to refresh the page by echoing a javascript to refresh the page for the next loop.

for example:

The page is http://localhost/index.php --> this preforms the first iteration with $i=1;

at the end of the script it will be redirected to http://localhost/index.php?i=$i++

if (!$_GET['i']){
 $i = 1;
}else{
 $i = $_GET['i'];
}
if ($i<500){
 // proceed with $i = $_GET['i']
 //then redirect to  http://localhost/index.php?i=$i++
}else{
 echo "done";
}

Now, consider a situation that the imput parameters come from a FORM to this script. (i.e. $parameter1 , $parameter2, $parameter3)
Then I have to pass them every time to new url (next iteration).

At normal work I can pass them as GET variable to new url but how can I pass them if I don't want the user be able to see the value of parameters in url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝礼 2024-12-15 07:16:46

在正常工作中,我可以将它们作为 GET 变量传递给新的 url,但是如果我不希望用户能够看到 url 中的参数值,我该如何传递它们?

您不能使用裸重定向,但如果您正在谈论特定用户,您可以通过将这些参数分配为 会话变量 Docs,然后将会话 ID 作为附加参数传递(或信任用户启用了 Cookie)。

function do_redirect($i, Array $parameters)
{
   $i = (int) $i;
   $parameters['i'] = $i; // save to session as well
   $_SESSION['parameters'] = $parameters;
   // redirect to http://localhost/index.php?i=$i&SID
}

if (is_form_request())
{
    $parameters = get_form_parameters();
    do_redirect(1, $parameters);
}
elseif (is_redirect_loop_request())
{
    $parameters = $_SESSION['parameters'];
    $i = $parameters['i'];
    if ($i < 500)
    {
        do_redirect($i++, $parameters);
    } else {
        echo "done.";
    }
}

At normal work I can pass them as GET variable to new url but how can I pass them if I don't want the user be able to see the value of parameters in url?

You can not with the bare redirect, but if you're talking about a specific user, you can do so by assigning those parameters as session variables Docs and then passing the session id as an additional parameter (or trust the user has cookies enabled).

function do_redirect($i, Array $parameters)
{
   $i = (int) $i;
   $parameters['i'] = $i; // save to session as well
   $_SESSION['parameters'] = $parameters;
   // redirect to http://localhost/index.php?i=$i&SID
}

if (is_form_request())
{
    $parameters = get_form_parameters();
    do_redirect(1, $parameters);
}
elseif (is_redirect_loop_request())
{
    $parameters = $_SESSION['parameters'];
    $i = $parameters['i'];
    if ($i < 500)
    {
        do_redirect($i++, $parameters);
    } else {
        echo "done.";
    }
}
薄荷→糖丶微凉 2024-12-15 07:16:46

不想粗鲁,但上面的两个答案都很容易出现安全问题(但会话解决方案是最好的)。至于@itamar的“加密”解决方案:这不完全是加密......这被称为“凯撒密码”(http://en.wikipedia.org/wiki/Caesar_cipher),它确实像纸核一样安全掩体……

它可以更容易、更安全;不要将迭代保存在会话中,而是保存在数据库中。对于下一个请求,您唯一要做的就是从数据库中获取迭代器并继续执行您想做的任何事情。会话可能被盗,这意味着有人可以让您从 $i=10 等地方迭代一千次。当迭代器存储在安全数据库中时,无法完成此操作。

Not to be rude, but both answers above are quite prone to security issues (but the session solution is the best one). As for the 'encryption' solution of @itamar: that's not exactly encryption... This is called 'Caesar cypher' (http://en.wikipedia.org/wiki/Caesar_cipher), which is indeed as safe as a paper nuclear bunker...

It can be much easier and safe as can be; do not save the iteration in the session, but in the database. For the next request, the only thing you have to do is get the iterator from the database and go on with whatever you want to do. Sessions can be stolen, meaning someone could let you iterate from, say, $i=10 a thousand times. It cannot be done when the iterator is stored in a secure database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文