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 

Saturday 2 June 2012

Date difference javascript

I want to compare the date selected from datepicker and the current date. Though everything seemed right, i didn't get the correct output. The reason is the date from datepicker contains time as 00:00:00 where as the currentdate contains the proper time. Set the time as zero using setHours().
date1 = new Date();
date1.setHours(0,0,0,0);
Reference: 
http://stackoverflow.com/questions/2698725/
comparing-date-part-only-without-comparing-time-in-javascript 

NuSOAP charset encoding issues

Once when I used NuSOAP, I faced problem in the display of chinese characters. They are displayed as question marks. Finally , I was able to crack it by setting the below varaible to false.
$soapClient->decode_utf8 = false;
Reference: 
http://stackoverflow.com/questions/5317858/nusoap-and-content-type
http://forum.nusphere.com/nusoap-with-utf-8-t4974.html 

GMT time in PHP

Sunday 27 May 2012

DirectoryIndex and AddType - Apache

The DirectoryIndex directive sets the list of resources to look for, when the client requests an index of the directory by specifying a / at the end of the a directory name.

DirectoryIndex index.html index.php /cgi-bin/index.pl
would cause the CGI script /cgi-bin/index.pl to be executed if neither index.html or index.php existed in a directory.
You can write PHP code in a file with extension other than .php but still the server recognises and execute it. This is possible if you add the below line in apache conf file  

AddType application/x-httpd-php .bop .foo .133t
AddType application/x-httpd-php .asp .py .pl
AddType application/x-httpd-php .htm .html

Reference: http://php.net/manual/en/security.hiding.php
 

Wednesday 21 March 2012

Excerpts from Robin Sharma's books

  1. A bit of fragrance clings to the hand that gives flowers.
  2. As Gandhi stepped aboard a train one day, one of his shoes slipped off and landed on the track. He was unable to retrieve it as the train was moving. To the amazement of his companions, Gandhi calmly took off his other shoe and threw it back along the track to land close to the first.
    Asked by a fellow passenger why he did so, Gandhi smiled. "The poor man who finds the shoes lying on the track," he replied, "will now have a pair he can use."
  3. People asked Thomas Alva Edison why he didn't invent a machine to help him hear. And he always replied that "a man who has to shout can never tell a lie".
  4. Kaizen - continuous improvement.
  5. One night a father was relaxing with his newspaper after a long day at the office. His son, who wanted to play, kept on pestering him. Finally, fed up, the father ripped out a picture of the globe that was in the paper and tore it into a hundred tiny pieces. 'Here son, go ahead and try to put this back together.' he said, hoping that this would keep the little boy busy long enough for him to finish reading his paper. To his amazement, his son returned after only one minute with the globe perfectly back together. When the startled father asked how he achieved this feat, the son smiled gently and replied 'Dad, on the other side of the globe there was a picture of a person, and once I got the person together, the world was okay.'"
  6. king Midas loved gold so much he prayed that everything he touched would turn to gold. When his wish was granted he rejoiced. That was until he realized that he couldn't eat because his food had turned to gold and so on, so forth."

Tuesday 20 March 2012

Edit button missing google blogger

rRecently Google started routing users to country-specific URL. i.e For example if you are in India and trying to use example.blogspot.com , it will be redirected to example.blogspot.in. This affects the normal functioning of the site like the edit option for post is missing. To prevent this we can use a flag NCR(No country redirect) in the URL like this http://example.blogspot.com/ncr.
Reference: http://support.google.com/blogger/bin/answer.py?hl=en&answer=2402711

Lightbox for ajax loaded content Drupal

Normal way of implementing lightbox does not work out for the onclick event of ajax loaded content. For that we should do the below thing
$(document).ajaxComplete(function() {
   Lightbox.initList();
});
Reference: http://drupal.org/node/282089