Archive for January, 2008

Date Validation Tutorial

Thursday, January 31st, 2008

Date Validation in PHP

This tutorial will show how to Validate Date fields using PHP code in ‘dd/mm/yyyy’ Format. It is a good practice to validate the date value when it is obtained using forms through user input.

PHP CODE:

//Check whether the submission is made
if(!isset($_POST["hidSubmit"])){
//Declarate the necessary variables
$strdate="";
$strdate1="";
DisplayForm();
}
else{
$strdate=$_POST["txtdate"];
//Check the length of the entered Date value
if((strlen($strdate)<10)OR(strlen($strdate)>10)){
echo(”Enter the date in ‘dd/mm/yyyy’ format”);
}
else{
//The entered value is checked for proper Date format
if((substr_count($strdate,”/”))<>2){
echo(”Enter the date in ‘dd/mm/yyyy’ format”);
}
else{
$pos=strpos($strdate,”/”);
$date=substr($strdate,0,($pos));
$result=ereg(”^[0-9]+$”,$date,$trashed);
if(!($result)){echo “Enter a Valid Date”;}
else{
if(($date<=0)OR($date>31)){echo “Enter a Valid Date”;}
}
$month=substr($strdate,($pos+1),($pos));
if(($month<=0)OR($month>12)){echo “Enter a Valid Month”;}
else{
$result=ereg(”^[0-9]+$”,$month,$trashed);
if(!($result)){echo “Enter a Valid Month”;}
}
$year=substr($strdate,($pos+4),strlen($strdate));
$result=ereg(”^[0-9]+$”,$year,$trashed);
if(!($result)){echo “Enter a Valid year”;}
else{
if(($year<1900)OR($year>2200)){echo “Enter a year between 1900-2200″;}
}
}
}
DisplayForm();
}
//User-defined Function to display the form in case of Error
function DisplayForm(){
global $strdate;

Explanation
*Expression ^[0-9]+$
^ :-Indicates the beginning of the string
[0-9]+ :- indicates that the string begins with a number between (0-9) and is followed by numbers.
$ :- Indicates the end of the String

Basic PHP Syntax

Monday, January 28th, 2008

A PHP scripting block usually starts with . The best thing about PHP scripting block is that you can place it anywhere in the document. placed anywhere in the document.

On servers with shorthand support enabled you can start a scripting block with .

It is recommend that you use the standard form

< html >
< body >

< ? php
echo "Hello World";
? >

< /body >
< /html >< /em >

Every line in PHP ends with a semi-colon. The two basic output text in PHP are: Echo and Print.

Libraries and Extension

Monday, January 28th, 2008

Libraries > PHP includes a large number of free and open source libraries with the core build.

Extensions > PHP allows developers to write extensions in C to add functionality to the PHP language. These can then be compiled into PHP or loaded dynamically at runtime. Extensions have been written to add support for the Windows API, process management on Unix-like operating systems, multibyte strings (Unicode), cURL, and several popular compression formats. Some more unusual features include integration with Internet relay chat, dynamic generation of images and Adobe Flash content, and even speech synthesis. The PHP Extension Community Library (PECL) project is a repository for extensions to the PHP language. -Source

Using Cookies in PHP

Monday, January 28th, 2008

What is a Cookie?Cookies are small bits of information that can be stored on a client computer. Once a cookie is created, it will expire after a specified time period. All the information stored in a cookie exist until it expires or deleted by the user.

Why do we need Cookies? Now-a-days most of the websites use cookies to store small amounts of information. Websites can read the values from the cookies and use the information as desired. The browser is capable of keeping track of the websites and their corresponding cookies and is capable of reading the information from relevant cookies. Some common use of cookies include:

  • User’s aesthetic preference for a specific site.
  • User keys to link them with their personal data - as used by many Shopping Cart Applications.
  • Allowing a user to remain ‘logged on’ until he explicitly logs out or the browser window is closed.

-Source

Creating Your Own “Function”

Tuesday, January 22nd, 2008

A function is a block of code that can be executed whenever we need it.

Creating PHP functions:

* All functions start with the word “function()”
* Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
* Add a “{” - The function code starts after the opening curly brace
* Insert the function code
* Add a “}” - The function is finished by a closing curly brace

Example

A simple function that writes my name when it is called:

<html>
<body>

<?php
function writeMyName()
{
echo “Michelle”;
}

writeMyName();
?>

</body>
</html>

Use a PHP Function

Now we will use the function in a PHP script:

<html>
<body>

<?php
function writeMyName()
{
echo “Michelle”;
}

echo “Hello world!<br />”;
echo “My name is “;
writeMyName();
echo “.<br />That’s right, “;
writeMyName();
echo ” is my name.”;
?>

</body>
</html>

The output of the code above will be:

Hello world!
My name is Michelle.
That’s right, Michelle is my name.

PHP + MySQL

Wednesday, January 16th, 2008

PHP stands for PHP: Hypertext Preprocessor. It is a server-side scripting language wherein the scripts are executed on the server itself. PHP is an open source software and is free to download and use. MySQL is a database server that is ideal for both small and large applications. MySQL can compile on a number of platforms and just like PHP, it is free to download and use. Both are open to everybody who wants to learn more on this.

PHP combined with MySQL can create a cross-platform which means that you can freely develop in Windows and serve on a Unix platform.

PHP Installation

Thursday, January 10th, 2008

So what do you need when installing PHP?

If your server supports PHP - you don’t need to do anything! You do not need to compile anything or install any extra tools  - just create some .php files in your web directory - and the server will parse them for you. Most web hosts offer PHP support.

However, if your server does not support PHP, you must install PHP. Below is a link to a good tutorial from PHP.net on how to install PHP5:

http://www.php.net/manual/en/install.php

PHP > Download PHP for free here: http://www.php.net/downloads.php

MySQL > Download MySQL for free here: http://www.mysql.com/downloads/index.html

Apache > Download Apache for free here: http://httpd.apache.org/download.cgi


Bad Behavior has blocked 238 access attempts in the last 7 days.