maj/100
[PHP] Hotlinking solution for CodeIgniter
I was having lately small problem with ImageSnip.com which I have developed few months ago. Site got some serious popularity in Asia and the images transfer was killing me. As we know, Asia i really big and very populated continent, so It would explain that few images fetched 200.000 times a day, where generating 5GB of traffic daily.
Although the traffic rate is high, most of the images were linked directly to server resources (images hotlinking). Hotlinked images ate my all free bandwidth in 4 days (20GB!) and I need to find a solution to that …
Solution to avoid hotlinking images is to place in images folder a .htaccess file. Although there are toms of examples, most of them don’t work for CodeIgniter framework (as ImageSnip is developed in), especially when developer uses „static” folder to host the css,js,images content…
After hour of browsing I’ve found solution(s) at Jeff Starr blog located at: http://perishablepress.com/press/2007/11/21/creating-the-ultimate-htaccess-anti-hotlinking-strategy/ which AT LAST WORKS! Code below:
###############################
# ultimate hotlink protection #
###############################
# disable directory browsing
# uncomment this option to protect access to directories
# Options -Indexes
# enable the following of symlinks
# uncomment this option if hotlink protection fails to work
# Options +FollowSymLinks
# verify presence of mod rewrite
<IfModule mod_rewrite.c>
# enable the rewrite engine
RewriteEngine on
# check that file exists
RewriteCond %{REQUEST_FILENAME} -f
# check for requested file types
# include additional file types here
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
# allow blank referrer requests
RewriteCond %{HTTP_REFERER} !^$
# allow all requests from your domain
# edit the domain to match your own
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?domain\. [NC]
# allow all requests from your ip address
# edit the ip address to match your own
RewriteCond %{HTTP_REFERER} !^https?://123\.123\.123\.123(.*)$ [NC]
# additional site access
# include additional sites here replace domain names and or
# remove unnecessary lines or add new lines for more sites
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?domain_01\. [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?domain_02\. [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?domain_03\. [NC]
# search engine access
# include or remove search engines feed readers and other sites
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteCond %{HTTP_REFERER} !google\. [NC]
RewriteCond %{HTTP_REFERER} !yahoo\. [NC]
# allow access to all requests for your anti hotlink image
# to serve an image instead of delivering a 403 error
# uncomment the next line and edit the path and file name
# RewriteCond %{REQUEST_URI} !^/hotlink\.jpg$ [NC]
# deliver the hotlink image for all requests for the listed files
# protect additional file types by editing the list below
# if you are serving a file instead of a 403
# uncomment the next line and edit the path and file name
# RewriteRule \.(gif|jpe?g?|png)$ http://domain.tld/hotlink.jpg [R,NC,L]
# return a 403 error when any of the following file types is requested
# you can protect additional file types by editing the list below
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
# close the module container
</ifModule>
As Iam no expert in .htaccess file configuration, this one takes the cake!
kwi/100
[C#] Row select on right click (context menu)
When using DataGridView control, activating it’s context menu (by right click on the grid) will not select the row/cell you are clicking at. For most of the operations users right-clicking on the grid want to perform operation on that selected item.
Solution to this problem is to select item at same time as the Context menu is shown by using Opening event of the context menu. Following code should be pasted into the Opening event handler function:
Point p = _myGRID.PointToClient(Cursor.Position);
DataGridView.HitTestInfo hti = _myGRID.HitTest(p.X, p.Y);
if (hti.RowIndex > -1)
{
_grid_Feeds.ClearSelection();
_grid_Feeds.Rows[hti.RowIndex].Selected = true;
}
The right clicked row will be selected and from now on you can get the bound object by _myGRID.SelectedRow .
mar/100
[PHP] Undefined index: lastBuildDate error fix
While working with lastRSS PHP lib I stumbed upon a following error:
Undefined index: lastBuildDate / lastrss.php / Line Number: 160
As far as I googled around I didn’t find ONE precise fix for it, so I tinker with it and eventually It worked. Probably it’s solution for similar problems in other libs…
Fix:
In lastrss.php find (around line 160)
if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
// convert lastBuildDate to specified date format
$result['lastBuildDate'] = date($this->date_format, $timestamp);
}
Trick is that PHP5 does not allow (or does not allow us to ignore that) access indexes in arrays that are not declared (or something…). This is PHP5 specific. Anyway fix for this is pretty simple, all what we need to do is to check if such result is defined whith isset() function. Correct code should look like this:
if (isset($result['lastBuildDate']) && $this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
// convert lastBuildDate to specified date format
$result['lastBuildDate'] = date($this->date_format, $timestamp);
}
In lastRSS lib (which is cool RSS parsing library for PHP) this occurs when two things get together:
- You define own date_format (and don’t leave it empty as default)
- You fetch RSS for the first time (before the cache is written). Any another attempt on fetching feeds from same rss address will not cause this error to appear, as long as the cache will be read. If you clear the cache error will come again…
mar/100
[Web] Can’t login to Live.com mail
I’am not a big fan of web based mail clients. I use them when I don’t have access to my primary systems (for ex. in cafes/PDA) but on my computers I’ve got installed standard stand-alone app mail client similar to Outlook (The Bat). I’ve recently created a mail account at live.com (Microsoft hosted mail system). Logging thru web mail does not make any problems. Although setting the account in TheBat was a problem. When I configured everything I get error:
-ERR authentication failed
I was sure that provided config (server, ports, user name) was correct. After several attempts I found out that live.com services REQUIRES max 16 symbol passwords and my password was longer …
Shorting password length to 16 symbols does the trick and allows user (user@live.com) to login …
paź/090
[C#] Własny ciąg wynikowy z daty i czasu
Tabela ze skrótami umożliwiającymi formatowania daty i czasu z wykorzystaniem…
DateTime.Now.ToString("ddMMyyyyHHmmss") ;
// Wynik: 01102009094315
… znajduje się tutaj: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx