我想在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;
}
?>
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
好的,有人发布了答案,所以我也会.
die();
在标题之前,标题永远不会发生.在标题后放置模具.
参考文献:
编辑:
成功测试您的代码后,我的建议是die();
在标题之后移动; 确保饼干确实设置/不为空,你(可能)需要删除$_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;
}
?>
Run Code Online (Sandbox Code Playgroud)
屈服:(数字仅为示例)
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'.
也可以看看:
将错误报告添加到文件的顶部,这将有助于查找错误.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Run Code Online (Sandbox Code Playgroud)
旁注:显示错误只能在分段中完成,而不能在生产中完成.
编辑#2:
在测试了我认为阻止重定向的内容之后,您的文件可能已经保存为带有BOM(字节顺序标记)的UTF-8,如果是这种情况,那么您将在标头之前输出.错误报告将通知您.
您可以将该文件另存为ANSI,也可以将UTF-8保存为无BOM.
请教:
归档时间: |
|
查看次数: |
678 次 |
最近记录: |