Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4240398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:09:34+00:00 2026-05-21T03:09:34+00:00

I know this question has been asked many times before but I can’t find

  • 0

I know this question has been asked many times before but I can’t find an answer to suit my needs.

I need to find a way to force the download of a file and then, after the download has started, redirect to a “thanks for downloading” page.

So far I have:

<?php
ob_start();

$token = $_POST['validationCode'];

if(isset($token)){

    $connect = mysql_connect('localhost', 'root', 'root');
    $db = mysql_select_db('mydb');

    if (!$connect || !$db){
        die('Connect Error (' . mysql_connect_errno() . ') '
                . mysql_connect_error());
    }

    $sql = mysql_query("SELECT * FROM emailaddresses WHERE token='$token'");
    $result = mysql_fetch_array($sql);
    if($result){
        header('Location: complete.php');
        header('Content-type: application/mp3');
        header('Content-Disposition: attachment; filename=track.mp3');
        $f = file_get_contents('downloads/track.mp3');
        print $f;
        $sql = "UPDATE emailaddresses SET download=1 WHERE token='$token'";
        $result = mysql_query($sql);
    }
    else{
        echo "There was a problem downloading the file" . mysql_error();
    }
}

ob_end_flush();

?>

It’s important to hide the download file’s location otherwise I would have just created an HTML link to the file.

I obviously can’t put a redirect header below the other headers as it just won’t work. I can’t really see where to go from here apart from opening this in a pop-up and directing the main window to the “thank you” page – but that is a LAST resort.

Can anyone give any suggestions?

Cheers,

Rich

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:09:35+00:00Added an answer on May 21, 2026 at 3:09 am
    1. You can’t hide a file location. It’ll be plainly visible to anybody determined enough to find it, by the very necessity that the browser needs to know the URL to download the file.
    2. You can’t do it with two header redirects in succession, as you said. You can only redirect to a different page after some timeout using Javascript.

    There really isn’t much choice. If your primary goal is to hide the URL, that’s a lost cause anyway. For good usability, you usually include the plain link on the page anyway (“Download doesn’t start? Click here…”), since the user may accidentally cancel the redirect at just the wrong time to irrevocably kill the download.


    You cannot output anything other than the file itself in the same request/response. You could try multi-part HTTP responses as suggested by @netcoder, but I’m not really sure how well that’s supported. Just start with the assumption that there’s one “wasted” request/response in which only the file is downloaded and nothing else happens. The way things usually work with this restriction is like this:

    • User clicks “download” link or submits form with his email address or whatever is required to initiate the download process.
    • Server returns the “Thank you for downloading from us! Your download will start shortly…” page.
    • This page contains Javascript or a <meta> refresh or HTTP Refresh header that causes a delayed redirect to the URL of the file.
    • The “Thank you” page will “redirect” to the file location, but since this causes the file to download, the page will not visibly change, only the download will be initiated.

    Look at http://download.com for an example of this in action.

    You can make the download location for the file be a script that only returns the file if the user is allowed to download the file. You can pass some temporary token between the “Thank you” page and the file download page to verify that the download is allowed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a question that has been asked many times before, but
Ok I know this question has been asked many many many times before, but
I know this question has been asked so many times before but I couldn't
I know this question has been asked many times before but I tried out
I know this question has been asked before numorous times but they seem to
I know this question has been asked several times, but I can't quite seem
I know this question has been asked many times, but my problem is a
I know that this question has been asked many times before in different guises
I know this question has been asked numerous times but I couldn't find a
NOTE: I know that this has been asked many times before, but none of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.