Web-development informational articles

Mounting state-enabled applications with php - web-development


Installment 1

Developing State-enabled Applications With PHP

When a user is browsing all the way through a website and is surfing from one web page to another, at times the website needs to consider the dealings (e. g. choices) performed by the user. For example, in a website that sells DVDs, the user typically browses all through a list of DVDs and selects characteristic DVDs for check out at the end of the shopping session. The website needs to bring to mind which DVDs the user has elected as the chosen items needs to be accessible again to the user when the user checks out. In other words, the website needs to bring to mind the State - i. e. the select items - of the user's browsing activities.

However, HTTP is a Stateless protocol and is ill-equipped to code name States. A accepted HTML website essentially provides in order to the user and a run of links that easily directs the user to other connected web pages. This Stateless characteristics of HTTP allows the website to be imitation athwart many servers for load balancing purposes. A major drawback is that while browsing from one page to another, the website does not consider the State of the browsing session. This make interactivity approximately impossible.

In order to add to interactivity, the developer can use the conference managing skin of PHP to boost the skin of HTTP in order to bear in mind the State of the browsing session. The are all in all 2 ways PHP does this:

1. Using cookies
2. Using Sessions

The next chapter discusses how to deal with sessions using cookies. . .

Installment 2


Cookies are used to store State-information in the browser. Browsers are permissible to keep up to 20 cookies for each area and the principles stored in the cookie cannot exceed 4 KB. If more than 20 cookies are bent by the website, only the most up-to-date 20 are stored. Cookies are only apposite in instances that do not compel composite meeting data lines and are not favoured by some developers as of privacy issues. Furthermore, some users disable assist for cookies at their browsers.

The subsequent is a average server-browser chain of actions that occur when a cookie is used:

1. The head waiter knows that it needs to bear in mind the State of browsing session

2. The ma?tre d'h?tel creates a cookie and uses the Set-Cookie description field in the HTTP answer to pass the cookie to the browser

3. The browser reads the cookie field in the HTTP answer and provisions the cookie

4. This cookie in order is accepted along coming browser-server broadcasting and can be used in the PHP scripts as a variable

PHP provides a affair called setcookie() to allow easy conception of cookies. The language rules for setcookie is: int setcookie(string name, [string val], [int expiration_date], [string path], filament domain, [int secure])

The parameters are:

1. name - this is a mandatory parameter and is used subsequently to categorize 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 definite name - e. g. John

3. expiration_date - the existence 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. province - the province the bent the cookie and is acceptable to read the filling of the cookie

6. assured - specifies if the cookie can be sent only all through a assured bond - e. g. SSL permit sessions

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

[?php //check if the $count capricious has been linked with the count cookie if (!isset($count)) {

$count = 0; } else {

$count++; } setcookie("count", $count, time()+600, "/", "", 0); ?]



[title]Session Behavior Using Cookies[/title]



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

[/body] [/html]

The next chapter discusses how to administer sessions using PHP meeting conduct functions with cookies enabled. . .

Installment 3

PHP Assembly Management - Cookies Enabled

Instead of storing meeting in rank at the browser all through the use of cookies, the in rank can as an alternative be stored at the ma?tre d' in gathering files. One gathering file is produced and maintained for each user session. For example, if there are three agreeing users browsing the website, three assembly files will be produced and maintained - one for each user. The assembly files are deleted if the conference is explicitly blocked by the PHP draft or by a daemon nonsense anthology administer provided by PHP. Good training attempt would call for sessions to be bunged explicitly in the script.

The next is a classic server-browser chain of dealings that occur when a PHP gathering managing is used:

1. The ma?tre d' knows that it needs to commit to memory the State of browsing session

2. PHP generates a sssion ID and creates a gathering file to store hope in sequence as necessary by following pages

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

4. This cookie that food the meeting ID is clearly and by design sent to the ma?tre d'h?tel for all consequent requirements to the server

The subsequent PHP session-handling exemplar accomplishes the same outcome as the preceding cookie example. Copy the code below (both the php and the html) into a file with the . php additional room and test it out.

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

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


$count = 0; } else {

$count++; }

$session_id = session_id(); ?]



[title]PHP Conference Managing - Cookie-Enabled[/title]



The in progress gathering id is: [?=$session_id ?]

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

[/body] [/html]

A abridgment of the functions that PHP provides for meeting behavior are:

1. boolean start_session() - initializes a session

2. cord session_id([string id]) - any proceeds the flow meeting id or enumerate the conference id to be used when the assembly is created

3. boolean session_register(mixed name [, mixed . . . ]) - registers variables to be stored in the meeting file. Each parameter approved in the do is a branch out variable

4. boolean session_is_registered(string variable_name) - checks if a capricious has been formerly registered to be stored in the meeting file

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

6. session_unset() - unsets all meeting variables. It is chief to note that all the variables hang about registered.

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

The next part discusses how to deal with sessions using PHP gathering managing functions when cookies are disabled. . .

Installment 4

PHP Gathering Conduct - Lacking Cookies

If cookies are disabled at the browser, the above case cannot work. This is as though the meeting file that provisions all the variables is kept at the server, a cookie is still looked-for at the browser to store the assembly ID that is used to ascertain the meeting and its linked conference file. The most communal way about this would be to explicitly pass the conference ID back to the ma?tre d'h?tel from the browser as a query parameter in the URL.

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

The next are excerpts that illustrate the discussion:

Manually shop 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 commentary is printed by John L.
John L is the Webmaster of Designer Banners (http://www. designerbanners. com).


KPS3 Hires Vivek Bhardwaj as Web Developer  Nevada Business Magazine

Web development company to move to downtown Battle Creek  MiBiz: West Michigan Business News

Junior Web Developer  Charlotte Agenda

Developed by:
home | site map
goldenarticles.net © 2019