Friday 3 August 2012

Apache basic authentication

To create a password file, in cmd prompt go to the bin dir where htpasswd.exe is present and type the below cmd

htpasswd -c test test

third parameter is name of the file where credentials are stored.
fourth parameter is the username. After executing this command, it prompts for password and a file named test will be created in bin dir.
The below config is placed in the conf file
<Directory "C:/Program Files/Apache Group/Apache2/htdocs/test" > // file path where authentication is needed
AuthType Basic
AuthName "Private Documentation Repository" // can be anything
AuthUserFile "C:/Program Files/Apache Group/Apache2/bin/test"  //path to file containing username and password

</Directory>
Restart the apache and restart the browser and try hitting , http://localhost/test. It asks for credentials.
Reference:
http://httpd.apache.org/docs/2.0/howto/auth.html
http://www.askapache.com/htaccess/apache-authentication-in-htaccess.html

Thursday 2 August 2012

Prevent hotlinking Apache

Sometimes other sites use images,video etc from your site. This is called hotlinking or bandwidth theft.
Hotlink protection can save you lots of bandwidth by preventing other sites from displaying your images. For hotlink protection, you need to create some rules in Apache .htaccess file. Put the below lines in .htaccess.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?drupalssl.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?drupalsample.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

The above rule prevent use of images having extensions jpg,jpeg,png,gif other than drupalssl.com and drupalsample.com. Even if they use it, they will just see the image placeholder. if you want to show some other image when they try to hotlink, last line can be changed to.
RewriteRule \.(jpg|jpeg|gif)$ http://www.drupalssl.com/Test3.png [NC,R,L]
References:
http://altlab.com/hotlinking.html/
http://www.htaccesstools.com/hotlink-protection/

Setting up PHP mail locally

Want to set up a mail functionality locally in PHP. Follow the below steps.

1. Download sendmail from http://glob.com.au/sendmail/
2. Follow the instructions in this link. http://www.jacmoe.dk/how-to-send-test-emails-using-php-mail-from-your-local-wamp-installation. http://serverfault.com/questions/105403/how-do-i-set-up-php-to-send-email-on-apache-windows