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

Tuesday, 10 July 2012

attachment opening as zip IE

IE opens the xls,doc and ppt files as a zip file. After extracted also, no files will be there. To avoid this add the below lines in Apache conf. If needed add for other file extensions also.

AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document .docx
AddType application/vnd.openxmlformats-officedocument.presentationml.presentation .pptx
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xlsx

Monday, 9 July 2012

Apache redirect,alias and SSL

1. Redirect /files http://localhost/info.php
if you put the above in the htaccess and hit http://localhost/files. It will be redirected to http://localhost/info.php.

2. Error log in htaccess
php_value log_errors On
php_value error_log php_error_log.txt
The above two lines enable the log and write it to php_error_log.txt

3. Alias example: lets say you have the some files in downloads dir of c drive. But when you hit http://localhost/files, it has to fetch from c:/downloads. The below config achieves that
Alias /files "C:/downloads/"
<Directory "C:/downloads">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

4. If you want to force a particular url to be secure, try below config
<Directory "C:/Program Files/Apache Group/Apache2/htdocs">
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq "www.drupalssl.com"
ErrorDocument 403 https://www.drupalssl.com
</Directory>

Note: After making changes restart apache and clear the browse cache. The redirect directive takes precedence over alias directive.
Reference:
http://www.apache-ssl.org/docs.html#SSLRequireSSL
http://www.askapache.com/htaccess/ssl-example-usage-in-htaccess.html
http://httpd.apache.org/docs/2.0/mod/mod_alias.html
http://www.thewebhostinghero.com/tutorials/apache-alias.html

Force files not to open in browser Apache

Sometimes you may not want to allow the user the to open the file in the browser and provide them option to open or save.Following links will be helpful.
http://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/
       AddType application/octet-stream .pdf
http://www.stuvel.eu/pdfdownload
       SetEnvIf Request_URI "\.pdf$" requested_pdf=pdf
       Header add Content-Disposition "attachment" env=requested_pdf
http://www.thingy-ma-jig.co.uk/comment/5305
      <FilesMatch "\.(?i:pdf)$">
            ForceType application/octet-stream
            Header set Content-Disposition attachment

      </FilesMatch>


Don't forget to enable the mod_header module in apache. If not enabled, will get 500 internal server error.
The first method works only in certain browsers. After doing the changes, always restart the apache and clear the browser cache.

Jquery Autocomplete

Jquery Doc:
http://jqueryui.com/demos/autocomplete/
http://docs.jquery.com/UI/Autocomplete

To perform some action when the item is selected using select event.
http://stackoverflow.com/questions/7733315/jquery-autocomplete-event-select

Normally, autocomplete searches the char or text entered in the entire string. If you want to change this behaviour and match with only the beginning of the string, refer below
http://jsbin.com/avoyu5/edit#javascript,html
http://www.jensbits.com/2011/04/28/jquery-ui-autocomplete-search-from-beginning-of-string/
http://stackoverflow.com/questions/2382497/jquery-autocomplete-plug-in-search-configuration
if you have any issues in using tag, use tag.label or tag.value
http://forum.jquery.com/topic/select-only-items-that-start-with-jquery-ui-autocomplete

Custom Formatting
http://stackoverflow.com/questions/2435964/jqueryui-how-can-i-custom-format-the-autocomplete-plug-in-results

Clear form field after select
http://stackoverflow.com/questions/2561903/clear-form-field-after-select-for-jquery-ui-autocomplete

Thursday, 5 July 2012

click event not working for anchor tags added dynamically

Lets say you are inserting new elements to the DOM and trying to add the onclick event to it, it wont work with just .click function. For that below is the solution.
$('#add').live('click', function(e) {
e.preventDefault();
alert('hello');
});
Reference:http://stackoverflow.com/questions/2264246/jquery-click-event-of-a-tag-added-at-run-time-doesnt-get-fired

Form not submitting on pressing enter

Some browsers have this problem, just add the below piece of code
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
  //call submit function and do whatever you want to
}
Reference:http://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed