标题(“位置:success.php");运行位置代码而不是重定向


header("Location:success.php"); Runs location code instead of redirect

我是PHP的新手,我试图创建一个表单,当用户填写时,被重定向到我的成功页面,然后开始下载,他们的详细信息保存到。txt文件。

我已经设法做到以上所有,但用户没有重定向,但下载文件的代码仍然运行(位于成功页面)。

为什么会发生这种情况?

如果我删除了下载文件的代码,然后用户被重定向,我需要在文件下载之前添加延迟吗?

index . php

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="myform.php" method="post">
Name <input type="text" name="name" value="">
Company <input type="text" name="company" value="">
Website <input type="text" name="website" value="">
Email <input type="text" name="email" value="">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

myform.php

<?php
$name = @$_POST['name']; 
$company = @$_POST['company']; 
$website = @$_POST['website']; 
$email = @$_POST['email']; 
$filename = "info.txt";
 $f_data= '
Name : '.$name.'
Email :  '.$email.'
Website: '.$website.'  
Company: '.$company.'  
=============================================================================    =
';
 $file = fopen($filename, "a");
fwrite($file,$f_data);
fclose($file);
header("Location:success.php");
?>

success.php

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
header("Content-Disposition: attachment; filename=guide.pdf");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize("guide.pdf"));
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen("guide.pdf", "r");
print fread($fp, filesize("guide.pdf"));
fclose($fp);
exit();
?>
<html>
<h1>Congrats</h1>
</html>    

success.php为下载。您的浏览器不会重定向到下载,它只会下载它。您需要将下载代码移动到另一个文件中——比如,download.php,并重定向到该文件。如果有一个"下载报告"按钮,而不是自动下载,用户体验可能会更好。