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?
Related
Let's say I have the following link:
www.blahblah.com/#!?page=index
How can I convert it to one of the following:
www.blahblah.com/#!/index (this one should be made with mod_rewrite)
www.blahblah.com/ajax/index (still mod_rewrite, but #! replaced with ajax)
www.blahblah.com/index (the page will load with AJAX like facebook, but #! will be hidden)
Can anyone give me examples of each of the questions above?
Thanks alot!
Anything after the hash (#) isn't sent to the server, so you cannot read it server-side. You can, however, redirect the user using JavaScript. The information you're looking for will be stored in the variable window.location.hash.
On page load, you can do something like the following:
hashString = window.location.hash.substring(8);
window.location = 'http://www.blahblah.com/'+hashString;
We're using substring to remove the first eight characters (#!?page=), so we'll be left with index.
Module rewrite only changes what the server sees. Module rewrite can't, change what the local browser sees, which is where the js is being run.
The way Facebook load, is through requesting the contents of the new page, then it updates the window URL instead of having to re-load everything. This is done, so If an item needs to be shared or linked the link is all up to date with what they're actually viewing, so when the page gets a fresh re-load, the browser loads the actual full php page, requested from the server.
The hidden # in a ajax page loading strategy is done by HTML 5 pushState.
In javascript you can use window.location.hash for this.
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.
I have a php page . I need to make it hard for user to get direct download link.For this i need a js function which start downloading pdf after 10sec automatically after page load. I dont want to provide a download link at all. Also I cant use onpageload . PDF must download.
Since most of the browsers will tell you where the downloaded file came from, i think you may want to mask the file itself behind a 'temporary' link with mod_rewrite or other custom parameters. You don't need to use JavaScript for this.
After then you can simply push the file with PHP similarly like this solution.
There is absolutely no way to hide a URL from an end user - All they need to do is use Fiddler 2, Firebug or similar tools to view the requested URL
Have your download page redirect to the PHP file that will download it. If it can download it, it will redirect the user back to the previous page, because the download doesn't have the right content type, although it could be just a plain .html file. You don't need Javascript to do this:
<meta http-equiv="refresh" content="10;url=http://mysite/d.php?file=resume">
I recommend the Smart File Download, from zubrag.com, if you don't already have a PHP file specifically for downloading.
I'm trying to explain as best as I can, sorry for my English.
I have a list of links, each linked to a php file with an id by parameters (ex. download.php?id=1 or ?id=2 and so on).
This file create a new instance of a class witch return the correct header of the files so it displays the save dialog box of the browser.
Now I need to check if the files is already downloaded in past (The first time you downloaded it I add a field on the mysql db).
This checks go ahead if you haven't download the files, else return false.
Here is the problem, when it returns false or something else the browser redirect me to the download.php file, so I get a blank page or what I'm echoing.
I need that if the file is already download it show me a js alert for advice ppl.
Hope you can understand what i mean.
Thanks for help
Technically you can without ajax, the download.php can output the following if the user has already downloaded the file:
<script>
alert('It was false - you already have the file!');
window.back();
</script>
Just depends how well it integrates with your site. Not tested but thats the general idea.
it's too late to show a js alert in that case, you'd need to do something like ajax to check whether the file has been downloaded, and then show the alert or start downloading the file then.
once your web browser has started loading a new url (eg download.php) then it is too late, you are already navigating away from the current page.
So you've got a page that looks like:
1. file1
2. file2
3. file3
and they've got links to the download script on each. If you want to prevent multiple downloads, you do it in two places. Here on the main file list page, and on the download.php page. When the user clicks on one of the files, you have an onclick handler remove the link from the clicked file. This can be done by refreshing this page (and simply not adding the download link when the page is generated), or using some DOM manipulation to remove the tag around the filename.
The download page will also do checks if the file's been sent previous and can handle that condition itself.
Doing so in both places will degrade nicely if for whatever reason the client doesn't have Javascript enabled.
You need to handle it in two places for the best behavior.
Determine if the user can download the file when you generate the download page, and don't create links for files that can't be downloaded.
To handle the case of someone having multiple windows open or otherwise downloading a file without reloading the downloads page, check if the user can download the file in download.php before sending any headers. If he can't, send a redirect back to the download page:
header("Location: downloads.php?error=repeat_download");
exit;
…and use the error parameter to include a message at the top of the file list explaining what happened.
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