【问题标题】:PHP redirection issuePHP重定向问题
【发布时间】:2011-11-28 19:29:48
【问题描述】:

我有一个打印用户 ID 列表报告的程序。该程序应该为上传列表中的用户一一打印报告。问题是,当我运行打印过程并使用 indexInList=30 打印报告时,出现错误:

此网页有重定向循环
http://127.0.0.1/content/8520?print=1&bulkprinting=1&filename=/private/var/tmp/phpHRXEw8.moved&indexInList=30&nopeergroup=1&nolabpage=0&hideScreeningOnly=1&showOnlyScreening=0&hideHoldMailing=1 的网页导致了过多的重定向。清除此站点的 cookie 或允许第三方 cookie 可能会解决问题。如果不是,则可能是服务器配置问题,而不是您的计算机问题。

我试图清理 cookie,但仍然收到同样的错误。

我在这里附上了一些代码,希望有人能帮助我:

  $sessionData['first_name'] = $foundUser->first_name;
  $sessionData['last_name']  = $foundUser->last_name;

  // Overwrite $_REQUEST variable with parameters before including
  // the hpa report
  $_REQUEST = array(
    'user_id'      => $foundUser->id,
    'bulkprinting' => true
  );
  if($nopeergroup) { $_REQUEST['nopeergroup'] = $nopeergroup; }
  if($nolabpage) { $_REQUEST['nolabpage'] = $nolabpage; }
  if($hideScreeningOnly) { $_REQUEST['hideScreeningOnly'] = $hideScreeningOnly; }
  if($showOnlyScreening) { $_REQUEST['showOnlyScreening'] = $showOnlyScreening; }
  if($hideHoldMailing) { $_REQUEST['hideHoldMailing'] = $hideHoldMailing; }

  $includeValue = include __DIR__.'/../hpa/hpa.php';

  $url = sprintf(
    "/content/8520?print=1&bulkprinting=1&filename=%s&indexInList=%s" .
    "&nopeergroup=%s&nolabpage=%s&hideScreeningOnly=%s" .
    "&showOnlyScreening=%s&hideHoldMailing=%s",
    $filename, $indexInList, (int)$nopeergroup, (int)$nolabpage,
    (int)$hideScreeningOnly, (int)$showOnlyScreening, (int)$hideHoldMailing);

  if($hradata[0] !== false) {
    $sessionData['hra_id'] = $hradata[0]['id'];
  }
  if($screeningdata[0] !== false) {
    $sessionData['screening_id'] = $screeningdata[0]['id'];
  }

  if($includeValue !== 1) {
    // Redirect to URL
    $sessionData['message']     = $messages_set[$includeValue];
    $_SESSION['printing_set'][] = $sessionData;

    redirect($url);
  }

  $sessionData['markAsMailed'] = true;
  $_SESSION['printing_set'][]  = $sessionData;
?>
<script type="text/javascript">
  function waitPrint() {
    window.print();
    var t = setTimeout("timed()", 1000);
  }
  function timed() {
    window.location.replace("<?php echo $url ?>");
  }

  if(window.attachEvent) {
    window.attachEvent("onload", waitPrint);
  } else if(window.addEventListener) {
    window.addEventListener("load", waitPrint, false);
  }
</script>

【问题讨论】:

    标签: php


    【解决方案1】:

    听起来您有很多文件需要打印!

    您可以更改浏览器设置(我记得在 Firefox 中可以)以允许超过 30 个循环。

    或者,您始终可以将代码限制为 30 个循环,然后等待进一步的用户交互以进行下一个 30。

    第三种选择是始终创建一个 Word 文档或 PDF,每页上都有一份报告,然后保存文件并打印 - 有点麻烦(在某种程度上),但至少您可以打印所有内容一次。

    【讨论】:

      【解决方案2】:

      为了将$includeValue 设置为任何值,文件__DIR__.'/../hpa/hpa.php' 必须在其中包含return 语句,如includePHP documentation 中所示,示例5。include 只会返回如果包含的文件返回值,则调用时的值。

      如果您的脚本仍然产生无限循环,则您在包含文件中的逻辑不正确,并且始终产生一个不是 1 的值。

      基本上,您的问题归结为以下代码:

      $includeValue = include __DIR__.'/../hpa/hpa.php';
      if($includeValue !== 1) {
          // Redirect
      }
      

      【讨论】:

      • 感谢您的回复。你知道如何解决这个问题吗?
      【解决方案3】:

      浏览器具有内置检查功能,可在站点被错误配置为重定向循环时为您提供帮助,并且您使用的浏览器必须限制为 30 个。您故意构建了一个重定向循环,但浏览器不知道这一点。与其使用window.location.replace() 方法,不如使用自动提交的表单?这应该与浏览器不同,并允许您的循环按设计进行。

      <script type="text/javascript">
        function waitPrint() {
          window.print();
          var t = setTimeout("timed()", 1000);
        }
        function timed() {
          window.reloadForm.submit();
        }
        if(window.attachEvent) {
          window.attachEvent("onload", waitPrint);
        } else if(window.addEventListener) {
          window.addEventListener("load", waitPrint, false);
        }
      </script>
      
      <form name="reloadForm" action="<?php echo $url ?>">
      </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        • 2011-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-05
        相关资源
        最近更新 更多