如何只执行页面URL并重定向到另一个页面?

时间:2015-08-03 06:18:03

标签: php redirect

我想执行一个URL,然后页面将重定向到请求的标头位置。但页面不重定向。我的上一页显示在这里。

查看我的代码 -

使用example.php

<?php
function test() {
    $aa = 'good morning';
    //file_get_content('http://localhost/DemoProject/test.php');
    $ch = curl_init('http://localhost/DemoProject/test.php');
    curl_exec($ch);
    return $aa;
}
$text = test();
header('location:http://www.google.co.in/search?q='.urlencode($text));
?>

test.php文件中,我编写了一个pdf生成的代码和电子邮件功能,生成PDF文件附件。我只想http://localhost/DemoProject/test.php执行。

当我运行example.php文件时。该页面将重定向到http://www.google.co.in/search?q=good+morning,这会显示包含test.php数据的当前页面。

如何只执行URL而不检索任何数据?

2 个答案:

答案 0 :(得分:1)

尝试在header()语句之后添加exit语句,这将使您的代码看起来像

<?php
function test() {
    $aa = 'good morning';
    //file_get_content('http://localhost/DemoProject/test.php');
    $ch = curl_init('http://localhost/DemoProject/test.php');
    curl_exec($ch);
    return $aa;
}
$text = test();
header('location:http://www.google.co.in/search?q='.urlencode($text));
exit;
?>

这是PHP文档,声明添加exit将停止所有其他代码执行并重定向到URL。 http://php.net/manual/en/function.header.php

还要确保在调用header()之前没有写入输出,即不回显或有空行,这些空行将在header()之前输出。

答案 1 :(得分:1)

您可以通过设置选项CURLOPT_RETURNTRANSFER

将输出发送到浏览器
$ch = curl_init('http://localhost/DemoProject/test.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
相关问题