Sunday, December 4, 2011

String Functions

Code Example


example.html
<html>
    <body>
        <form action="example.php">
            Enter a string <input type="text" name="string" value="" /><br/>
            <input type="submit" value="   Submit    " name="submit"/>
        </form>
    </body>
</html>
example.php
<?php
 if(isset ($_GET['submit'])){
     $string = $_GET['string'];
     //encrypt the string
     echo 'encrypt the string : '.  crypt($string).'<br/>';

     // lenth of th string  
     echo 'lenth of the string : '.  strlen($string).'<br/>';  

     // possition of a string
     echo 'possition of a in string : '.  strpos($string, 'a').'<br/>';

     //capitalize the first character
     echo 'uper case first char : '.  ucfirst($string).'<br/>';
 }
?>

explode() method

Code Example

split() method is deprecated in php 5.3. Instead of split()  use explode().

explode.php

<?php
$text= "hello friends how are you?";
$newtext= explode(' ', $text);
foreach ($newtext as $str){
    echo $str.'<br/>';
}
?>
output:
hello
friends
how
are
you?

Cookie Example

Code Example

setcookie.php

<?php
//setting a cookie

setcookie('user','Icon Training');

//retrieving a cookie

echo $_COOKIE['user'];
?>
deletecookie.php 

<?php
//deleting the user cookie

setcookie('user','',  time()-3600);

?>

Uploading an image file on server

Code Example

upload.html 
<html>
    <head>
        <title>Upload an image</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="uploadimage.php"  enctype="multipart/form-data" method="post">
            <input type="file" name="file" value="" /><br/>
            <input type="submit" value="   Upload   " />
            
        </form>
    </body>
</html>
uploadimage.php
<?php
$image = $_FILES['file']['name'];
$img_type = $_FILES['file']['type'];
$img_size = $_FILES['file']['size'];
$img_error = $_FILES['file']['error'];
if(!empty ($image) && ($img_type == 'image/gif' || $img_type == 'image/png' || $img_type == 'image/jpeg')
 && $img_size > 0 && $img_size < 40000 && $img_error == 0)
{
    $target="image/".$image;
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
    {
        echo 'image file uploaded successfully';
    }
 else {
        echo 'error';
    }
}
?>

Server side validation

Code Example


registration.html 
<html>
    <head>
        <title>Registration</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="registration.php">
            <table border="0" cellspacing="14">
                <tr>
                    <th colspan="2">Registration form</th>
                </tr>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="fname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email ID</td>
                        <td><input type="text" name="email" value="" /></td>
                    </tr>
                    <tr>
                        <td>Date Of Birth</td>
                        <td><input type="text" name="dob" value="" /></td>
                    </tr>
                    <tr>
                        <td>Phone no: </td>
                        <td><input type="text" name="phone" value="" /></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><input type="text" name="address" value="" /></td>
                    </tr>
                     <tr>
                        <td>Gender</td>
                        <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" />Female</td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="   Submit  " /></td>                    
                    </tr>                
            </table>
        </form>
    </body>
</html>
registration.php
<?php
//accessing form data
    $fname=$_GET['fname'];
    $lname=$_GET['lname'];
    $email=$_GET['email'];
    $dob=$_GET['dob'];
    $phone=$_GET['phone'];
    $address=$_GET['address'];
    $gender=$_GET['gender'];
    if(!empty($fname) && !empty ($lname) && !empty ($email) && !empty ($dob) && !empty ($phone) && !empty ($address) && !empty ($gender))
    {
        if(!is_numeric($fname) && !is_numeric($lname) && !is_numeric($email) && !is_numeric($address) && is_numeric($phone))
        {
            // creating connection
            $conection = mysqli_connect('localhost','root','','php_training') or die();
            $query = "INSERT INTO registrationch03 VALUES(0,'$fname','$lname','$email','$dob','$phone','$address','$gender')";
            $data=  mysqli_query($conection,$query);
    
            echo 'registration successfull.';
  
            mysqli_close($conection);
        }
        else {
            echo 'please enter proper data';
        }
    }
    else{
        echo 'Please enter all details';
    }
?>

Simple Registration form

Code Example


Registration.html

<html>
    <head>
        <title>Registration</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="Registration.php">
            <table border="0" cellspacing="14">
                <tr>
                    <th colspan="2">Registration form</th>
                </tr>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="fname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email ID</td>
                        <td><input type="text" name="email" value="" /></td>
                    </tr>
                    <tr>
                        <td>Date Of Birth</td>
                        <td><input type="text" name="dob" value="" /></td>
                    </tr>
                    <tr>
                        <td>Phone no: </td>
                        <td><input type="text" name="phone" value="" /></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><input type="text" name="address" value="" /></td>
                    </tr>
                     <tr>
                        <td>Gender</td>
                        <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" />Female</td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="   Submit  " /></td>
                    
                    </tr>
                
            </table>

        </form>
    </body>
</html>

Registration.php

<?php
    //accessing data from form 

    $fname=$_GET['fname'];
    $lname=$_GET['lname'];
    $email=$_GET['email'];
    $dob=$_GET['dob'];
    $phone=$_GET['phone'];
    $address=$_GET['address'];
    $gender=$_GET['gender'];
    
    // creating connection

    $conection = mysqli_connect('localhost','root','','php_training') or die();
    $query = "INSERT INTO registrationch03 VALUES(0,'$fname','$lname','$email','$dob','$phone','$address','$gender')";
    $data=  mysqli_query($conection,$query);
    
    echo 'registration successfull.';
  
    mysqli_close($conection);
      
?>