douzhan1935 2015-09-27 15:01
浏览 51

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

This question already has an answer here:

I'm new to PHP and I'm trying to create a form which when the user fills out, is redirected to my success page and then a download is initiated and their details are saved to a .txt file.

I've managed to do all of the above but the user isn't redirected, but the code to download the file still runs (which is located in the success page).

Why is this happening?

If I removed the code to download the file then the user is redirected, do I need to add a delay before the file download?

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>    
</div>
  • 写回答

1条回答 默认 最新

  • drq61040 2015-09-27 15:36
    关注

    success.php is a download. Your browser will not redirect to the download, it will just download it. You will need to move your download code to another file - say, download.php, and redirect to that. It may be better user experience to have a "download your report" button rather than an automatic download.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么