Thursday, March 1, 2018

PHP Unit 3 : Handling Form, Session Traking and PHP Components for B.C.A., M.C.A. and all IT Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
Contact No : 7600044051
E-Mail : rupareleducation@gmail.com



Unit 3: Handling Form, Session Traking & PHP Components in PHP

Explain Cookies

·        Cookies are text files stored on the client side computer that can each contain around 4000 characters. Up to 20 cookies can store for each website, and the client computer can store a maximum of 300 cookies in total. Cookie files can be opened with any text editor so sensitive information should be encrypted.
·        They are useful to retain user preferences, shopping cart section and other part of data. For instance, rather than directly store a user’s password in a cookie it is better to store a unique identification string that references a database entry containing different cookies. Cookies are located in

C:\document and setting\admin\cookies

·        Cookies are the temporary storage area where the information is to be stores. The cookies are seen in the address bar of the browser, history of the websites visited etc…
·        The cookies which are created are presented in the local machines.
·        The activated cookies are stored in the form of text document where in the information about the cookies name, time of visit etc stored.
·        For an individual website individual text file is created in local machines.
·        To create cookie the function setcookie () is used.

Syntax: setcookie(cookie name, cookie value)
         
To display cookie (the cookie value ,the global variable $_COOKIE[‘cookie name’]) is used.

Example:

<?php
Setcookie(“Tempcookie”,”cookie will expire in 1 minute”,time() + 60);
echo $_COOKIE[“tempcookie”];
?>

PHP SESSION
Session is the temporary storage area where the information is to be stored. The sessions are used when there is any kind of login.
The login can be found in any E-mail account, shopping cart, downloading any projects or software.
Where there is login there is session. Once the user gets login the username of the user is developed on each page until the user gets logout.
So entire session depends on the user, it gets started when the user logins and it gets destroyed when user logouts.

To create session two steps are there as shown below:
ð      start the session
ð      set the value of the session
o       To start the session the session_start() is used.
o       To set the session value the global variable $_SESSION [‘session_name’] is used.

ð      Next to display the session, the session values there are two steps:
o       Start the session
o       Display using the echo function. 
§        Syntax:
 echo $_SESSION[‘session_name’];

ð      To destroy the session  there are 3 steps
o       Start session
o       Clear the session value
o       Destroy the session
§        To clear the session the function unset is used. Syntax is unset($_SESSION[‘session_name’])
§        To destroy the session the function session_destroy() is used.

Example: Login.php
<?php
$user=”admin”;
$pass=”admin123”;
If($user==”admin” && $pass==”admin123”)
{
                    session_start();
                    $_SESSION [‘username’]=”admin”;
                    header (“Location:Next.php”);
}
?>

Example: Next.php
<?php
session_start();
echo $_SESSION[‘username’].”<a href=’logout.php’>Logout</a>”;
?>



Ex. Logout.php
<?php
          session_start();
          unset($_SESSION[‘username’]);
          session_destroy();
?>

ð      session can be created using session_id() function. This function either returns the value in the string format. It returns the arbitrary session_id , If the session is set.

ð      Syntax: string session_id([‘session_id’]);

ð      The session can also be created using session_name() function. It is similar to the session id except that it gets or sets session_name.

ð      Syntax: string session_name([‘session_name’]);

Explain Server Variables

HTTP Cookies : $_COOKIE:

An associative array of variables passed to the current script via HTTP cookies. Automatically global in any scope.
This is a super global or automatic global variable. This is simply means that it is available in all scope throughout a script.
To access it within function or method as you do with

$HTTP_COOKIE_VARS.
 <?php
Setcookie(“name”,”World”);
Echo “Hello” .htmlspecialchars($_COOKIE[“name”]);
?>

HTTP GET variables: $_GET:

An associative array of variables passed to the current script via HTTP get method. Automatically global in any scope.
This is a superglobal or automatic global variable. This is simply means that it is available in all scope throughout a script.
To access it within function or method as you do with $HTTP_GET_VARS.
<?php
          echo “Hello”.htmlspecialchars($_GET[“name”]);
?>

HTTP POST variables: $_POST:

An associative array of variables passed to the current script via HTTP POST method. Automatically global in any scope.
This is a superglobal or automatic global variable. This is simply means that it is available in all scope throughout a script.
To access it within function or method as you do with $HTTP_POST_VARS.

Ex: <?php
echo “Hello”.htmlspecialchars($_POST[“name”]);
?>

Request variables: $_REQUEST:
An associative array consisting of the contents of $_GET, $_POST and $_COOKIE.
This is a superglobal or automatic global variable. This is simply means that it is available in all scope throughout a script.
Global Variables:$_GLOBALS:
An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.
Server variables: $_SERVER:
$_SERVER is an array containing information such as headers, path and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these:
                   To access it within $_HTTP_SERVER_VARS.

Some of the server variables are as under.
·        GATEWAY_INTERFACE
·        SERVER_NAME
·        SERVER_SOFTWARE
·        SERVER_PROTOCOL
·        REQUEST_METHOD
·        REQUEST_TIME
·        QUERY_STRINGHTTP_ACCEPT

Explain GD Library.
·        Php is a general purpose language used to develop web application with a full featured database driven web site.
·        The use of php is suitable for tracking quick development of huge variety of project along with its extremely each learning curve.
·        PHP has powerful capabilities for generating images through its popular GD  libraries.
·        The extension has been available for a long time to php developers but quiet frequently its functionality has been required the web application implementation dynamically.
·        GD library comes bundled with both php 4 and php 5 but many web developers do not take full advantages of its capabilities.
·        It is extremely useful in the situation where it is necessary to display number of graphics that need one or more dynamic components. 
·        GD it self can work with a multiple images but when you use with php you can get information about any GIF,JPEG,SWF,TIFF,BMP,PSD,XBM image format.
·        You can also manipulate and create images in GIF,JPEG,PNG,WBMP image format.
·        GD can also allow php to create such as square, polygon and ellipse as well as text box using true type font.

In GD library different functions are used like,

·        gd_info(): Used to get the current installed GD library
·        getimagesize() : to get the size of image
·        imagecreatetruecolor(): used to create a new true color image\
·        imagecolorallocate() : used to allocate a color for an image
·        imagearc(): used to create an arc image
·        imagepolygon() :used to draw polygon image
·        imagerectangle(): used to draw rectangle image
·        imagepng() : used to display png image
·        imagejpeg(): used to display jpeg image
·        imageline(): This function is draws a line between the two given points.
·        Imageellipse(): This function draws an ellipse centered at the specified coordinates.
·        Imagefilledpolygon(): This function creates a filled polygon in the given image.
·        Imagefilledrectangle(): This function creates a filled rectangle with the color in the given image starting at point 1 and ending at point 2.0,0 is the top left corner of the image.
·        Imagedestroy(): This function is destroying the image.Returns TRUE on success or false on failure.

Explain PHP regular expression:
·        Regular expression means the formula and general format and any string or expression. It is used for pattern matching.
·        It is also known as regexis. Regular expression are used for complex string manipulation in PHP. It is also used for the validation on server side script.

Advantages:
ð      It is portable the data to be entered in invalid format.
ð      It is a portable language as it has general format.
ð      It is easy to learn and use.

Disadvantages:
ð      The validation is provided using server side scripting , which is the main disadvantage because the webpage is refresh again and again.

There are two types of regular expression.
1.       POSIX extended  regular expression.
2.       PERL compatible regular expression.

POSIX extended regular expression:
When this type of regular expression is used at that time the ereg() function is required to be used the pattern matching. This function is returns the length of the match if the regular expression matches the string. Ereg is abbreviated as Extended regular expression.

PERL compatible regular expression.
This type is developed using the PERL language. The function which is used for the given regular expression is preg() means Perl regular expression.
Explain uploading a php file.
A php script can be used with HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a php script.
Information in the phpinfo.php page describes the temporary  directory that is used for file uploads as upload_tmp_dir and the maximum permitted size of the files that can be uploaded is stated as upload_max_filesize.
The process of uploading a file follows these steps.
·        The user open the page containing HTML form featuring  text file, a browse button and a submit button.
·        The user clicks the browse button and select a file to upload from the local pc.
·        The full path to the selected file appears in the text then the user clicks on the submit button
·        The selected file is sent to the temp. directory on the server.
·        The php script that was specified as the form handler in the form’s action attribute that checks the file and then copies the file into an intended directory.
·        An uploaded file could be a text file , image file or any document.

Explain How to sending mail using PHP mail function.
To active the mail() function for PHP it must be configured correctly in the php.ini file. Open php.ini file available in directory and find the section headed  mail function.              
Simply need to let PHP know the location of their sendmail applications. The path and any desired switch should be specified to the sendmail_path directive.
PHP provides a convenient way to send email with the mail() function.

     Syntax:  mail( to,subject,message, headers, parameters);

Ex: sending simple text email using mail()
  <?php
                   $to=”email”;
                   $subject=”This is subject”;
                   $message=”This is simple text message”;
                   $header=”From:email\r\n”;
                   $retval=mail($to,$subject,$message,$header);
                   if($retval==true)
                   {
                             echo “Message sent successfully”;
                   }
                   else
                   {
                              echo “Message could not be sent”;
                   }
?>


Best Of Luck 

Please Share and Comment This Post.... Thank You