Greenguy's Board


Go Back   Greenguy's Board > General Business Knowledge
Register FAQ Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 2006-04-25, 02:10 PM   #1
cd34
a.k.a. Sparky
 
cd34's Avatar
 
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
You don't always need php (POTD Script)

Had an issue with a client machine yesterday, load was skyrocketing on their machine due to a large number of hits on a POTD script. The script used readfile to send the thumbnail to the affiliate site and I'm guessing it was done this way because it was modified from an earlier version of the script.

First, the words Picture of the Day mean that you need to make that decision once per day. There is no reason to decide which picture to display on each pageload. A cron job will take care of running things once a day and you can save quite a bit of load on your server.

What their html looked like:

Code:
<a href="http://site.com/potd/index.php?site=asdf&type=large&id=XXXXXX
<img src="http://site.com/potd/index.php?site=asdf&type=thumb1"></a>
The relevent part of the php script

Code:
<?
$site = strip_tags($site);
$type = strip_tags($type);
$id = (int)strip_tags($id);
$type = substr($type,5);
if(($type > 0) && ($type < 5)){
        $open_file = "updates/tn$type$site.jpg";
        header("Content-type: image/jpeg");
        readfile($open_file);
        exit;
}else{
// more code here to handle the display of the fullsize page
Note: I don't like using short tags, i.e. <?, I always suggest that you use <?php. They also made some assumptions that globals would be turned on -- I prefer to use $_REQUEST['id'] or $_GET['id'] just in case the script is moved to a server where globals are not turned on. Having globals on is a security issue and most hosting accounts don't turn globals on. Additionally they did try to do some error handling to make sure bad urls didn't slip through, however, it is preferrable to validate to make sure the values are good, rather than to try and fix values that have been submitted.

Now, they do use a script to rotate in the correct thumbnails once a day, so, they avoid the date calculation on every POTD load, however, readfile in php is slower than apache serving the page. Of course, they could have done a location redirect to the final destination of the page, but, that would have been additional load on Apache.

Most people use mod_rewrite to handle hotlinking, but, that isn't all that mod_rewrite can do. To ease the load and maintain compatibility with all of the other sites out there that had the existing POTD code on their websites, I wrote a rule that would rewrite the url and serve the picture without ever hitting php, but, if the rule didn't match, the php script would handle it. This client had a number of sites using that code, so, there was some validation to do inside the rule to make sure we were sending the data correctly.

The end result was a short mod_rewrite rule that handled the requests that matched the first rule, and dropped through to their script to be handled if the URL didn't match the rules. PHP only gets hit when the request falls through the rule and therefore the load dropped almost instantly.

The rule that eliminated php:

Code:
RewriteEngine on

RewriteCond %{QUERY_STRING} ^site=(site1|site2|3rdsite|another)&type=thumb([1-4])$
RewriteRule ^index.php$ updates/tn%2%1.jpg [L]
This rule looks at the requested image and handles ONLY the display of the thumbnail. It handles:

Code:
<img src="http://site.com/potd/index.php?site=asdf&type=thumb1">
and serves the image:

updates/tn1asdf.jpg

It would have been preferrable for the html code to look something like:

Code:
<a href="http://site.com/potd/index.php?site=asdf&type=large&id=XXXXXX
<img src="http://site.com/potd/updates/tn1asdf.jpg"></a>
completely eliminating the need for php to handle the thumbnail image and allowing the script to handle the fullsized image.

How the rewrite rule works:

Code:
RewriteEngine on

RewriteCond %{QUERY_STRING} ^site=(site1|site2|3rdsite|another)&type=thumb([1-4])$
RewriteRule ^index.php$ updates/tn%2%1.jpg [L]
The RewriteCond looks at the query string portion of the url, the part after the ?, makes sure that it matches a particular format, and then assigns the parts within the () to %1 and %2 through a 'backreference'. The second part matches index.php exactly, and rewrites the url INTERNALLY and serves updates/tn(the thumb #)(the site parameter).jpg and tells apache not to look at any further rules.

If [R] had been set, apache wouldn't have silently served the image but done a redirect to the new location.
__________________
SnapReplay.com a different way to share photos - iPhone & Android
cd34 is offline   Reply With Quote
Old 2006-04-25, 06:23 PM   #2
f69j69b
With $10,000, we'd be millionaires! We could buy all kinds of useful things like ... love!
 
f69j69b's Avatar
 
Join Date: Jan 2004
Location: colorado
Posts: 318
hi cd34 must be nice to have all that knowledge

Fred
__________________
https://furry-yiff.com/
f69j69b is offline   Reply With Quote
Old 2006-04-25, 06:49 PM   #3
pornoTGB
Shut up brain, or I'll stab you with a Q-tip!
 
pornoTGB's Avatar
 
Join Date: Jun 2005
Location: Austria
Posts: 118
Send a message via ICQ to pornoTGB
so what you did is make static images now rather then to create them with php? (btw I have better experience with fpassthru rather then reading and outputing the data)

also I think on most browsers this only affects the first load of the image.. If the URL to the image doesnt change then it should get cached
pornoTGB is offline   Reply With Quote
Old 2006-04-25, 07:13 PM   #4
cd34
a.k.a. Sparky
 
cd34's Avatar
 
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
actually, the images were static before, but served via readfile. fpassthru is indeed better, but, in reality, there was no reason for any of that processing at all.

And if you use fpassthru or readfile, it sets the expire time and is not cached.
__________________
SnapReplay.com a different way to share photos - iPhone & Android
cd34 is offline   Reply With Quote
Old 2006-04-25, 07:21 PM   #5
pornoTGB
Shut up brain, or I'll stab you with a Q-tip!
 
pornoTGB's Avatar
 
Join Date: Jun 2005
Location: Austria
Posts: 118
Send a message via ICQ to pornoTGB
yeah ofcourse they were static before.. what I rather ment was availible staticly for the user
I use this for hotlinking-protection (so you cant even hotlink by using a script..)

it also might have been interesting for you so users with some pc-knowledge cant look at the images that are intended for the next couple days

recreating the images is only needed if it is vital to keep away bandwidth-hogs

about the expire-time: perhaps with special headers...? I'll look that up
EDIT: header("Cache-Control: .........

Last edited by pornoTGB; 2006-04-25 at 07:34 PM..
pornoTGB is offline   Reply With Quote
Old 2006-04-25, 07:47 PM   #6
cd34
a.k.a. Sparky
 
cd34's Avatar
 
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
If you use a script to copy the picture to a predefined location, the source location is hidden.

And typically sponsor programs want their POTD thumbnail to be hotlinkable.
__________________
SnapReplay.com a different way to share photos - iPhone & Android
cd34 is offline   Reply With Quote
Old 2006-04-25, 08:06 PM   #7
raymor
The only guys who wear Hawaiian shirts are gay guys and big fat party animals
 
Join Date: Jan 2004
Posts: 178
Send a message via ICQ to raymor
If you're rotating the pic each day a really simple and fast way is to
use the date variables in rewrite. This example is from a guy who wanted
7 pics, so they would repeat each week. You can change the variable
to rotate through 31 pics, one for each day of the month, or 366 pics,
one for each day of the year. The HTML code:

Code:
<a href="potd.jpg"><img src="tn_potd.jpg></a>
The .htaccess:
RewriteRule ^tn_potd.jpg $ tn_%{TIME_WDAY}.jpg
RewriteRule ^potd.jpg $ %{TIME_WDAY}.jpg

SSI can also be used similarly, by echoing the proper portion of the current
date or time as part of the file name.
__________________
Ray Morris
support@bettercgi.com
Strongbox/Throttlebox & more
TXDPS #A14012
raymor is offline   Reply With Quote
Old 2006-04-25, 08:40 PM   #8
babymaker
Someone Turn Off The Damn Heat!
 
babymaker's Avatar
 
Join Date: Aug 2003
Location: The Sewer....err.philly i mean
Posts: 1,366
Send a message via ICQ to babymaker Send a message via Yahoo to babymaker
Thanx For The Info
__________________
Get ElevatedX W/Hosting 99MO!
babymaker is offline   Reply With Quote
Old 2006-04-26, 06:55 AM   #9
ClickBuster
I'm normally not a praying man, but if you're up there, please save me Superman!
 
ClickBuster's Avatar
 
Join Date: Dec 2004
Location: Bulgaria
Posts: 476
Send a message via ICQ to ClickBuster
Nice quickfix man
ClickBuster is offline   Reply With Quote
Old 2006-04-26, 07:07 AM   #10
ClickBuster
I'm normally not a praying man, but if you're up there, please save me Superman!
 
ClickBuster's Avatar
 
Join Date: Dec 2004
Location: Bulgaria
Posts: 476
Send a message via ICQ to ClickBuster
I have no skills with mod_rewrite

So, I would make changes in the rattion PHP script. On rotation the filepaths would be /update/potd/site1/1.jpg 2.jpg, etc - for direct linking to the image AND I would deff kill the old PHP script.

I think, the way you made your mod_rewrite, if the Query String is:

type=thumb1&site=site1

The rewrite will fail.
__________________
The tendency is to push it as far as you can
-- Fear and Loathing In Las Vegas
ClickBuster is offline   Reply With Quote
Old 2006-04-26, 03:44 PM   #11
Loganp8000
I'm going to the backseat of my car with the woman I love, and I won't be back for TEN MINUTES
 
Join Date: Feb 2006
Posts: 85
I am worried about messing with the mod rewrite too. But i certainly could use a better potd script. Im going to play with this.
Loganp8000 is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:45 PM.


Mark Read
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc