【问题标题】:How to include php file instead of url in php redirect如何在 php 重定向中包含 php 文件而不是 url
【发布时间】:2016-02-05 12:44:37
【问题描述】:

而不是这样的常规重定向:

<?php
  header('Location: http://domain.com');
?>

...我需要从另一个 php 文件而不是 http://domain.com 调用 URL,所以类似这样:

<?php
  header('Location: <?php include("file.php");?>');
?>

File.php 中包含正确的 url(使用 API 代码生成)。但它不会读取 php 重定向中的 php 代码。如何让它发挥作用?

【问题讨论】:

  • 为什么不先包含它并在其中保存一个带有所需域的变量并将其附加到您的标头函数中?

标签: php redirect url-redirection http-redirect


【解决方案1】:

这取决于你的 php 文件如何传递生成的 url。

如果它使用回显,使用下一个:

ob_start();
include('file.php');
$output = ob_get_clean();
header('Location: '.$output);

或者如果它使用返回:

$return = include('file.php');
header('Location: '.$return);

【讨论】:

    【解决方案2】:

    试试这个:

    file.php

    <?php 
        // Determine your URL and assign it to $url
        $url = "http://example.com"; 
    ?>
    

    带有标头重定向的文件

    <?php 
        require_once('file.php');
        header('Location: '.$url);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2010-09-30
      • 1970-01-01
      • 2012-08-10
      相关资源
      最近更新 更多