Hosting The Right Way

Stop Parking Domain Names
Develop Your Domain Names

Developing State-enabled Applications With PHP


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, sometimes the website needs to remember the actions (e.g. choices) performed by the user. For example, in a website that sells DVDs, the user typically browses through a list of DVDs and selects individual DVDs for check out at the end of the shopping session. The website needs to remember which DVDs the user has selected because the selected items needs to be presented again to the user when the user checks out. In other words, the website needs to remember the State - i.e. the selected items - of the user's browsing activities.

However, HTTP is a Stateless protocol and is ill-equipped to handle States. A standard HTML website basically provides information to the user and a series of links that simply directs the user to other related web pages. This Stateless nature of HTTP allows the website to be replicated across many servers for load balancing purposes. A major drawback is that while browsing from one page to another, the website does not remember the State of the browsing session. This make interactivity almost impossible.

In order to increase interactivity, the developer can use the session handling features of PHP to augment the features of HTTP in order to remember the State of the 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 in the browser. Browsers are allowed to keep up to 20 cookies for each domain and the values stored in the cookie cannot exceed 4 KB. If more than 20 cookies are created by the website, only the 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 remember the State of browsing session

2. The server creates a cookie and uses the Set-Cookie header field in the HTTP response to pass the cookie to the browser

3. The browser reads the cookie field in the HTTP response and stores the cookie

4. This cookie information is passed along future browser-server communications and can be used in the 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 identify the cookie

2. value - the value of the cookie - e.g. if the cookie is used to store the name of the user, the value parameter will store the actual name - e.g. John

3. expiration_date - the lifetime of the cookie. After this date, the cookie expires and is unusable

4. path - the path refers to the URL from which the cookie is valid and allowed

5. domain - the domain the created the cookie and is allowed to read the contents of the cookie

6. secure - specifies if the cookie can be sent only through a secure connection - e.g. SSL enable sessions

The following is an example that displays to the user how many times a specific web page has been displayed to the user. Copy the code below (both the php and the html) into a file with the .php extension and test it out.

[?php //check if the $count variable has been associated with the 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...

Installment 3

PHP Session Handling - Cookies Enabled

Instead of storing session information at the browser through the use of cookies, the information can instead be stored at the server in session files. One session file is created and maintained for each user session. For example, if there are three concurrent users browsing the website, three session files will be created and maintained - one for each user. The session files are deleted if the session is explicitly closed by the PHP script or by a daemon garbage collection process provided by PHP. Good programming practice would call for sessions to be closed explicitly in the script.

The following is a typical server-browser sequence of events that occur when a PHP session handling is used:

1. The server knows that it needs to remember the State of browsing session

2. PHP generates a sssion ID and creates a session file to store future information as required by subsequent pages

3. A cookie is generated wih the session ID at the browser

4. This cookie that stores the session ID is transparently and automatically sent to the server for all subsequent requests to the server

The following PHP session-handling example accomplishes the same outcome as the previous cookie example. Copy the code below (both the php and the html) into a file with the .php extension and test it out.

[?php //starts a session session_start();

//informs PHP that count information needs to be remembered in the session file if (!session_is_registered("count")) {

session_register("count");

$count = 0; } else {

$count++; }

$session_id = session_id(); ?]

[html]

[head]

[title]PHP Session Handling - Cookie-Enabled[/title]

[/head]

[body]

The current session id is: [?=$session_id ?]

This page has been displayed: [?=$count ?] times.

[/body] [/html]

A summary of the functions that PHP provides for session handling are:

1. boolean start_session() - initializes a session

2. string session_id([string id]) - either returns the current session id or specify the session id to be used when the session is created

3. boolean session_register(mixed name [, mixed ...]) - registers variables to be stored in the session file. Each parameter passed in the function is a separate variable

4. boolean session_is_registered(string variable_name) - checks if a variable has been previously registered to be stored in the session file

5. session_unregister(string varriable_name) - unregisters a variable from the session file. Unregistered variables are no longer valid for reference in the session.

6. session_unset() - unsets all session variables. It is important to note that all the variables remain registered.

7. boolean session_destroy() - destroys the session. This is opposite of the start_session function.

The next installment discusses how to manage sessions using PHP session handling functions when cookies are disabled...

Installment 4

PHP Session Handling - Without Cookies

If cookies are disabled at the browser, the above example cannot work. This is because although the session file that stores all the variables is kept at the server, a cookie is still needed at the browser to store the session ID that is used to identify the session and its associated session file. The most common way around this would be to explicitly pass the session ID back to the server from the browser as a query parameter in the URL.

For example, the PHP script generates requests subsequent to the start_session call in the following format: http://www.yourhost.com/yourphpfile.php?PHPSESSID=[actual session ID]

The following are excerpts that illustrate the discussion:

Manually building the URL:
$url = "http://www.yoursite.com/yourphppage.php?PHPSESSID=" . session_id(); [a href="[?=$url ?]"]Anchor Text[/a]

Building the URL using SID:
[a href="http://www.yoursite.com/yourphppage.php?[?=SID ?]"]Anchor Text[/a]

Used with the author's permission.

This article is written by John L.
John L is the Webmaster of Designer Banners (http://www.designerbanners.com).

How To Make Money With Expired Domain Names

Other Article Sites

findabook.com  moneycd.info  a-mortgage.info   about-lemon-laws.info  aboutstudentloans.info
all-about-publishing.info  auctions-articles.info  bestcollege-university.com  bestispconnection.com
biblefolder.com  blogger-website.com  books-used.info  brokers-guide.info  buywindows.info  cable-dsl.info
career-miner.com  carpel-tunnel.info  cashinaflash.info  cashloanreviews.info  casinobell.com  chat-house.info
clearmycredit.info  collegeloantips.info  crones.info  depression-articles.info   dirnic.net  dishguides.info
divers-below.com  expodog.info   financewizz.com  fire-insurance.info  getgood.info  handleit.net   it-idea.info
health-supplies.info  hosting-right.com  insidealert.com  insurance-facts.info  jobs-employment.info
justgood.info  lookgold.net   lowcost-travel.info  money-source.info  myhostzone.info  numisblog.com
peoplesearchfinder.info  pr-articles.info  realeas.com   refinancing-guides.info  spyware-remove.info
telelot.info  the-law.info   toppaid.info  travel-deals.info  travelcorrect.com  wedding-guide-site.com
your-blog.info  your-credit.info

MORE ARTICLES:
7 Things You Can Do Today To Make Google Fall Head Over Heels In Love With Your Small Business Website
Creating your own small business website can be a scary thing to do. It requires some planning to get it right and make Google sit up and take notice of your site at first glance. Small business website search engine optimization (SEO) is one of the keys, but all too often, astute business people leave the creation of their websites up to a family member or a friend who happens to know something about it. This is strange, for they wouldn't dream of letting the same people build their bricks and mortar store or office.

Leadership Safaris Launches New Website
Overcome challenges of the business jungle; let us guide your leadership journey. Leadership Safaris, Inc. is thrilled to announce the launch of its new website. Specializing in Management and Leadership Solutions to help drive profits, our business leaders and executive coaches are ready to support you to achieve the next level of success for you, your team and your business.

Arizona Web TV Launches Global Missing Children Website - MissingChildrenWebTV.com - to Show Video Clips of Missing Children
Danielle Hampson, a Scottsdale, AZ entrepreneur, founder of Arizona webTV LLC and The Art of Networking LLC, is concerned for missing children and their families. MissingChildrenWebTV.com (www.MissingChildrenWebTV.com) is a 100 % non-revenue generation initiative for Hampson who has two goals. The first is to help the families left without their loved ones. Her second goal is to develop a program to bring more awareness to this human tragedy.

New Website Launch Allows Wealthy Women to Help Others Achieve Prosperity
This month marks the achievement of a milestone for Wealthy Women LLC, with the launch of their new website, www.breathingprosperity.com. The site was created as a unique resource for people looking to expand their wealth consciousness and define and pursue their dreams.

Home Business; Free Hosting Versus Paid For Your Website
The thing about free hosting and free auto responders is, you will likely have to put up with a lot of advertising (not your own) put there by the host. This is also why I don't believe in banner exchanges.

Closing the Deal: Ecommerce Tips
Basically, ecommerce can be defined as the buying and selling of products or services online. Ecommerce has many advantages over traditional "brick and mortar" companies. Consumers can quickly and efficiently browse through hundreds of products or services without ever leaving the comfort of their home, or compare prices and products with the click of a button.

Get More Web Site Traffic With Free Methods
While there are many ways to jump start a websites traffic intake, some webmasters simply don't have the necessary resources to take action like some of the big guys. But luckily, there are a few ways to get increased web site traffic for free! You won't have to spend any of your hard earned money, yet you will be able to generate as much website traffic as you want. Sounds like a dream come true, right? Well it is! However, to make that dream a reality, you will have to motivate yourself to work harder and get more done.

Telecommuting Website (Part One)
"I've heard you talk about building a website to get telecommuting jobs. Can you tell me more about this, and how I would do it?"If you all listen to WAHMTalkRadio.

How To Take One Small Step Onto A New Revolution And Turn Your Website Into A Money Maker!
For a while now, maybe you?ve been watching the digital revolution pass you by. You hear stories about regular people stumbling upon some idea, putting up a website, and making a mint. Then maybe you think, ?I wish that was me!? Or maybe you already have a great website, but it doesn?t have an angle into making real money.

New York City Sightseeing Attraction Launches New Interactive Website
New Website Offers a Glimpse into NY SKYRIDE, the Most Exhilarating New York City Sightseeing Attraction. Visitors see why a NYC Tour Starts at www.NYSKYRIDE.com

Internet Advertising: What Is An Advertising Website?
The key to increasing the volume of sales on your website is to increase the number of visitors you get by effective use of internet advertising The advertising must not only reach many people, it must reach many of the right people

Deep Dive Media Launches Addiction Web Site
Deep Dive Media LLC announces the launch of ReadyToQuit.com - a web portal and online community designed to arm addicts and loved ones with the resources to fight the disease of addiction in a healing and encouraging environment.

Basic Guide to Building Better Website Traffic
Building an online business would of course require a lot of things, to get straight to the point; you need to generate traffic, with the versatility the basic guides to building better website traffic, there are many ways you could find that could help optimize the potential of your site or online business in generating traffic.

Website Design: Redecorate Your Web Site To Increase Sales
Getting traffic to your website is not the only factor that affects your sales.Your website has to be powerful enough to convert these traffic and visitors to buyers.

Web Design and Search Engine Optimization Company, Dise?o Web y posicionamiento
Web site design and search engine optimization has taken new dimensions in the Web2.0 era. With the stabilization of the American economy, web businesses have again started with a new zeal. Staying competitive in the new era is all about staying ahead in the search engine rankings. As a result they are taking help from professional search engine optimization companies to boost up their web rankings. Costa Rica is a country where e-commerce websites have started in a big way and as a result seo companies are in much demand as web design companies. If you are planning to launch a website in Costa Rica, you have many options to get your website designed and optimized. There are some firms which give end-to-end solutions from web designing to...

Develop Your Domain Names | Site Map | Home

Privacy Policy | Copyright/Trademark Notification