Forum Moderators: coopster

Message Too Old, No Replies

Test if a url with parameters exists

is all I need to complete a gzip script

         

Winnetou

1:31 am on Apr 19, 2008 (gmt 0)

10+ Year Member



Hi,

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!

MattAU

3:06 am on Apr 19, 2008 (gmt 0)

10+ Year Member



You can't use parameters in file paths, only urls.

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]

Winnetou

4:46 am on Apr 19, 2008 (gmt 0)

10+ Year Member



Hi MattAU,

can you elaborate on using "url wrappers" please? I have not heard of this concept before. What I've found through Google is that it includes to use

@ini_set(‘user_agent’, $userAgent);

but I'm not sure why and what it does.

MattAU

8:50 am on Apr 19, 2008 (gmt 0)

10+ Year Member



Allow URL wrappers (actually called allow_url_fopen) is a php setting which decides whether or not you can use functions like fopen, file_get_contents etc. to open remote / http files.

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.

Winnetou

1:03 pm on Apr 19, 2008 (gmt 0)

10+ Year Member



ini_get('allow_url_fopen') returns "1" for my host :-)

But when I precede $file in the routine above with the full URL ('http://www.mysite.com/') it still fails.

Do you say that it should go and read the URL just as if it was pasted into a browser's address bar?

MattAU

11:45 pm on Apr 21, 2008 (gmt 0)

10+ Year Member



Yep, pretty much. However it's not perfect and doesn't actually emulate what a browser does - it doesn't send the same headers as a browser would, so a site may not respond in the same way.

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.

Winnetou

12:52 pm on Apr 22, 2008 (gmt 0)

10+ Year Member



Progress!

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?

MattAU

10:18 am on Apr 23, 2008 (gmt 0)

10+ Year Member



I'd say the infinite loop problem is caused by using a url in file_get_contents.

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.

Winnetou

7:46 am on Apr 25, 2008 (gmt 0)

10+ Year Member



So I guess I've got a catch-22 here: If I use urls to verify if a file/link exists I get a .htaccess infinite loop, if I use the file format I cannot verify any of my gallery links.

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]

PHP_Chimp

9:26 am on Apr 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried ob_gzhandler [us3.php.net]?