Skip to main content

Posts

Showing posts from August, 2010

.htaccess tips

If you are using smarty in your project, and want to prevent access to template files, add the following lines of code in your .htaccess file of your project. <files ~ "\.tpl$"> order deny,allow allow from none deny from all </files> In place of .tpl, if you write .html, all html files are denied access. If you want to remove .php extension from the url of a page (not the parameters), add this code to your .htaccess file. Options +FollowSymLinks Options +Indexes RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([^\.]+)$ $1.php [NC,L] In this way, a url like http://www.domain.com/page.php will work even if you enter in the url: http://www.domain.com/page

Calculate Age

This class can calculate a person's age and also display values like the number of days and weeks lived. <?php class Age { var $age = ''; function calculateAge($iTimestamp) { $iDiffYear = date('Y') - date('Y', $iTimestamp); $iDiffMonth = date('n') - date('n', $iTimestamp); $iDiffDay = date('j') - date('j', $iTimestamp); // If birthday has not happen yet for this year, subtract 1. if ($iDiffMonth < 0 || ($iDiffMonth == 0 && $iDiffDay < 0)) { $iDiffYear--; } $this->age = $iDiffYear; } function getAge() { return $this->age; } function get_rank($rank) { $last = substr( $rank, -1 ); $seclast = substr( $rank, -2, -1 ); if( $last > 3 || $last == 0 ) $ext = 'th'; else if( $last == 3 ) $ext = 'rd'; else if( $last == 2 ) $ext = 'nd'; else $ext = 'st'; if( $last == 1 && $seclast == 1) $ext = 'th'; if( $las

Generate Random password

<?php /* This class creates random password with alphanumeric values, and its md5 value can be stored in database. Generally useful in cases like when a user forgets his password. */ class PassMd5 { private $password; public function create($pass) { $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; $pass = ''; for($i=0;$i<12;$i++) { $pass .= substr($str,rand(0,62),1); } $this->password=$pass; return $this->password; } } // end of class // example of how to use $newpass=new PassMd5(); echo "New password = ".$newpass->create($pass); echo "<br>(md5) of New password = ".md5($newpass->create($pass)); ?> Output:

AJAX XML example to show selected user details

This example will use the Ajax XML way of sending response. For this tutorial, first create a table, "xml_users" with the following fields: id int(1) NOT NULL PRIMARY KEY AUTO_INCREMENT FirstName varchar(12) LastName varchar(12) Age int(2) HomeTown varchar(15) Job varchar(12) Insert few records in the table. Now write the index.php file, that shows the drop down list of users' names from the table. The onchange event of this select box will fire the javascript function that sends the data to a php file using ajax. Ex: onchange="showUser(this.value)" The javascript: function showUser(str) { xmlHttp=GetXmlHttpObject() var url="responsexml.php" url=url+"?q="+str // q is the id passed url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } /* Now write the span tags for showing the response */ <span id="job">&

Catch value of ajax dropdown

This explains how to catch the selected value of the dropdown box (which came from an ajax response), in our form. Suppose our form has a dependant dropdown, that gets populated from an ajax request to another page. Now how to catch the value selected in this dynamic dropdown ? You can use a function like update_val(this.value) as an onchange event to this ajax dropdown. In this function, which is present on your form page, you can update a hidden field in your form with the selected value from the dynamic dropdown. Because we cannot catch the selected value of the ajax dropdown in our form page directly. Example: We need to display a dynamic dropdown of fruits based on a value selected from the location dropdown in our form. Here, "ajax_selectbox" is the hidden field in the form "form1". form page: function update_val(select_val) { document.form1.ajax_selectbox.value = select_val.value; } ajax page: // sample ajax response of fruits, based on the location selected

Date picker dates Validation

This script validates the start and end dates that are selected from a datepicker. Note that here, the dates are manually taken, but in real time, you get the input values of date1 and date2 ($a and $b in php code) from a date picker. Just use a datepicker and populate the values in input fields. Script code: <script type="text/javascript"> function check_dates() { var a = document.validate_dates.date1.value; var b = document.validate_dates.date2.value; // alert(a); // alert(b); a1 = a.split("-"); b1 = b.split("-"); // alert(a1); // alert(a1.length); var d1 = new Date(); d1.setFullYear(a1[0],a1[1]-1,a1[2]); var d2 = new Date(); d2.setFullYear(b1[0],b1[1]-1,b1[2]); // alert(d1); // alert(d2); if(d2 < d1) { alert("End date cannot be less than start date"); return false; } return true; } </script> Form code: <?php $a = "2010-08-02"; // get it from date-picker $b = "2010-08-01"; // get it from date-picker ?> &