Monthly Archives: September 2011

Get last key from array in PHP

The fastest and the correct way to get the last key from an array is:

$lastkey = array_pop(array_keys($arr));

Example:

$arr[] = array("red" => "apple", "yellow" => "banana");

Let’s say you want to add another element to the previous created array, for that you need to know the last created index so:

$lastkey = array_pop(array_keys($arr));
$arr[$lastkey]["brown"] = "kiwi";

And there you have it.

Prevent hotlinking with apache

To allow all domain except one:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain\.com/$ [NC]
RewriteRule \.(gif|jpg|png)$ http://example.com/hotlink.png [R,L]

This will prevent holinking from the domain offendingdomain.com and serve the image from example.com instead.

Prevent more than one domain:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain\.com/$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain2\.com/ [NC]
RewriteRule \.(gif|jpg|png)$ http://example.com/hotlink.png [R,L]

This will prevent hotlinking from the 2 offendind domains, if you need more just duplicate the second line.

Block all:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com/.*$ [NC]
RewriteRule .*\.(gif|jpe?g|png)$ http://example.com/hotlink.png [R,NC,L]

This will block all hotlinking and and serve the image from example.com instead.

 

NOTE: Be carefull not to rewrite to a image in the same domain as yours or you could create a redirect loop.

for each file in batch/dos

Command line usage:

for /f %f in ('dir /b c:\') do echo %f

Batch file usage:

for /f %%f in ('dir /b c:\') do echo %%f

If the directory contains files with space in the names, you need to change the delimiter the for /f command is using. for example, you can use the pipe char.

for /f "delims=|" %%f in ('dir /b c:\') do echo %%f

If the directory name itself has a space in the name, you can use the `usebackq` option on the `for`:

for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f

And if you need to use output redirection or command piping, use the escape char (`^`):

for /f "usebackq delims=|" %%f in (`dir /b "c:\program files" ^| findstr /i microsoft`) do echo %%f