<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Php Development &#187; PHP/mysql</title>
	<atom:link href="http://www.maheshchari.com/category/php-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.maheshchari.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 17 Jan 2012 02:42:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>sending email with PHP (2)</title>
		<link>http://www.maheshchari.com/sending-email-with-php-2/</link>
		<comments>http://www.maheshchari.com/sending-email-with-php-2/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 14:15:01 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[email]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=715</guid>
		<description><![CDATA[Configure PHP email function for windows server. We learnt about sending mail from PHP with linux sendmail in this series of tutorial. On windows we don&#8217;t have pre configured email sever on mostly server. to fix this PHP mail function on windows, we can do with ini_set php function or setting it up on php.ini. [...]]]></description>
			<content:encoded><![CDATA[<h1>Configure PHP email function for windows server.</h1>
<p>We learnt about sending mail from PHP with linux sendmail in this series of <a href="http://www.maheshchari.com/sending-email-with-php-1/">tutorial</a>. On windows we don&#8217;t have pre configured email sever on mostly server. to fix this PHP mail function on windows, we can do with <strong>ini_set</strong> php function or setting it up on php.ini.</p>
<h2>Setting php.ini </h2>
<p>if you have access to php.ini ,you can change the default global value .</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">[mail function]
SMTP = localhost
sendmail_from = me@example.com</pre></div></div>

<h2>Setting at run time ini_set </h2>
<p>we can change this configuration at run time with <strong>ini_set</strong> function as given below. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SMTP'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sendmail_from'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fromaddress@domain.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Example</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SMTP'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sendmail_from'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fromaddress@domain.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
&nbsp;
<span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'toaddress@domain.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Sample  subject'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'message body '</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=715&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/sending-email-with-php-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>sending email with PHP (1)</title>
		<link>http://www.maheshchari.com/sending-email-with-php-1/</link>
		<comments>http://www.maheshchari.com/sending-email-with-php-1/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 17:10:57 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[email]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=710</guid>
		<description><![CDATA[How to send email in PHP Email ( Electronic mail ) is most popular communication service on internet . Generally it is common practice to use this service as marketing media for digital products. There are a lot of free email service available on internet like Gmail, Yahoo,AOL and more. So as a developer we [...]]]></description>
			<content:encoded><![CDATA[<h1>How to send email in PHP</h1>
<p><strong>Email</strong> (<strong> Electronic mail </strong>) is most popular communication service on internet . Generally it is common practice to use this service as marketing media for digital products.   There are a lot of free email service available on internet like <strong>Gmail</strong>, <strong>Yahoo</strong>,<strong>AOL</strong> and more. </p>
<p>So as a developer we must know the <strong>mail</strong> functionality in PHP like <strong>send mail</strong> with php and <strong>read mail</strong> with PHP imap ,pop functions. in this tutorial we are going to learn how to send the mail with different MIM types and it mail components. </p>
<p>An email message consists of three components, the message <em>envelope</em>, the message <em>header</em>, and the message <em>body</em>. envelope controls the mail sending path like where it originated and where to be sent the mail. header part will denotes the contents of the email and sender and receiver information. body part contains the main user data information.In this series mail tutorial we learn with different PHP functions to send the mail.</p>
<p>On Linux server by default PHP comes with <strong>send mail</strong> or <strong>exim</strong> email server. But on windows PHP must be configured to allow from <strong>SMTP</strong> . </p>
<p>Main PHP <strong>extensions</strong> for email handling are </p>
<ul>
<li>Cyrus </li>
<li>IMAP </li>
<li>Mail</li>
<li>Mailparse</li>
<li>vpopmail</li>
</ul>
<h2>PHP email function</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mail</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$to</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$message</span> <span style="color: #339933;">,</span><span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This function takes mainly four parameters, to,subject, message, headers.It will return a boolean that refers if mail is sent successfully or not. </p>
<h2>$to parameter</h2>
<p>$to parameter is a string to recipients of the email , we can give the &#8216;,&#8217; comma separated email address. it can be send for more than one recipients at at time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&quot;To Display Name1&quot; &lt;reciever@domain1.dom&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$to</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">',&quot;To Display Name2&quot; &lt;reciever@domain2.dom&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<h2>$subject parameter</h2>
<pre>$subject="This is my sample subject.";</pre>
<p>$message parameter contain any length of string that is sperated with new line characters . </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Sample message line 1 <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$message</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;sample message line 2&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
<h2>$header  parameter</h2>
<p> is the modifier or extending the mail sending option.It has many options ,but we will discuss importent ones here. see below snippet for example.</p>
<pre>
$headers = 'From: "Sender Display Name" <sender@sender.dom>' . PHP_EOL .
'Cc: "CC Display Name" <info@domain.dom>' . PHP_EOL .
'X-Mailer: PHP-' . phpversion() . PHP_EOL.
'MIME-Version: 1.0' . PHP_EOL. 'Content-type: text/plain; charset=UTF-8' . PHP_EOL.
'X-Priority: 1 (Higuest)'.PHP_EOL.
'X-MSMail-Priority: High'.PHP_EOL.
'Importance: High'.PHP_EOL.
'Reply-To: info@example.com'.PHP_EOL.
'Return-Path: info@example.com.PHP_EOL.
'Message-Id: <' . md5(uniqid(microtime())) . '@' . $hostname . '>'.PHP_EOL.
           </pre>
<p>In the above code <strong>PHP_EOL</strong> is PHP constant that represent new line character . </p>
<h2>Main list of email headers</h2>
<p>&#038;nbsp</p>
<p><strong>From,</strong> email header notes from what email address is sent. </p>
<p><strong>Cc</strong> , this email header inform the server that this mail also can be sent to this email address also.</p>
<p><strong>X-Mailer,</strong> this email header inform the mail recipent client about the sender client. </p>
<p><strong>MIME ,</strong> this email header tells about the how the mail should be handle like plain text,html and attachment.</p>
<p><strong>X-Priority,</strong> tells the priority of the email for generel server.</p>
<p><strong>X-MSMail-Priority</strong>, tell the priority of the email for windows server hosted email servers.</p>
<p><strong>Importance</strong>, tells the priority of the email this will work for some of the email server.</p>
<p><strong>Reply-To</strong>, if user reply the sent email ,what the email address it to be taken, if it is not given default is From ,email header.</p>
<p><strong>Return-Path</strong>, if an email is bounced, where to be notified , this can be same email address like From or other email also.this email header can be used to track some email campains .</p>
<p><strong>Message-Id</strong>, is optional email header if we do not provide server will automatically generates and sends. this header can be used to track email campaings also.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=710&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/sending-email-with-php-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php get url</title>
		<link>http://www.maheshchari.com/php-get-url/</link>
		<comments>http://www.maheshchari.com/php-get-url/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 20:36:03 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=669</guid>
		<description><![CDATA[How to get current page full URL with PHP In mostly case we might be need current url of the page with PHP in user log in authentication functionality. when a user go for log in page, after successfull log in the user we should return the user to same page where he came from [...]]]></description>
			<content:encoded><![CDATA[<h1>How to get current page full URL with PHP<br />
</h1>
<p>In mostly case we might be need current url of the page with PHP in user log in authentication functionality. when a user go for log in page, after successfull log in the user we should return the user to same page where he came from . this is same for log out scenario also. We need this current page url in cross site cummunication also. So i wrote a small php function to get the current url of the working page. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> current_url<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$serverrequri</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'PHP_SELF'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$serverrequri</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #009933; font-style: italic;">/**
     * server request uri depends upon server OS version
     */</span>
    <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTPS&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">''</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTPS&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;on&quot;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;s&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009933; font-style: italic;">/**
     * get protocal if SSL enabled.
     */</span>
    <span style="color: #000088;">$serverprotocol</span> <span style="color: #339933;">=</span> strleft<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_PROTOCOL&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$serverport</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_PORT&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;80&quot;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_PORT&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Page might be run with IP and Port also like 127.0.0.1:8080
     */</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$serverprotocol</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;://&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$serverport</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$serverrequri</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>By calling above function you get the <strong>current page url </strong>and you pass this url to log in and log out page with return get parameter with <strong>base64_encode</strong> and send back user to by <strong>base64_decode</strong> .</p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=669&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/php-get-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>md5 php</title>
		<link>http://www.maheshchari.com/md5-php/</link>
		<comments>http://www.maheshchari.com/md5-php/#comments</comments>
		<pubDate>Sun, 08 May 2011 06:55:59 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[password]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=654</guid>
		<description><![CDATA[php salt random password generation with md5 At present we are going to learn password hashing with random php string. Even we state this for PHP ,but this logic can be applied to any programming language. hashing means generating one way encrypting the given string, which is impossible to get the original string from the [...]]]></description>
			<content:encoded><![CDATA[<h1>php  salt random password generation with md5</h1>
<p>At present we are going to learn  password hashing with random php string. Even we state this for PHP ,but this logic can be applied to any programming language. hashing means generating one way encrypting the given string, which is impossible to get the original string from the hash. this helps to prevent the brute force attaks. This hashing can be used not only password hashing ,it can be applied to user sensitive data .main goal for hashing a string is get uniqueness one data for given fixed lenngth string and generating at minimum time and miniumum application resources. </p>
<h2>Why hashing a string or password?</h2>
<p>for example we take the authentication system where we store the user name and password in plain format. when hacker get into our application got access to database, then he can see the user password. in this case not only hacker can see that passwords,but also other webmasters, coder,designer ..etc can also see the password.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO tbl_user (username,password) VALUES ('<span style="color: #006699; font-weight: bold;">$username</span>','<span style="color: #006699; font-weight: bold;">$password</span>')&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/** sample tbl_user data
*username , password
*mahesh,mypassword1
*chari,password2
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So we must encrypt the password by using php famouse algorithms like <strong>md5</strong>,<strong>sha1</strong>..etc. for example. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO tbl_user (username,password) VALUES ('<span style="color: #006699; font-weight: bold;">$username</span>','<span style="color: #006699; font-weight: bold;">$password</span>')&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/** sample tbl_user data
*username , password
*mahesh,0d28e4080dc8f64fc9603639bb7aa1b9
*chari,6cb75f652a9b52798eb6cf2201057c73
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>With this method hacker who may access database  can not gues the password, but he can know that password with brute force algorithms. This algorithm guess the key with all available alphabet combinations. like a,b,c,&#8230;..aa,ab,ac&#8230;aaa..aab&#8230;etc. So this is also use less or weak implementation of authentication. </p>
<p>Now we add a application secrete that can be called plain hash to user password and do the encrypt, now brute force attack takes much time compared to previous one, it is some what strong compared to other. but if the hacker get to know this secret it is make easy again. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HASH'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'mysecretehash'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">HASH</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO tbl_user (username,password) VALUES ('<span style="color: #006699; font-weight: bold;">$username</span>','<span style="color: #006699; font-weight: bold;">$password</span>')&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/** sample tbl_user data
*username , password
*mahesh,4e71c452f4aa24c19bf807a957986fc8
*chari,c645f52b2d8fcfc25e78ed73fc030bd3
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Since this plain salt is also weak, we generate a strong one that contains special symbols,numbers,capital letter,small letters and more lengthy and keep in mind that it wont take much time to encrypt and application resources. </p>
<p>Now we use salt with md5 ,then it get more strong.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HASH'</span><span style="color: #339933;">,</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysecretehash'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">HASH</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO tbl_user (username,password) VALUES ('<span style="color: #006699; font-weight: bold;">$username</span>','<span style="color: #006699; font-weight: bold;">$password</span>')&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/** sample tbl_user data
*username , password
*mahesh,4e71c452f4aa24c19bf807a957986fc8
*chari,c645f52b2d8fcfc25e78ed73fc030bd3
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now even though above password is so strong but there is still a chance to know if attacker waits for a long time. </p>
<p>Now we use more customized salt generation for individual user liek , we combine user name also into salt. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HASH'</span><span style="color: #339933;">,</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysecretehash'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #990000;">HASH</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO tbl_user (username,password) VALUES ('<span style="color: #006699; font-weight: bold;">$username</span>','<span style="color: #006699; font-weight: bold;">$password</span>')&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/** sample tbl_user data
*username , password
*mahesh,4e71c452f4aa24c19bf807a957986fc8
*chari,c645f52b2d8fcfc25e78ed73fc030bd3
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now it is 98% is stronger than previous ones. remaining is there chance if attaker wait long time. </p>
<p>now we use individaul random salt that is totally unique for each user, so it become more powerfull and strong. now we deal with real solution with customized solution for each individual salt . </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #666666; font-style: italic;">//generate unique random string with given length</span>
<span style="color: #000000; font-weight: bold;">function</span> salt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">srand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>double<span style="color: #009900;">&#41;</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$randsymbols</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;!@#$%^&amp;*()_+=-';:,&lt;.&gt;`~?[]{}&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$randchar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&quot;</span><span style="color: #339933;">;</span> 
		<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$length</span><span style="color: #339933;">%</span><span style="color:#800080;">3</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span> ? <span style="color: #000088;">$randsymbols</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$randchar</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$salt</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color:#800080;">.04</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$length</span><span style="color: #339933;">--;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">mt_srand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//hash the string with favorite encrypt algorithm, here i used md5, you can consider sha1 or else	</span>
<span style="color: #000000; font-weight: bold;">function</span> password<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now we have two functions one do gives unique string ,other do hashing the password. now we add one column to our <strong>tbl_user</strong> , called <strong>customsalt</strong> as varchar(45) , now we add the user to as below</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HASH'</span><span style="color: #339933;">,</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysecretehash'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$customsalt</span><span style="color: #339933;">=</span>salt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span>password<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #990000;">HASH</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$customsalt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO tbl_user (username,password,customsalt) VALUES ('<span style="color: #006699; font-weight: bold;">$username</span>','<span style="color: #006699; font-weight: bold;">$password</span>','<span style="color: #006699; font-weight: bold;">$customsalt</span>')&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/** sample tbl_user data
*username , password,customsalt
*mahesh,4e71c452f4aa24c19bf807a957986fc8,c645f52b2d8fcfc25e78ed73fc030bd3
*chari,c645f52b2d8fcfc25e78ed73fc030bd3,4e71c452f4aa24c19bf807a957986fc8
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<p>Okay , now we consider fetching the user data and validating the user given user name and password for log in functionality. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HASH'</span><span style="color: #339933;">,</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysecretehash'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM tbl_user WHERE username='<span style="color: #006699; font-weight: bold;">$username</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$hashed</span><span style="color: #339933;">=</span>password<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #990000;">HASH</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customsalt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hashed</span><span style="color: #339933;">!==</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Invalid password.'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Remember you must put your <strong>hash</strong> constant in non <strong>public directory </strong>and put that file into running script. </p>
<p>This is my idea only  where i implemented on my projects , each developer has own methods as per their application level security level. </p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=654&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/md5-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql foreign key constraint</title>
		<link>http://www.maheshchari.com/mysql-foreign-key-constraint/</link>
		<comments>http://www.maheshchari.com/mysql-foreign-key-constraint/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 17:42:43 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=648</guid>
		<description><![CDATA[#1005 &#8211; Can&#8217;t create table &#8216;tbl_user&#8217; (errno:105) While we design ER model of our database (schema) , we get this common error . i was asked by many of my friends and colleagues about this issue. first we have to check the tables for both primary tables and foreign tables ,if a Er table exists [...]]]></description>
			<content:encoded><![CDATA[<h1>#1005 &#8211; Can&#8217;t create table &#8216;tbl_user&#8217; (errno:105)</h1>
<p>While we design ER model of our database (schema) , we get this common error . i was asked by many of my friends and colleagues about this issue.<br />
first we have to check the tables for both primary tables and foreign tables ,if a Er table exists we have to check for both main tables have primary key are indexed are not. </p>
<p>This error causes mainly adding foreign key constraint without indexing primary keys.<br />
primary and foreign keys must have same type.<br />
Another this works with only innoDB engine only.</p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=648&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/mysql-foreign-key-constraint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ping search engins</title>
		<link>http://www.maheshchari.com/ping-search-engins/</link>
		<comments>http://www.maheshchari.com/ping-search-engins/#comments</comments>
		<pubDate>Sat, 05 Mar 2011 16:29:18 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[ping]]></category>
		<category><![CDATA[search engine]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=634</guid>
		<description><![CDATA[How to submit our new site to search engine. Generelly web developers build sites or web application and think they completed their job, ofcourse it is. There is a little bit task is that submit the site to search engines like google,yahoo,bing and ask.Search engines will crawl the sites from TLD&#8217;s zone files in regular [...]]]></description>
			<content:encoded><![CDATA[<h1>How to submit our new site to search engine. </h1>
<p>Generelly web developers build sites or web application and think they completed their job, ofcourse it is. There is a little bit task is that submit the site to search engines like <strong>google</strong>,<strong>yahoo</strong>,<strong>bing</strong> and <strong>ask</strong>.Search engines will crawl the sites from TLD&#8217;s zone files in regular interval. So the main entry point for the site is index page of the site. from there search engines crawl the site,if the index page has proper linked. So if a page of our site to be submitted to search engine, it should be linked  atleast on other pages. In most cases it won&#8217;t happen completely. In some cases our site or web application is complexed or user generated content. </p>
<p>So we should make a site map that has all site pages indexed in the specified format or protocal from the <a href="http://sitemaps.org/protocol.php" target="_blank">sitemap.org</a> . We submit this sitemap.xml to search engines by ping . Even there is no use of sitemap.xml in the point of SEO (search engine optimisation) , but our site will be indexed properly . </p>
<h2>Ping the search engines</h2>
<p><strong>Why we ping the search engins?</strong></p>
<ul>
<li>Search engine takes some time to crawl the web site depending on its schedule. </li>
<li>May be some web applications content is generated dynamically by users in timely manner . </li>
<li>In some cases our web site can not be properly inter linked to all our web pages.</li>
<li> improper navigation system. </li>
<li>Some content may be accessed by ajax only ,not for search engines.</li>
<li>Updating the web site content in regular interval, but search engine takes time to update that content. </li>
</ul>
<p><strong>What is sitemap.xml?</strong></p>
<ul>
<li>Sitemap is an easy way for webmasters to inform search engines about pages on                  their sites that are available for crawling. In its simplest form, a Sitemap is                  an XML file that lists URLs for a site along with additional metadata about each web page. You can see the detail protocal or format by visiting <a href="http://sitemaps.org/protocol.php" target="_blank">sitemaps.org</a></li>
</ul>
<p><strong>How to build sitemap.xml?</strong></p>
<ul>
<li>We can code the sitemap.xml if our web site is dynamically generated content. </li>
<li>We can build manually sitemap.xml ,if our site is static and has less pages. </li>
<li>We can build sitemap.xml  by online tools,if our site is large and has more static pages. </li>
</ul>
<p><strong>How can we submit the sitemap.xml to search engins?</strong></p>
<ul>
<li>We can submit the sitemap.xml to search engine by manually in regular interval , weekly ,daily,monthly or when our web content is modified.</li>
<li>We can submit the sitemap.xml by scripting or coding with cron job with some regular interval or when our web content updated.</li>
</ul>
<p><strong>What are the submitting URLs of search engines?</strong></p>
<p>Google: </p>
<pre>http://www.google.com/webmasters/sitemaps/ping?sitemap=http://www.yourwebsite.com/sitemap.xml
</pre>
<p>Yahoo: </p>
<pre>http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=YahooDemo&amp;url=http://www.yourwebsite.com/sitemap.xml</pre>
<p>MSN or Bing :</p>
<pre>http://www.bing.com/webmaster/ping.aspx?siteMap=http://www.yourwebsite.com/sitemap.xml</pre>
<p>Ask:</p>
<pre>http://submissions.ask.com/ping?sitemap=http://www.yourwebsite.com/sitemap.xml</pre>
<p><strong>Ping the above serch engine submit urls</strong></p>
<p>Just open your web browser address bar and copy the above URLS by replacing www.yourwebsite.com/sitemap.xml with your sitemap url. </p>
<p><strong>Submitting sitemap with PHP</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.google.com/webmasters/sitemaps/ping?sitemap=http://www.yourwebsite.com/sitemap.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=YahooDemo&amp;url=http://www.yourwebsite.com/sitemap.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://submissions.ask.com/ping?sitemap=http://www.yourwebsite.com/sitemap.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.bing.com/webmaster/ping.aspx?siteMap=http://www.yourwebsite.com/sitemap.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Remember <strong>file_get_contents</strong><br />
function need permission for open remote pages on server, if not you can use <a href="http://www.maheshchari.com/simple-curl-function-to-send-data-remote-server/">fsocket_open</a>, <a href="http://www.maheshchari.com/simple-curl-function-to-send-data-remote-server/">curl</a></p>
<p>&nbsp; </p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=634&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/ping-search-engins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>60 validation functions with php 2 part</title>
		<link>http://www.maheshchari.com/60-validation-functions-with-php-2-part/</link>
		<comments>http://www.maheshchari.com/60-validation-functions-with-php-2-part/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 07:01:07 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=613</guid>
		<description><![CDATA[This is second part of 60 PHP validation functions. Download the code PHP validation functions php validation function for alpha characters php validation function for alpha dash characters php validation function for alpha numeric characters php validation function for ascii input string php validation function for base64 string php validation function for boolean objects php [...]]]></description>
			<content:encoded><![CDATA[<p>This is second part of 60  PHP validation functions. </p>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<h1>PHP validation functions</h1>
<ul>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_alpha">php validation function for alpha characters</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_alphadash">php validation function for alpha dash characters</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_alphanumeric">php validation function for alpha numeric characters</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_ascii">php validation function for ascii input string </a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_base64">php validation function for base64 string</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_boolean">php validation function for boolean objects</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_creditcard">php validation function for credit card</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_date">php validation function for date</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_dateDe">php validation function for dateDe</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_dateISO">php validation function for dateISO</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_decimal">php validation function for decimal</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_email">php validation function for email address</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_emaildomain">php validation function for email domain exists</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_empty">php validation function for empty object</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_enum">php validation function for enum oject</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_exists">php validation function for variable exists</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_hexcolor">php validation function for hexcolor</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_htmlsafe">php validation function for html tags </a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_integer">php validation function for integer</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_ipaddress">php validation function for ip address</a></li>
<li><a href="#function_jssafe">php validation function for jssafe</a></li>
<li><a href="#function_length">php validation function for string length</a></li>
<li><a href="#function_lower">php validation function for lower string</a></li>
<li><a href="#function_macaddress">php validation function for mac address</a></li>
<li><a href="#function_matches">php validation function for matches</a></li>
<li><a href="#function_maxlength">php validation function for max length</a></li>
<li><a href="#function_maxvalue">php validation function for max value</a></li>
<li><a href="#function_md5">php validation function for md5 string </a></li>
<li><a href="#function_minlength">php validation function for min length</a></li>
<li><a href="#function_minvalue">php validation function for min value</a></li>
<li><a href="#function_multiline">php validation function for multiline</a></li>
<li><a href="#function_numeric">php validation function for numeric</a></li>
<li><a href="#function_phone">php validation function for phone</a></li>
<li><a href="#function_picode">php validation function for pin code</a></li>
<li><a href="#function_rangelength">php validation function for range length</a></li>
<li><a href="#function_rangevalue">php validation function for range value</a></li>
<li><a href="#function_regex">php validation function for regex</a></li>
<li><a href="#function_rgb">php validation function for rgb</a></li>
<li><a href="#function_time12">php validation function for time12</a></li>
<li><a href="#function_time24">php validation function for time24</a></li>
<li><a href="#function_timezone">php validation function for time zone</a></li>
<li><a href="#function_token">php validation function for token</a></li>
<li><a href="#function_unique">php validation function for unique</a></li>
<li><a href="#function_upper">php validation function for upper</a></li>
<li><a href="#function_url">php validation function for url</a></li>
<li><a href="#function_urlexists">php validation function for url exists</a></li>
<li><a href="#function_usssn">php validation function for usssn</a></li>
<li><a href="#function_utf8">php validation function for utf8</a></li>
</ul>
<div class="fclass"><a name="function_jssafe"></a></p>
<h1>Validation for Javascript injection (jssafe)</h1>
<p>Use: There is a chance to hackers to include javascript code in our Input form elements such as Text area ,Text element.It is best practice to avoid such hacking in E-commerce applications.</p>
<p><strong>Javascript Script Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span>script<span style="color: #009900;">&#91;</span>^<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*&gt;</span><span style="color: #009900;">&#91;</span>\s\r\n<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>\<span style="color: #339933;">!--</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">--&gt;</span><span style="color: #009900;">&#41;</span>?<span style="color: #009900;">&#91;</span>\s\r\n<span style="color: #009900;">&#93;</span><span style="color: #339933;">*&lt;</span>\<span style="color: #339933;">/</span>script<span style="color: #339933;">&gt;/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Check given sting has script tags
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_jssafe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&lt;script[^&gt;]*&gt;[\s<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>]*(&lt;\!--)?|(--&gt;)?[\s<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>]*&lt;\/script&gt;/&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_length"></a></p>
<h1>Validation for String length</h1>
<p>Use: validate string length.</p>
<p><strong>Regular Expression</strong></p>
<p>We used handy PHP functions here</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check for exactly length of string
  * @param   string  
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_length<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_lower"></a></p>
<h1>Validation for lower case String</h1>
<p>Use: Oops! in my experiance i didn&#8217;t got a chance to validate againest this. But i kept for reference cause it may use full to any one.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Given sting is lower cased
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_lower<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[a-z]+$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_macaddress"></a></p>
<h1>Validate macaddress</h1>
<p>Use: In some network related and secure money transfer application ,it best practice to rely on MacAddress than IP address. This can be captured with JAVA applet only</p>
<p><strong>Mac address Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks given value again MAC address of the computer
  * @param   string   value      
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_macaddress<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/'</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_matches"></a></p>
<h1>Validation for strict Object matches</h1>
<p>Use: It is used in DB session handling , we fetch object from serialized input from files, Database ..etc. when unserialise it best practice to validate that object .</p>
<p><strong>Regular Expression</strong></p>
<p>We use PHp comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * compares two any kind of values ,stictly
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_matches<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_maxlength"></a></p>
<h1>Validation for String lenght maximum </h1>
<p>Use: in mostly forms we must validate the input value for maximum length, cause Mysql VARACHAR(255) supports.</p>
<p><strong>Regular Expression</strong></p>
<p>We use PHP comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check string length exceeds maximum length     
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_maxlength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_maxvalue"></a></p>
<h1>Validation for number maximum value</h1>
<p>Use: It is most common validation againest numbers</p>
<p><strong>Regular Expression</strong></p>
<p>We use PHP comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given number exceeds max values   
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_maxvalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_md5"></a></p>
<h1>Validate  md5 String</h1>
<p>Use: in mostly case we use md5 for secret data passing and key , password validation.</p>
<p><strong>md5 string Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>f<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span>i</pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks that a field matches a v2 md5 string
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_md5<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[0-9a-f]{32}/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_minlength"></a></p>
<h1>Validation for String minimum length</h1>
<p>Use: in mostly forms we must validate the input value for maximum length, cause Mysql VARACHAR(255) supports.</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  *Check the string length has minimum length
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_minlength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$min</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$min</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_minvalue"></a></p>
<h1>Validation for number minimum value</h1>
<p>Use : mostly forms elements we check this validation, example in registration form we ask user age. </p>
<p><strong>Regular Expression</strong></p>
<p>We use PHP comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given number below value   
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_minvalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_multiline"></a></p>
<h1>Validation for multy line characters.</h1>
<p>Use: some times ,we need to strip or validate the new line characters. </p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>\n\r\t<span style="color: #009900;">&#93;</span><span style="color: #339933;">+/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given sring has multilines 
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_multiline<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\t</span>]+/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_numeric"></a></p>
<h1>Validation for numeric</h1>
<p>Use: validate given input string is numeric with optional +,-,. values</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>\<span style="color: #339933;">-+</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>\<span style="color: #339933;">.</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check a number optional -,+,. values
  * @param   string        
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[\-+]?[0-9]*\.?[0-9]+$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_phone"></a></p>
<h1>Validate for Phone Number</h1>
<p>Use: validate given input is phone number </p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Matches a phone number that length optional numbers 7,10,11
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_phone<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lengths</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lengths</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$lengths</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\D+/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lengths</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_picode"></a></p>
<h1>Validate  Pincode</h1>
<p>Use: validate given input is phone number </p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">We used more than one regular expression depending on countries</pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks that given value matches following country pin codes.     
  * at = austria
  * au = australia
  * ca = canada
  * de = german
  * ee = estonia
  * nl = netherlands
  * it = italy
  * pt = portugal
  * se = sweden
  * uk = united kingdom
  * us = united states
  * @param String   
  * @param String
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_pincode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'us'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$patterns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'at'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{4,4}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'au'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[2-9][0-9]{2,3}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ca'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^[a-zA-Z].[0-9].[a-zA-Z].\s[0-9].[a-zA-Z].[0-9].'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'de'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{5,5}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ee'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^[0-9]{5,5}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nl'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{4,4}\s[a-zA-Z]{2,2}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'it'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{5,5}$'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'pt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{4,4}-[0-9]{3,3}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'se'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{3,3}\s[0-9]{2,2}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'uk'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'us'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^[0-9]{5,5}[\-]{0,1}[0-9]{4,4}$'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$country</span><span style="color: #339933;">,</span> <span style="color: #000088;">$patterns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$patterns</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$country</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_rangelength"></a></p>
<h1>Validation for string lenght is with in range of given lengths</h1>
<p>Use : This is the most common validation again user input values.</p>
<p><strong>Regular Expression:</strong></p>
<p>Here we used PHP handy functions. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * check given string length is between given range 
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rangelength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$min</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$min</span> and <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_rangevalue"></a></p>
<h1>Validation for given integer value between given two values</h1>
<p>Use : This is the most common validation again user input values.</p>
<p><strong>Regular Expression:</strong></p>
<p>Here we used PHP handy functions. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * check given number between given values
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rangevalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span><span style="color: #000088;">$min</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$min</span> and <span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_regex"></a></p>
<h1>Validation input value againest given regex</h1>
<p>Use: in some cases we have use custom regular expression.</p>
<p><strong>Regular Expression:</strong></p>
<p>Here we used PHP handy functions. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * check given number between given values
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rangevalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span><span style="color: #000088;">$min</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$min</span> and <span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_rgb"></a></p>
<h1>Validation for rgb colour value</h1>
<p>use: we validate the user input is RGB color </p>
<p><strong>rgb color Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span>rgb\<span style="color: #009900;">&#40;</span>\s<span style="color: #339933;">*</span>\b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>\b\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span>\b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>\b\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span>\b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>\b\s<span style="color: #339933;">*</span>\<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span>rgb\<span style="color: #009900;">&#40;</span>\s<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\d?\d<span style="color: #339933;">%|</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">%</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\d?\d<span style="color: #339933;">%|</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">%</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\d?\d<span style="color: #339933;">%|</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">%</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>\s<span style="color: #339933;">*</span>\<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Check is rgb color value
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rgb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))$/&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_time12"></a></p>
<h1>Validate  time 12 hours format</h1>
<p>use: Validate for 12 hours format time </p>
<p><strong>12 hours format Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>aApP<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>mM<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Time in 12 hours format with optional seconds
     * 08:00AM | 10:00am | 7:00pm
     * @param   string         
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_time12<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_time24"></a></p>
<h1>Validation for 24 hours format time</h1>
<p>use: Validate for 24 hours format time </p>
<p><strong>24 hours time format Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>?$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Time in 24 hours format with optional seconds
     * 12:15 | 10:26:59 | 22:01:15 
     * @param   string         
     * @return  boolean
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> is_time24<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_timezone"></a></p>
<h1>Validation for time zone format</h1>
<p>use: Validate time zone format GMT </p>
<p><strong>GMT time zone Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span><span style="color: #339933;">-+</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #208080;">03</span><span style="color: #009900;">&#93;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">45</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">14</span><span style="color: #339933;">:</span><span style="color: #208080;">00</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Checks given value matches a time zone  
     * +00:00 | -05:00 
     * @param   string         
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_timezone<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[-+]((0[0-9]|1[0-3]):([03]0|45)|14:00)$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_token"></a></p>
<h1>Validation for token</h1>
<p>use: A token that don&#8217;t have white spaces.</p>
<p><strong>Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>\s<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #009933; font-style: italic;">/**
     * A token that don't have any white space
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_token<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\s/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_unique"></a></p>
<h1>Validate  unique array elements</h1>
<p>Use: in some times we ask user multiple choices ,so these elements are unique are not!!</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #009933; font-style: italic;">/**
     * check if array has unique elements,it must have  minimum one element
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_unique<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #009900;">&#41;</span><span style="color: #000088;">$arr</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$count1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$count2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>count1 <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span> and <span style="color: #009900;">&#40;</span>count1 <span style="color: #339933;">==</span> <span style="color: #000088;">$count2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_upper"></a></p>
<h1>Validation for input value is Upper case</h1>
<p>Use: in experience i didn&#8217;t got any chance to validate this ,but it is may use full for others.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>A<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Given string is upper cased?
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_upper<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[A-Z]+$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_url"></a></p>
<h1>Validation for URL </h1>
<p>Use: validate given input is URL format </p>
<p><strong>Regular Expression</strong></p>
<p>&nbsp;</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Valid URL or web address
     * @param   string      
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:@&amp;=+$,A-Za-z0-9])+)([).!';\/?:,][[:blank:]])?$&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_urlexists"></a></p>
<h1>Validate  URL exists</h1>
<p>Use: some times given we need If URL works or not</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Check given url really exists?
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_urlexists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">@</span><span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_usssn"></a></p>
<h1>Validation for US Social security number</h1>
<p>Use : validate the user input is valid US social security number format</p>
<p><strong>US social security number Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Checks given value matches us citizen social security number
     * @param   string         
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_usssn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d{3}-\d{2}-\d{4}$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_utf8"></a></p>
<h1>Validation for utf8</h1>
<p>Use: given file input string is UTF8 format</p>
<p>Regular Expression</p>
<p>PHP function </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * check given sting is UTF8 
     * @param   string  
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_utf8<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%(?:
        [\xC2-\xDF][\x80-\xBF]        
        |\xE0[\xA0-\xBF][\x80-\xBF]               
        |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}     
        |\xED[\x80-\x9F][\x80-\xBF]               
        |\xF0[\x90-\xBF][\x80-\xBF]{2}   
        |[\xF1-\xF3][\x80-\xBF]{3}                  
        |\xF4[\x80-\x8F][\x80-\xBF]{2}    
        )+%xs'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p> Use: i have noticed my tutorials are directly copied on other blogs without informing, i spent some man hours doing this , if you want refer please send me back link. </p>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=613&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/60-validation-functions-with-php-2-part/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>60 validation functions with php</title>
		<link>http://www.maheshchari.com/60-validation-functions-with-php/</link>
		<comments>http://www.maheshchari.com/60-validation-functions-with-php/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 14:48:01 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=602</guid>
		<description><![CDATA[Hello my readers, after one month of hard work , i created a validation class and made a complete tutorial with 60 php validation methods in PHP. here each functions works independently. These methods are used in every kind of web applications. even i written the functions in PHP, you can use them in Javascript [...]]]></description>
			<content:encoded><![CDATA[<p>Hello my readers, after one month of hard work , i created a validation class and made a complete  tutorial with 60 php validation methods in PHP. here each functions works independently. These methods are used in every kind of web applications. even i written the functions in PHP, you can use them in Javascript also by taking regular expression directly. If any method i missing ,fixing  or you get idea for implementing and adding here please contact me.we can use this php class in form validation.</p>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<h1>PHP validation functions</h1>
<ul>
<li><a href="#function_alpha">php validation function for alpha characters</a></li>
<li><a href="#function_alphadash">php validation function for alpha dash characters</a></li>
<li><a href="#function_alphanumeric">php validation function for alpha numeric characters</a></li>
<li><a href="#function_ascii">php validation function for ascii input string </a></li>
<li><a href="#function_base64">php validation function for base64 string</a></li>
<li><a href="#function_boolean">php validation function for boolean objects</a></li>
<li><a href="#function_creditcard">php validation function for credit card</a></li>
<li><a href="#function_date">php validation function for date</a></li>
<li><a href="#function_dateDe">php validation function for dateDe</a></li>
<li><a href="#function_dateISO">php validation function for dateISO</a></li>
<li><a href="#function_decimal">php validation function for decimal</a></li>
<li><a href="#function_email">php validation function for email address</a></li>
<li><a href="#function_emaildomain">php validation function for email domain exists</a></li>
<li><a href="#function_empty">php validation function for empty object</a></li>
<li><a href="#function_enum">php validation function for enum oject</a></li>
<li><a href="#function_exists">php validation function for variable exists</a></li>
<li><a href="#function_hexcolor">php validation function for hexcolor</a></li>
<li><a href="#function_htmlsafe">php validation function for html tags </a></li>
<li><a href="#function_integer">php validation function for integer</a></li>
<li><a href="#function_ipaddress">php validation function for ip address</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_jssafe">php validation function for jssafe</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_length">php validation function for string length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_lower">php validation function for lower string</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_macaddress">php validation function for mac address</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_matches">php validation function for matches</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_maxlength">php validation function for max length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_maxvalue">php validation function for max value</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_md5">php validation function for md5 string </a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_minlength">php validation function for min length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_minvalue">php validation function for min value</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_multiline">php validation function for multiline</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_numeric">php validation function for numeric</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_phone">php validation function for phone</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_picode">php validation function for pin code</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_rangelength">php validation function for range length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_rangevalue">php validation function for range value</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_regex">php validation function for regex</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_rgb">php validation function for rgb</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_time12">php validation function for time12</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_time24">php validation function for time24</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_timezone">php validation function for time zone</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_token">php validation function for token</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_unique">php validation function for unique</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_upper">php validation function for upper</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_url">php validation function for url</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_urlexists">php validation function for url exists</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_usssn">php validation function for usssn</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_utf8">php validation function for utf8</a></li>
</ul>
<div class="fclass"><a name="function_alpha"></a></p>
<h1>validation for alpha characters</h1>
<p><strong>Use:</strong> It validates only Alphabets i.e small and capital letters. Input parameter must be <strong>trimmed</strong> ,it won&#8217;t allow spaces also.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span>i</pre></div></div>

<p>Regular Expression has case insensitive modifier.</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Matches only alpha letters
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_alpha<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([a-zA-Z])+$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameters</strong></p>
<ul>
<li>Mahesh</li>
<li>chari</li>
<li>PHP</li>
</ul>
<p><strong>Invalid parameters</strong></p>
<ul>
<li>Mahesh1</li>
<li>cha2ri</li>
<li>PHP5.0.2</li>
</ul>
</div>
<div class="fclass"><a name="function_alphadash"></a></p>
<h1>validation for alphadash characters</h1>
<p><strong>Use:</strong> It validates only Alphabets and dashes (under score, dash) i.e small , capital letters,_,-. Input parameter must be <strong>trimmed</strong> ,it won&#8217;t allow spaces also.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z_<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span>i</pre></div></div>

<p>Regular Expression has case insensitive modifier. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Matches alpha and dashes like -,_
     * @param   string  
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_alphadash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([A-Za-z_-])+$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameters</strong></p>
<ul>
<li>Mahesh</li>
<li>chari_</li>
<li>PHP_OOPS</li>
</ul>
<p><strong>Invalid parameters</strong></p>
<ul>
<li>Mahesh1</li>
<li>cha2ri</li>
<li>PHP5.0.2</li>
<li>$_POST</li>
</ul>
</div>
<div class="fclass"><a name="function_alphanumeric"></a></p>
<h1>validate  alpha numeric characters</h1>
<p><strong>Use:</strong> It validates only Alphabets and dashes (under score, dash) i.e small , capital letters,_,-. Input parameter must be <strong>trimmed</strong> ,it won&#8217;t allow spaces also.</p>
<p><strong>alpha numeric characters regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span>i</pre></div></div>

<p>Regular Expression has case insensitive modifier. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Matches alpha and numbers only
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_alphanumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([a-zA-Z0-9])+$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameters</strong></p>
<ul>
<li>Mahesh</li>
<li>chari</li>
<li>PHP OOPS</li>
</ul>
<p><strong>Invalid parameters</strong></p>
<ul>
<li>PHP5.0.2</li>
<li>$_POST</li>
</ul>
</div>
<div class="fclass"><a name="function_ascii"></a></p>
<h1>validation for ascii input from a file</h1>
<p>Use: It is very important to work with multi language application , we must validate for utf-8, ascii characters. </p>
<p><strong>ascii string format validate Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>^\x00<span style="color: #339933;">-</span>\x7F<span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span>i</pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * is given string is ascii format?
  * @param   string        
  * @return  boolean
  */</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> is_ascii<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[^\x00-\x7F]/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameter</strong></p>
<p>We can&#8217;t display file specific characters here . just test with note pad text.</p>
<p><strong>Invalid parameter</strong></p>
<p>We can&#8217;t display file specific characters here . just test with note pad text.</p>
</div>
<div class="fclass"><a name="function_base64"></a></p>
<h1> base64 string Validation</h1>
<p>Use: Generelly we encrypt the URL parameters with <strong>base64_encode</strong> and <strong>base64_decode</strong> from end users,while validating with sessions,passing secrate data. Then we must check that parameter string for valid base64 encoding. </p>
<p><strong>base64 string Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>^a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">/</span>\<span style="color: #339933;">+=</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Matches base64 enoding string
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_base64<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[^a-zA-Z0-9\/\+=]/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameter</strong></p>
<p>base64 encoded string</p>
<p><strong>Invalid parameter</strong></p>
<p>non base64 encoded string</p>
</div>
<div class="fclass"><a name="function_boolean"></a></p>
<h1>Validation for boolean or Bool type</h1>
<p>Use: Generelly we use so many type values for number (1,0), strings (&#8216;yes&#8217;,'no&#8217;),(&#8217;1&#8242;,&#8217;0&#8242;),bool (true,false)</p>
<p><strong>PHP function </strong></p>
<p>Custom <strong>function</strong> that validates informal and formal type of boolean. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * is given field is boolean value or not
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_boolean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$booleans</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$literals</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'true'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'false'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'no'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$booleans</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$bool</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$bool</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$literals</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>PHP core function </strong></p>
<p><strong>is_bool</strong>() can be used</p>
<p><strong>Valid parameter</strong></p>
<p>1,0,&#8217;yes&#8217;,'no&#8217;,true,false</p>
<p><strong>Invalid parameter</strong></p>
<p>any different paramenter from the above.</p>
</div>
<div class="fclass"><a name="function_creditcard"></a></p>
<h1>Validate credit card number</h1>
<p>Use: E-Commerce applications uses,for user credit card validation.</p>
<p><strong>credit card validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6011</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>?\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>?\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>?\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">13</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>credit card validation php  function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Valid Credit Card
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_creditcard<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^((4\d{3})|(5[1-5]\d{2})|(6011)|(7\d{3}))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_date"></a></p>
<h1>Validation for date any format</h1>
<p>Use: While validating time event dependent applications like organising,appointment..etc application we use date validation.</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Check if given string matches any format date
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_dateDe"></a></p>
<h1>Validation for dateDe [d.m.y]</h1>
<p>Use: While validating time event dependent applications like organising,appointment..etc application we use date validation.</p>
<p><strong>date de validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d\d?\<span style="color: #339933;">.</span>\d\d?\<span style="color: #339933;">.</span>\d\d\d?\d?$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks given value matches date de
  * @param   string         
  * @return  boolean
  */</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> is_dateDE<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d\d?\.\d\d?\.\d\d\d?\d?$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_dateISO"></a></p>
<h1>Validation for dateISO</h1>
<p>Use: While validating time event dependent applications like organising,appointment..etc application we use date validation.</p>
<p><strong>iso date Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span>\<span style="color: #339933;">/-</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span>\<span style="color: #339933;">/-</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP  validate date iso function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks given value matches us citizen social security number
  * @param   string         
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_dateISO<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_decimal"></a></p>
<h1>Validation for decimal</h1>
<p>Use: Validation while processing ecommerce form for money values. </p>
<p><strong>decimal validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check decimal with . is optional and after decimal places up to 6th precision
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_decimal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d+(\.\d{1,6})?$/'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_email"></a></p>
<h1>Validation for email address</h1>
<p>Use: validation on user registration ,contact forms.</p>
<p><strong>email address validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">+</span>_\<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">+</span>_\<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*@</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span>ix</pre></div></div>

<p><strong>PHP email address validation function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * valid email     
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_emaildomain"></a></p>
<h1>validate  email domain exists</h1>
<p>Use: validation on user registration ,contact forms. Before testing this function check it is valid email.</p>
<p><strong>email Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>^<span style="color: #339933;">@</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++@/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks that a field is exactly the right length.
  * @param   string   value
  * @link  http://php.net/checkdnsrr  not added to Windows until PHP 5.3.0
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_emaildomain<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">checkdnsrr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[^@]++@/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'MX'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_empty"></a></p>
<h1>Validation for empty input for all type</h1>
<p>Use: validate the input parameter that is string ,boolean,array ,object, null .etc types also for empty</p>
<p><strong>PHP validate function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check if field empty string ,orject,array
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_empty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_enum"></a></p>
<h1>Validation for enum</h1>
<p>Use: we need to check a value for fixed number of specified values only, takes a input value and compares the given array, it is alias of <strong>in_array</strong> PHP function </p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given string againest given array values
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_enum<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_exists"></a></p>
<h1>Validation for variable exists</h1>
<p>Use: check if a array key element exists.</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given array key element exists?
  * @param   string   
  * @return  boolean
  */</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> is_exists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_hexcolor"></a></p>
<h1>Validate  hexa decimal color</h1>
<p>Use: validate the given value for hexa decimal color format </p>
<p><strong>hexa decimal color Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #666666; font-style: italic;">#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Valid hexadecimal color ,that may have #,
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_hexcolor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_htmlsafe"></a></p>
<h1>Validate  html tags</h1>
<p>Use: It is best practice to validate our forms textarea elements data againest HTML content ,before we displaying HTML with Mysql data. If we don&#8217;t validate this will override our Web page template layout. </p>
<p><strong>HTML tag Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;.*&lt;</span>$<span style="color:#800080;">1</span><span style="color: #339933;">&gt;/</span></pre></div></div>

<p><strong>PHP HTML tag validation function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * given sting has html tags?
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_htmlsafe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&lt;(.*)&gt;.*
  &lt;<span style="color: #006699; font-weight: bold;">$1</span>&gt;
  /&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_integer"></a></p>
<h1>Validation for integer </h1>
<p>Use: validate integer </p>
<p><strong>validate input for integer php function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  *Matches exactly number
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">is_integer</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_ipaddress"></a></p>
<h1>Validate ip address</h1>
<p>Use: It is best practice to validate  user remote address with PHP , in some times user may comes as anonymus i.e Proxy enabled . it is best used in IP bound applications like polling, affiliate marketing applications.. </p>
<p><strong>ip address validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;.*&lt;</span>$<span style="color:#800080;">1</span><span style="color: #339933;">&gt;/</span></pre></div></div>

<p><strong>ip address validate php  function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Valid IP address
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_ipaddress<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=602&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/60-validation-functions-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php template system</title>
		<link>http://www.maheshchari.com/php-template-system/</link>
		<comments>http://www.maheshchari.com/php-template-system/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 09:45:12 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=578</guid>
		<description><![CDATA[A basic template engine that seperates the business logic from display logic. &#160; The basic idea of this template system is to implement a light weight MVC patter.We use only two PHP functions ob_start(); ob_get_clean(); Advantages: With this php template system we can include nested templates also. since this php template system does directly php [...]]]></description>
			<content:encoded><![CDATA[<h2>A basic template engine that seperates the business logic from display logic.</h2>
<p>&nbsp;</p>
<p>The basic idea of this template system is to implement a light weight MVC patter.We use only two PHP functions </p>
<ul>
<li>ob_start();</li>
<li>ob_get_clean(); </li>
</ul>
<h2>Advantages:</h2>
<ul>
<li>With this php template system we can include nested templates also.</li>
<li>since this php template system does directly php code it performs much faster.  </li>
<li>very light weight. </li>
</ul>
<h2>ob_start()</h2>
<p>This function will turn output buffering on. While output buffering is     active no output is sent from the script (other than headers), instead the     output is stored in an internal buffer. This buffer can be retrived to a string variable using <strong>ob_get_contents()</strong>.</p>
<h2>ob_get_clean()</h2>
<p>Gets the current buffer contents and delete current output buffer. this buffer in turn returns as a string like <strong>ob_get_contents()</strong>. </p>
<p>To test this template system download the example files and see the example1.php,example2.php</p>
<h2>Template Class<br />
</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> template
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> template<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">include</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">ob_get_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Example</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'template.class.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tpl</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> template<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tvars</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tvars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'Mahesh'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tvars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'arrayvars'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Apple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Mango'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Banana'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tvars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nestedtemplate'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template.nest.tpl.php'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template2.tpl.php'</span><span style="color: #339933;">,</span><span style="color: #000088;">$tvars</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>  <a href='http://www.maheshchari.com/wp-content/uploads/2010/12/template.zip'>Dowload the example</a></p>
<h2>Output</h2>
<p><img src="http://www.maheshchari.com/wp-content/uploads/2010/12/phptemplate.jpg" alt="" title="phptemplate" width="401" height="272" class="aligncenter size-full wp-image-579" /></p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=578&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/php-template-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>email domain really exists</title>
		<link>http://www.maheshchari.com/email-domain-really-exists/</link>
		<comments>http://www.maheshchari.com/email-domain-really-exists/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 07:28:44 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[anti spam]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=573</guid>
		<description><![CDATA[How to check users email address domain really exists? We know how spam bothers much on contact pages of web application,even we use captcha ,there will be manually generated spam. And we don&#8217;t know given email address is fake or real. Good thing is that we can check is given email address fake or not [...]]]></description>
			<content:encoded><![CDATA[<h1>How to check users email address domain really exists?</h1>
<p>We know how spam bothers much on contact pages of web application,even we use captcha ,there will be manually generated spam.<br />
And we don&#8217;t know given email address is fake or real. </p>
<p>Good thing is that we  can check is given email address fake or not with php .For this PHP has handy function that checks the email domain for real time exists.</p>
<p>Bad thing is that this function is not bundled until 5.3 on windows server.</p>
<h2>checkdnsrr</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_emaildomain<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">checkdnsrr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[^@]++@/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'MX'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=573&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/email-domain-really-exists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

