Forum Moderators: coopster
I'm currently developing a script which acts as a gzip compression for my site since my host does not support mod_gzip.
Everything's fine except if users click on a url like this:
h*tp://www.mysite.com/gallery/index.html?image=1
where Javascript evaluates the parameter and returns the corresponding image in the page.
In my PHP script I do the following before I start compressing:
// Get the url
$file = $_SERVER["REQUEST_URI"];
// Trim the string down to the format 'path/to/file.html'.
$file = ltrim($file,"/");
// If the url points to a directory, or if it's the root dir, add 'index.html':
if ( (substr($file, -1) == '/') ¦¦ ( strlen($file) == 0 ) ) $file .= 'index.html';
// If the file or url does not exist, raise a 404 header and return error file:
if ( !file_exists($file) ) {
header( "HTTP/1.1 404 Not Found" );
$file = "error/error404.html";
$output = file_get_contents($file);
echo $output;
exit;
}
// Get the file's content:
$file_contents = file_get_contents($file);
[...]
When the user clicks on a url as the one above the script always outputs a 404 error because it cannot find the file 'gallery/index.html?image=1'.
I was thinking of removing the parameter, testing for the file as such and then reassembling the parameter for 'file_get_contents', but it seems that function cannot process parameter urls either.
Do you know what I could do? How can I test if a parameter-laden url is valid or, what PHP function can check such a url?
Thanks!
One option would be to use file_get_contents (with url wrappers on) to open http://example.com/gallery/index.html?image=1.
I doubt images would be compressed much by gzip (they're already compressed as jgeps, figs etc), perhaps you could skip the compression for them?
[edited by: dreamcatcher at 2:00 pm (utc) on April 23, 2008]
[edit reason] use example.com. Thanks. [/edit]
It's up to your host whether they allow it or not, some don't due to security concerns. Try it, if you don't get any errors then it works.
I think you're better off approaching this in a different way. Opening the page via http, reading the page then compressing it and sending it to the user isn't very efficient. Have a think about incorpating compression into the actual image.html file or creating a function to load the data from image.html and add the extra ?image=1 data from that function.
I've tested and tried and found that I actually had an infinite loop in my setup (I used a session variable to confirm).
So the problem seems to be as follows:
1. I enter a page in the browser
2. server receives request, processes htaccess
3. htaccess says:
AddHandler doGZip .html
Action doGZip /gzip.php
4. server processes gzip.php (which I partly posted in my original post)
5. server finds $file_contents = file_get_contents($file);
6. server sends http request => goto 2.
My curren gzip.php now looks as follows:
$file = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];// If the url points to a directory, or if it's the root dir, add 'index.html':
if ( (substr($file, -1) == '/') ¦¦ ( strlen($file) == 0 ) ) $file .= 'index.html';// Get the file's content:
$file_contents = file_get_contents($file);
(I've temporarily disabled the file exists check.)
I feel I'm closing in on a solution. If someone could help me solve that infinite loop I'd be very happy.
MattAU, could you expand on how you would code a function to "add" the url parameter?
Someone requests a page (an .html file, lets say index.html), Apache sends the page to PHP which then requests that page via file_get_contents, which Apache treats as any other request, which in turn sends it to php again which requests the index.html page from Apache again etc... :)
I was pretty unclear with the 'add' function... Basically what I meant was something that loads the page locally and creates the php page as the original function / page did... Maybe useful, maybe not depending on how the image gallery script is currently written.
Can anyone offer me help to solve this dilemma?
Thanks!
[edit: I open a new thread in the Apache Webserver forum]
[edited by: Winnetou at 7:51 am (utc) on April 25, 2008]