Developing State-enabled Applications With PHP

Written by John L


Installment 1

Developing State-enabled Applications With PHP

When a user is browsing through a website and is surfing from one web page to another, sometimesrepparttar website needs to rememberrepparttar 105569 actions (e.g. choices) performed byrepparttar 105570 user. For example, in a website that sells DVDs,repparttar 105571 user typically browses through a list of DVDs and selects individual DVDs for check out atrepparttar 105572 end ofrepparttar 105573 shopping session. The website needs to remember which DVDsrepparttar 105574 user has selected becauserepparttar 105575 selected items needs to be presented again torepparttar 105576 user whenrepparttar 105577 user checks out. In other words,repparttar 105578 website needs to rememberrepparttar 105579 State - i.e.repparttar 105580 selected items - ofrepparttar 105581 user's browsing activities.

However, HTTP is a Stateless protocol and is ill-equipped to handle States. A standard HTML website basically provides information torepparttar 105582 user and a series of links that simply directsrepparttar 105583 user to other related web pages. This Stateless nature of HTTP allowsrepparttar 105584 website to be replicated across many servers for load balancing purposes. A major drawback is that while browsing from one page to another,repparttar 105585 website does not rememberrepparttar 105586 State ofrepparttar 105587 browsing session. This make interactivity almost impossible.

In order to increase interactivity,repparttar 105588 developer can userepparttar 105589 session handling features of PHP to augmentrepparttar 105590 features of HTTP in order to rememberrepparttar 105591 State ofrepparttar 105592 browsing session. The are basically 2 ways PHP does this: 1. Using cookies 2. Using Sessions

The next installment discusses how to manage sessions using cookies...

Installment 2

Cookies

Cookies are used to store State-information inrepparttar 105593 browser. Browsers are allowed to keep up to 20 cookies for each domain andrepparttar 105594 values stored inrepparttar 105595 cookie cannot exceed 4 KB. If more than 20 cookies are created byrepparttar 105596 website, onlyrepparttar 105597 latest 20 are stored. Cookies are only suitable in instances that do not require complex session communications and are not favoured by some developers because of privacy issues. Furthermore, some users disable support for cookies at their browsers.

The following is a typical server-browser sequence of events that occur when a cookie is used: 1. The server knows that it needs to rememberrepparttar 105598 State of browsing session 2. The server creates a cookie and usesrepparttar 105599 Set-Cookie header field inrepparttar 105600 HTTP response to passrepparttar 105601 cookie torepparttar 105602 browser 3. The browser readsrepparttar 105603 cookie field inrepparttar 105604 HTTP response and storesrepparttar 105605 cookie 4. This cookie information is passed along future browser-server communications and can be used inrepparttar 105606 PHP scripts as a variable

PHP provides a function called setcookie() to allow easy creation of cookies. The syntax for setcookie is: int setcookie(string name, [string val], [int expiration_date], [string path], string domain, [int secure])

The parameters are: 1. name - this is a mandatory parameter and is used subsequently to identifyrepparttar 105607 cookie 2. value -repparttar 105608 value ofrepparttar 105609 cookie - e.g. ifrepparttar 105610 cookie is used to storerepparttar 105611 name ofrepparttar 105612 user,repparttar 105613 value parameter will storerepparttar 105614 actual name - e.g. John 3. expiration_date -repparttar 105615 lifetime ofrepparttar 105616 cookie. After this date,repparttar 105617 cookie expires and is unusable 4. path -repparttar 105618 path refers torepparttar 105619 URL from whichrepparttar 105620 cookie is valid and allowed 5. domain -repparttar 105621 domainrepparttar 105622 createdrepparttar 105623 cookie and is allowed to readrepparttar 105624 contents ofrepparttar 105625 cookie 6. secure - specifies ifrepparttar 105626 cookie can be sent only through a secure connection - e.g. SSL enable sessions

The following is an example that displays torepparttar 105627 user how many times a specific web page has been displayed torepparttar 105628 user. Copyrepparttar 105629 code below (bothrepparttar 105630 php andrepparttar 105631 html) into a file withrepparttar 105632 .php extension and test it out.

[?php //check ifrepparttar 105633 $count variable has been associated withrepparttar 105634 count cookie if (!isset($count)) { $count = 0; } else { $count++; } setcookie("count", $count, time()+600, "/", "", 0); ?]

[html] [head] [title]Session Handling Using Cookies[/title] [/head] [body] This page has been displayed: [?=$count ?] times. [/body] [/html]

The next installment discusses how to manage sessions using PHP session handling functions with cookies enabled...

MySQL Database Handling in PHP

Written by John L


Most interactive websites nowadays require data to be presented dynamically and interactively based on input fromrepparttar user. For example, a customer may need to log into a retail website to check his purchasing history. In this instance,repparttar 105568 website would have stored two types of data in order forrepparttar 105569 customer to performrepparttar 105570 check –repparttar 105571 customer’s personal login details; andrepparttar 105572 customer’s purchased items. This data can be stored in two types of storage – flat files or databases.

Flat files are only feasible in very low to low volume websites as flat files have 3 inherent weaknesses: 1.The inability to indexrepparttar 105573 data. This makes it necessary to potentially read ALLrepparttar 105574 data sequentially. This is a major problem if there are a lot of records inrepparttar 105575 flat file becauserepparttar 105576 time required to readrepparttar 105577 flat file is proportionate torepparttar 105578 number of records inrepparttar 105579 flat file. 2.The inability to efficiently control access by users torepparttar 105580 data 3.The inefficient storage ofrepparttar 105581 data. In most cases,repparttar 105582 data would not be encrypted or compressed as this would exacerbaterepparttar 105583 problem no. 1 above

The alternative which is, in my opinion,repparttar 105584 only feasible method, is to storerepparttar 105585 data in a database. One ofrepparttar 105586 most prevalent databases in use is MySQL. Data that is stored in a database can easily be indexed, managed and stored efficiently. Besides that, most databases also provide a suite of accompanying utilities that allowrepparttar 105587 database administrator to maintainrepparttar 105588 database – for example, backup and restore, etc.

Websites scripted using PHP are very well suited forrepparttar 105589 MySQL database as PHP has a custom and integrated MySQL module that communicates very efficiently with MySQL. PHP can also communicate with MySQL throughrepparttar 105590 standard ODBC as MySQL is ODBC-compliant, However, this will not be as efficient as usingrepparttar 105591 custom MySQL module for PHP.

The rest of this article is a tutorial on how to use PHP to: 1.Connect to a MySQL database 2.Execute standard SQL statements againstrepparttar 105592 MySQL database

Starting a Session with MySQL

Beforerepparttar 105593 PHP script can communicate withrepparttar 105594 database to query, insert or updaterepparttar 105595 database,repparttar 105596 PHP script will first need to connect torepparttar 105597 MySQL server and specify which database inrepparttar 105598 MySQL server to operate on.

The mysql_connect() and mysql_select_db() functions are provided for this purpose. In order to connect torepparttar 105599 MySQL server,repparttar 105600 server name/address; a username; and a valid password is required. Once a connection is successful,repparttar 105601 database needs to be specified.

The following 2 code excerpts illustrate how to performrepparttar 105602 server connection and database selection: @mysql_connect("[servername]", "[username]", "[password]") or die("Cannot connect to DB!"); @mysql_select_db("[databasename]") or die("Cannot select DB!");

Cont'd on page 2 ==>
 
ImproveHomeLife.com © 2005
Terms of Use