Skip to main content
Participant
May 11, 2011
Question

Help with simple php page linking question

  • May 11, 2011
  • 2 replies
  • 553 views

I posted this question yesterday but for some reason it isn't showing up on the forum, so I'm reposting.

I have a small php file to force a download when a user clicks on a link:

<?php

header ("Content-type:application/pdf");

header ("Content-Disposition:attachment;filename='form.pdf'");

readfile ("form.pdf");

?>

I need to add code to this script which will automatically redirect the user to the home page within the same browser window, either during or immediately after the file downloads. And if it is possible, prevent the browser back button from returning the user back to the page that contains the download link.

I tried adding this after the readfile line: echo "<a href="mywebsite.com/index.html" /a>";

But that failed miserably, a monument to my limited php skills. Can someone help me out?

This topic has been closed for replies.

2 replies

nickentin
Participating Frequently
May 12, 2011

You can do this in PHP:

header ("Location: redirectToHere.html");

Not sure how to disable the back button.

Nick

RichLornAuthor
Participant
May 12, 2011

Hi Nick

Thanks for your suggestion!  I tried adding that header line, substituting the full path to index.html in place of redirectToHere. When I placed it after the readfile line in my script, the file downloaded normally but the browser page wasn't redirected. Then I tried putting your header line with the other header lines above readfile, but when clicking on the link, the file does not download and instead I get an error 404 Page not found. Am I missing something?

nickentin
Participating Frequently
May 12, 2011

Hmm... when you call the readfile() it opens the file that you link to.  The location is then set to that, so you can't redirect.

Is there another way you could download the file?  I'm afraid I've gone slightly beyond my PHP knowledge.  Sorry.

Nick

RichLornAuthor
Participant
May 11, 2011

Oops, my bad. My original message is now showing

on the board. Sorry about the double post. Now if I can just get someone to answer...