在重定向 PHP 之前执行代码


execute code before redirect php

我想在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;
}
?>

感谢您的帮助

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

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

引用:

  • http://php.net/manual/en/function.die.php
  • http://php.net/manual/en/function.header.php

编辑:

在成功测试您的代码和我建议在标头后移动die();之后;确保cookie确实设置为/不为空,并且您(可能需要)从header( "Location: general.html" . $_SERVER["REQUEST_URI"] );中删除$_SERVER["REQUEST_URI"]

使用header( "Location: general.html" . $_SERVER["REQUEST_URI"] );我无法重定向到general.html,删除$_SERVER["REQUEST_URI"]确实重定向到general.html

引用:

  • 检查 PHP cookie 是否存在,如果未设置其值
  • http://php.net/manual/en/function.isset.php
  • http://php.net/manual/en/function.empty.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";
$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;
}
?>

产量:(数字仅为示例)

用户名 cookie 电子邮件 cookie 11.22.33.44用户名 cookie 电子邮件 cookie 11.22.33.44用户名 cookie 电子邮件 cookie 11.22.33.44

关于$_SERVER["REQUEST_URI"]

"REQUEST_URI" 为了访问此页面而提供的 URI;例如,"/index.html"。

  • http://php.net/manual/en/reserved.variables.server.php

另请参阅:

  • $_SERVER['REQUEST_URI'] 和 $_GET['Q'] 有什么区别?

将错误报告添加到文件顶部,这将有助于查找错误

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code

旁注:显示错误只能在暂存中完成,而不应在生产中完成。


编辑#2:

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

要么将该文件另存为 ANSI,要么另存为 UTF-8 而不另存 BOM。

咨询:

  • 如何修复PHP中的"标头已发送"错误
  • https://en.wikipedia.org/wiki/Byte_order_mark

die在重定向之前,您应该将其放在标头重定向之后;)