Tuesday, December 2, 2008

Part 5: Managing Sessions

What is a Session Variable?
Session variables are similar to cookies, that is they are used to store information for a particular period of time. The values in session variables exist only till the session exists. They are used to carry information from one page to another of a web site. We can create a session using a session identifier and store it in the server. When the client makes any request, the data stored in the session variable can be accessed by PHP any number of times until the session is ended.

Why do we need a Session Variable?
In web sites, passing information between pages using a query string is very difficult. For example once you log into a site, passing the username to all the pages of the site using query string is very difficult, but this can be done easily using Session variables. These variables can be used to pass information from one page to another without using a query string, since it is easy to maintain and retrieve.

How to set Session Variables?
Session variables can set using 'session_register' function.

Part4: Functions in PHP

In this part 4, you'll learn about Functions in PHP. This includes - What is a Function?, How to define and call functions in PHP.

What is a Function?

A Function is a small set of statements defined by the programmer to do a specific action. They take input values in the form of 'arguments' and return values after execution. They can be written anywhere in the program. They are used to reduce the programming complexity and to handle the programming structures easily. The function takes an input, performs some operation with it and returns a value after successful execution. Functions are basically of two types, namely:

Functions with no return value
Functions with return values


How to define & call functions?

Functions are defined using the keyword 'function' followed by the function name. The input parameters are listed in parentheses after the function name, followed by the function code within braces after the arguments

Part 3: Using Cookies in PHP

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.

How to create a cookie?

Cookies can be set using the 'setcookie' function in PHP. This function takes three arguments - name of the variable, value of the variable and the expiry time period.