在重定向php之前执行代码

时间:2016-01-28 12:38:41

标签: php redirect

我想在txt文件中写入一些信息(用户名,用户邮件和IP地址),然后重定向到另一个html文件。如果我尝试重定向,则文本文件中不会出现任何内容。但是,如果我不重定向到HTML文件,PHP脚本正常工作。我错过了什么?

<?php
$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
$txt1 = file_get_contents("newfile.txt");
$ip = get_client_ip_server();
$txt2 = $_COOKIE["userName"] ."\t" .$_COOKIE["userEmail"]." \t".$ip. "\r\n";
$txt3 = $txt1 . $txt2;
fwrite($myfile, $txt3);
fclose($myfile);
die();
header( "HTTP/1.0 302 Found" );
header( "Status: 302" ); # this is for chrome compliance
header( "Location: general.html" . $_SERVER["REQUEST_URI"] );
// Function to get the client ip address
function get_client_ip_server() {
$ipaddress = '';
      if (getenv('HTTP_CLIENT_IP'))
            $ipaddress =getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
}
?>

感谢您的帮助

2 个答案:

答案 0 :(得分:6)

好的,有人发了答案,所以我也会。

die();在标题之前,因此标题永远不会发生。将标题放在标题之后。

参考文献:

修改

成功测试您的代码后,我的建议是在标题后移动die();;确保Cookie确实设置为/非空并且您(可能)需要从$_SERVER["REQUEST_URI"]

中移除header( "Location: general.html" . $_SERVER["REQUEST_URI"] );

使用header( "Location: general.html" . $_SERVER["REQUEST_URI"] );失败让我重定向到general.html,删除$_SERVER["REQUEST_URI"]的确重定向到general.html

参考文献:

将此作为测试:

<?php
$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
$txt1 = file_get_contents("newfile.txt");
$ip = get_client_ip_server();
// $txt2 = $_COOKIE["userName"] ."\t" .$_COOKIE["userEmail"]." \t".$ip. "\r\n";

$txt2 = "Username cookie"  ."\t" . " Email cookie " ." \t".$ip. "\r\n";

$txt3 = $txt1 . $txt2;
fwrite($myfile, $txt3);
fclose($myfile);

header( "HTTP/1.0 302 Found" );
header( "Status: 302" ); # this is for chrome compliance

header( "Location: general.html");
// header( "Location: general.html" . $_SERVER["REQUEST_URI"] );

die();

// Function to get the client ip address
function get_client_ip_server() {
$ipaddress = '';
      if (getenv('HTTP_CLIENT_IP'))
            $ipaddress =getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
}
?>

屈服:(数字仅为示例)

Username cookie  Email cookie   11.22.33.44

Username cookie  Email cookie   11.22.33.44

Username cookie  Email cookie   11.22.33.44

关于$_SERVER["REQUEST_URI"]

  

'REQUEST_URI'       为访问此页面而给出的URI;例如,'/ index.html'。

另见:

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

旁注:只应在暂存时进行显示错误,而不是生产。

编辑#2:

在测试我认为阻止重定向的内容后,您的文件可能已保存为带有BOM(字节顺序标记)的UTF-8,如果是这种情况,那么您将在标头之前输出。错误报告会通知您。

要么将该文件保存为ANSI,要么将UTF-8保存为无BOM。

咨询:

答案 1 :(得分:1)

die在重定向之前,你应该把它放在标题重定向之后;)