Download and "thank you" page in one click - php

I have a php-apache website on which I am trying to track download conversions using Google Analytics. I want my users to initiate the download and be redirected to a "thank you" page in one click. The way I'm envisioning this is:
The user clicks one of several download buttons which sends them to a generic thankyou.php page, and passes a variable telling that page which file to give them. Thankyou.php contains a header which uses that variable to start a download dialogue.
If there are better ways to do this, I am open to anything. To my bewilderment, I haven't found a good way to do this after several hours of poking around here and on Google.

Send the file after the ThankYou page has been loaded.
You could either use an iframe for this (if this is allowed in your (X)HTML variant), e.g.
<iframe src="download.php?id=123" style="display:none;" />
or a meta refresh, e.g.
<META http-equiv="refresh" content="1;URL=download.php?id=123">
or use JavaScript or whatever is able to call a URL. You could then write something like your download should start automatically in a second. If not please click this link, where the link is href'ed to download.php?id=123 as well.
In download.php you'd just send the regular headers for sending a file and pass it to the client. Check this question's answers to see how.
Also see this related question.

Related

Refresh Page with Status Code after Content was loaded

I am building a file manager with php. Every request is handled by a php script, i.e. also direct download paths will be pre-checked so I can prohibit certain downloads and also display other content.
My problem now is, any here comes stackoverflow into play, that I do not know any solution how I can prepare the download aka display the content first, then refresh the page once so I can send the file ... but without side effects.
Refreshing is not a problem of course, but no refreshing solution works so far. Refreshing by header() keeps my page from displaying any content, refreshing with or javascript works regarding the display of the content, however, downloads with e.g. wget do not work anymore.
The problem in other words is:
Due to the prior content load a valid status code will be sent, thus programs like wget or curl will not follow the refresh, so it will not download the file, only the html.
To get the actual file with curl or wget you need to send http headers, however, the 3XX redirection status codes will need a value for Location. If header('Location: ..') is sent the website will not be displayed before the refresh.
A solution is required that sending appropriate status codes is possible and the content will still be loaded prior the file distribution.
One partially solution has been found with status code 206, which does not require a location value and still works with wget. curl actually does not. so maybe a better solution is still available?
Thanks for your help!
What about the <meta> tag?
<meta http-equiv="refresh" content="5" URL="http://your-url.com/redirect/">
Write this statement after printing content and it will wait 5 seconds before redirecting
<meta http-equiv="refresh" content="5;url=http://your-url.com/redirect/" />

Make a #-tag javascript link word for non javascript

Using the following tutorial I want my website to use AJAX to load the content (but also want to be able to use the back button etc. etc):
http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp
Ofcourse if someone has javascript disabled the website should also work (without Ajax).
The problem however comes when a javascript enabled user sends a link to a non javascript enabled user. Because javascript is disabled it will not handle the #-tag correctly and will just go to the homepage (so linking directly to pages from a javascript user to non-javascript user is impossible). Is there a way to resolve this issue (preferably php or htacces).
HTML5 gives us methods to alter the URL without refreshing the page https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries
This means you can update something without a page refresh but still give the user a url they can bookmark or send to someone else. These urls will work without JavaScript, as long as you have pages at those locations or are catching them with mod_rewrite or similar.
https://github.com/browserstate/history.js is a great little pollyfill which will use the HTML5 history stuff if the browser supports it, otherwise (Internet Explorer) it changes the hash of the url.
Basically, three steps:
code your "a" tags just normal: <a href='about'>About us</a>
in your javascript code, intercept all click events on <a> tags and navigate to # + this.href. So when they click the above url, you navigate to site.com/#about instead of site.com/about
in your javascript code, have a timer function that reads the hash value form the current location and loads a corresponding url (with # removed) via ajax
Since you code your html just as usual, the site remains fully accessible for non-js users, and, more important, for search engines' bots.
In response to the comments I can suggest the following:
redirect your home page via javascript from just site.com to site.com/js/
when <a href='about'> is clicked, navigate to site.com/js/#about
on the "js" page, have something like <a id=about href="/about">click here</a> for non-js users
Why not just build your application normally and then add the AJAX on top, rather than going the other way round and causing more work for yourself?
Ask yourself, why do you need AJAX page transitions? Does your app actually need them, or is it just because you've seen it on another site, like Twitter?

Prompting a file download on my web page

I wanted to serve a download of my program on special download page. Something like: http://site.com/download, where there will be a standard HTML page and also a file download prompt. I was wondering how I'd go about implementing this.
The only way I can think of is having a hidden iframe in the page pointing to the file the user wants to download. I also know of the PHP function readfile() but I don't see how I can implement that on the page aswell as have a HTML output shown to the user.
Any help is appreciated, thanks.
You can use a META redirect, which since it points to a download will not leave the page you're on.
On your HTML page, try including something like this:
<META HTTP-EQUIV="REFRESH" CONTENT="1;URL=/download/sitefile.zip">
This will browse to the file after 1 second, which should prompt the visitor to download it without leaving the page.
You can have the site http://example.com/download this can be normal html site. With redirection after few seconds (can be done in js or meta).
It should redirect to PHP site with fpassthru() function in it. That way you can easily implement additional security in the PHP.
HINT: make sure to set proper HEADERS in the PHP file so browsers will start download the file instead of showing the content in browser screen.

Grab the page which is redirecting

I am trying to grab a file .pdf from a server. There is a hyperlink at the page, by clicking that link it goes to a page, it checks for some privileges, then it redirects to another page which shows the content of the .pdf within an Iframe.
lets say beginning url is http://site.com/docs/1.pdf
on click it goes to another page, then another one and it comes whth the last page
http://site.com/viewer/pdfs/1.pdf
the last page shows the pdf content within an Iframe.
I realized that the software IDM (Internet download manager) can follow the redirections and download the file by clicking the first link.
I was wondering if there is an algorithm or library or class or hint that I can figure out how to do that in PHP scripting.
by the way, once I wrote a code to read the header of the page and I could redirect to the second page, but I want to know if there is a general algorithm for this or not.
If you are doing the HTTP stuff manually, check for 30x statuscodes and the Location header.
However, you could simply use CURL and set CURLOPT_FOLLOWLOCATION.
Yes, just like ThiefMaster said, you could look for the Location header.
Have a look here, maybe this can be a help to you:
http://codesnippets.joyent.com/posts/show/1214 This function retrieves file size of a remote file, why don't you try to change it slightly so that it gets the final URL?

PHP Zip code matching

How can I have it if a zip is entered that I have a page for go to the page for the zip and if not then go to another page? I'm new to PHP and don't know how to start.
The simplest solution is to do the following:
Submit the form to a php script
From the php script get the Zip code from the form and check to see if you have the page for it (using file_exists maybe)
If you have a page then send the Location header to redirect to that page:
header("Location: /path/to/zip/pages/$ZipPage");
If not then redirect to your alternate page (using the method above.
You can achieve the same effect by including the page you want directly but that will prevent the user from bookmarking the page.
If you also need to forward the form onto the next page then that'll probably mean the alternative solution would be better (there are ways around the bookmarking problem though which I can go though if you would like)
Check out the php manual. The section your looking for is on html forms processing.
http://us.php.net/tutorial.forms

Categories