Prashant Ingale

Juice of PHP, MySQL, Apache, Linux, Actionscript, Flex

Archive for August, 2008

Date Validation using PHP

leave a comment

In this we will see date validation function written in PHP.
For using this function date has to be separated with “-” or “/” or u can change to your favorite one. If anyone have any queries or suggestions they are welcome.

function date_validate($datefield) {
	    // First check to see if the input ($datefield) is in one of the accepted formats
	    // Check for delimiters ("-" or "/") and put three fields into an array
	    if (strpos($datefield, "-")) {
	      $datesplit = explode("-", $datefield);
	    } elseif (strpos($datefield, "/")) {
	      $datesplit = explode("/", $datefield);
	    } else {
	        $date_err="Error: Invalid date field. No proper delimiters (- or /) found";
	        return false;
	    }
	    // Check for three input fields (month, day, year)
	    if (count($datesplit)>3) {
	        $date_err="Error: Invalid date field. Too many fields (".count($datesplit).") found";
	        return false;
	    }
	    // Put date array into single format
	    if (strlen($datesplit[2])==4) { // The year is listed last - switch fields around
	        $newdatesplit[0]=$datesplit[2]; // Move Year to first field
	        $newdatesplit[1]=$datesplit[0]; // Move Month to second field
	        $newdatesplit[2]=$datesplit[1]; // Move Day to third field
	        $datesplit=$newdatesplit;
	    } elseif (strlen($datesplit[0])==4) { // The year is first listed - do nothing
	        // nothing to be done
	    } else { // Date entered is not valid; could not find year field
	        $date_err=”Error: Date not valid. No Year field found (Year must be 4 digits)”;
	        return false;
	    }
	    // Main validation code
	    if ($datesplit[1]>12) { // No valid month field
	        $date_err=”Error: Invalid Month field (”.$datesplit[1].”) “;
	        return false;
	    } else {
	       switch ($datesplit[1]) { // Check number of days in a month
	           case 4:
	           case 6:
	           case 9:
	           case 11:
	                if ($datesplit[2]>30) {
	                    $date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
	                    return false;
	                }
	                break;
	           case 2: // February Check
	                   if (($datesplit[0]/4)==(floor($datesplit[0]/4))) {
	                    if (($datesplit[0]/100)==(floor($datesplit[0]/100))) {
	                        if (($datesplit[0]==1600) or ($datesplit[0]==2000) or ($datesplit[0]==2400)) {
	                            if ($datesplit[2]>29) {
	                                $date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
	                                return false;
	                            }
	                        } else {
	                            if ($datesplit[2]>28) {
	                                $date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
	                                return false;
	                            }
	                        }
	                    } else {
	                        if ($datesplit[2]>29) {
	                            $date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
	                            return false;
	                        }
	                    }
	                } else {
	                    if ($datesplit[2]>28) {
	                        $date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
	                        return false;
	                    }
	                }
	                break;
	           default:
	                if ($datesplit[2]>31) {
	                    $date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
	                    return false;
	                }
	        }
	      }
	          // Add leading zero if month or day field is only one character
	      if (strlen($datesplit[1])==1) {
	          $datesplit[1]=”0″.$datesplit[1];
	      }
	      if (strlen($datesplit[2])==1) {
	          $datesplit[2]=”0″.$datesplit[2];
	      }
	      // Create date field in MySQL format
	      $newdate=$datesplit[0].”-”.$datesplit[1].”-”.$datesplit[2];
	      return true;
	}

Written by admin

August 29th, 2008 at 1:01 am

Posted in PHP Validations

Email address validation - javascript

leave a comment

Below is simple yet powerful email validation script using regular expression.

function validateEmail(myEamil) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myEmail.value)){
		return (1)
	}
	return (0)
}

Written by admin

August 27th, 2008 at 1:09 am

PHP Simple Error Handling

leave a comment

This is simple example of error handing in PHP using .htaccess. Let’s create .htaccess file first open your favorite editor and write below code and save it.

ErrorDocument 400 /error/index.php?error=400
ErrorDocument 401 /error/index.php?error=401
ErrorDocument 403 /error/index.php?error=403
ErrorDocument 404 /error/index.php?error=404
ErrorDocument 500 /error/index.php?error=500

Please refer Apache Docs for getting information on ErrorDocument and error codes. After saving .htaccess file place it in the root folder. Create new file and put below code in that file for handling errors.


$homepage = “mywebpage.com”;
if (isset($_GET['error'])) {
    if ($_GET['error'] == 400) {
        $title = ‘Error 400 - BAD REQUEST!’;
        $message = ‘The server didn\’t understand the request.’;
    } elseif ($_GET['error'] == 401) {
        $title = ‘Error 401 - UNAUTHoRIZED!’;
        $message = ‘You are not unauthorized to view the page requested.’;
    } elseif ($_GET['error'] == 403) {
        $title = ‘Error 403 - ACCESS FORBIDDEN!’;
        $message = ‘Access for this request was denied.’;
    } elseif ($_GET['error'] == 404) {
        $title = ‘Error 404 - PAGE NOT FOUND!’;
        $message = ‘File not found! ‘;
    } elseif ($_GET['error'] == 406) {
        $title = ‘Error 404 - NOT ACCEPTABLE!’;
        $message = ‘The resource cannot be displayed! ‘;
    } elseif ($_GET['error'] == 500) {
        $title = ‘Error 500 - INTERNAL SERVER ERROR!’;
        $message = ‘The server encountered an unexpected condition which prevented it from fulfilling the request.’;
    } elseif ($_GET['error'] == 502) {
        $title = ‘Error 502 - BAD GATEWAY!’;
        $message = ‘The server, while acting as a gateway or proxy, received an invalid response from the upstream server.’;
    } elseif ($_GET['error'] == 504) {
        $title = ‘Error 504 - GATEWAY TIMEOUT!’;
        $message = ‘The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.’;
    }
}

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HEAD>
<TITLE>[<? echo $title ?>]</TITLE>
<META http-equiv=Content-Type content=”text/html; charset=iso-8859-1″>
<META HTTP-EQUIV=Refresh CONTENT=”30; URL=http://<? echo $homepage ?>”>
<STYLE>
BODY {
	FONT-SIZE: 10px;
 BACKGROUND: url(http://<? echo $homepage ?>/error/bg.gif);
	COLOR: #333333;
	LINE-HEIGHT: 180%;
	FONT-FAMILY: tahoma, arial;
}
A {
	PADDING-RIGHT: 1px;
	PADDING-LEFT: 1px;
	PADDING-BOTTOM: 1px;
	COLOR: #ff6600;
	PADDING-TOP: 1px;
	TEXT-DECORATION: none
}
A:hover {
	PADDING-RIGHT: 1px;
	PADDING-LEFT: 1px;
	PADDING-BOTTOM: 1px;
	COLOR: #ff6600;
	PADDING-TOP: 1px;
	TEXT-DECORATION: underline;
}
TD {
	FONT-SIZE: 10px;
	COLOR: #333333;
	LINE-HEIGHT: 180%;
	FONT-FAMILY: tahoma, arial;
}
.box {
	BORDER-RIGHT: #e3e3e3 1px solid;
	BORDER-TOP: #e3e3e3 1px solid;
	BACKGROUND: #ffffff;
	BORDER-LEFT: #e3e3e3 1px solid;
	WIDTH: 400px;
	BORDER-BOTTOM: #e3e3e3 1px solid;
}
</STYLE>
<SCRIPT language=”JavaScript”>
<!–
 function getgoing()
  {
    top.location=”http://someplace.com”;
   }
   if (top.frames.length==0)
    {
     alert(”You will be redirected to our main page in 10 seconds!”);
     setTimeout(’getgoing()’,100000);
     }
//–>
</SCRIPT>
</HEAD>
<BODY onLoad=”getgoing();”>
<CENTER>
  <BR>
  <BR>
  <BR>
  <BR>
  <DIV class=box> <BR>
    <BR>
    <H3><? echo $title; ?><br>
      <? echo $message; ?></H3>
    <br>
    <DL>
      <DD> You will be automatically redirected to  www.<? echo $homepage ?> in 10 seconds.<br>
        If u don’t wish to wait click <a href=”http://<? echo $homepage ?>”>here</a>. </DD>
    </DL>
  </DIV>
</CENTER>
</BODY>
</HTML>

PHP if conditions will trap error codes and assign respective title and message for the code. And below presentation code will display an appropriate message to the user for 10 seconds and then it will redirect page to the provided URL in $homepage variable
Now create directory in the root folder called “error” and save this file with the name index.php and your are done.

Written by admin

August 23rd, 2008 at 12:17 pm