close

Installment 1

Developing State-enabled Applications With PHP

When a person is reading finished a website and is surfboarding from one web leaf to another, sometimes the website of necessity to recall the schedule (e.g. choices) performed by the somebody. For example, in a website that sells DVDs, the user typically browses finished a roll of DVDs and selects separate DVDs for cheque out at the end of the purchasing conference. The website wants to remind which DVDs the user has elected because the select items inevitably to be given once more to the user once the user checks out. In remaining words, the website requests to recall the State - i.e. the elite items - of the user's reading actions.

However, HTTP is a Stateless prescript and is ill-equipped to pedal States. A regulation HTML website primarily provides facts to the mortal and a set of links that simply directs the soul to otherwise correlative web pages. This Stateless temperament of HTTP allows the website to be replicated intersectant plentiful servers for consignment reconciliation purposes. A starring consequence is that spell browse from one page to another, the website does not think the State of the reading conference. This trademark interactivity well-nigh impractical.

In command to escalate interactivity, the developer can use the meeting manual labor features of PHP to increase the features of HTTP in directive to remember the State of the reading conference. The are simply 2 way PHP does this:

1. Using cookies

2. Using Sessions

The adjacent installment discusses how to conduct operations sessions using cookies...

Installment 2

Cookies

Cookies are in use to lumber room State-information in the looker. Browsers are allowed to hold on to up to 20 cookies for each sphere and the belief keep in the cake cannot carry too far 4 KB. If more than 20 cookies are created by the website, singular the hottest 20 are keep. Cookies are sole opportune in instances that do not need thorny session subject area and are not favourite by few developers because of shelter issues. Furthermore, both users disenable utilize for cookies at their browsers.

The shadowing is a standard server-browser sequence of dealings that fall out once a cookie is used:

1. The restaurant attendant knows that it requirements to retrieve the State of browse session

2. The waiter creates a cooky and uses the Set-Cookie line parcel of land in the HTTP comeback to leave behind the biscuit to the browser

3. The witness reads the biscuit area in the HTTP answer and stores the cookie

4. This cake records is passed on impending browser-server study and can be used in the PHP scripts as a variable

PHP provides a work named setcookie() to let smooth creative activity of cookies. The sentence structure for setcookie is:
int setcookie(string name, [string val], [int expiration_date], [string trail], twine domain, [int support])

The parameters are:

1. name - this is a obligatory parametric quantity and is utilized subsequently to place the cookie

2. value - the worth of the cookie - e.g. if the cake is used to storehouse the describe of the user, the plus parametric quantity will cache the actualised describe - e.g. John

3. expiration_date - the lifespan of the biscuit. After this date, the biscuit expires and is unusable

4. boardwalk - the footsteps refers to the URL from which the biscuit is reasoned and allowed

5. domain - the sphere the created the cooky and is allowed to read the tabular array of the cookie

6. out of harm's way - specifies if the cooky can be transmitted lonesome done a unafraid connexion - e.g. SSL change sessions

The shadowing is an instance that displays to the mortal how some times a particularized web leaf has been displayed to the individual. Copy the code at a lower place (both the php and the html) into a data file with the .php delay and assessment it out.

[?php
//check if the $count irregular has been associated next to the put a figure on cookie
if (!isset($count)) {

$count = 0;
} other {

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

[html]

[head]

[title]Session Handling Using Cookies[/title]

[/head]

[body]

This leaf has been displayed: [?=$count ?] modern world.

[/body]
[/html]

The adjacent installment discusses how to win composer victimization PHP meeting handling functions near cookies enabled...

Installment 3

PHP Session Handling - Cookies Enabled

Instead of storing group discussion records at the browser finished the use of cookies, the records can alternatively be keep at the dining-room attendant in conference files. One conference record is created and preserved for all soul conference. For example, if location are iii synchronized users browse the website, iii session files will be created and well-kept - one for all somebody. The group discussion files are deleted if the conference is explicitly obstructed by the PHP letters or by a fiend food waste collection manoeuvre provided by PHP. Good scheduling run through would telephone call for composer to be out of use shockingly in the scrawl.

The shadowing is a usual server-browser series of dealings that take place once a PHP session manual labor is used:

1. The waiter knows that it requests to recollect the State of reading session

2. PHP generates a sssion ID and creates a group discussion file to storehouse impending rumour as necessary by later pages

3. A biscuit is generated next to the conference ID at the browser

4. This cooky that stores the group discussion ID is transparently and unthinkingly sent to the restaurant attendant for all sequent requests to the server

The stalking PHP session-handling trial accomplishes the aforementioned result as the ex- cake paradigm. Copy the symbols to a lower place (both the php and the hypertext mark-up language) into a file beside the .php extension and testing it out.

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

//informs PHP that count information necessarily to be remembered in the conference 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 ongoing session id is: [?=$session_id ?]

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

[/body]
[/html]

A drumhead of the functions that PHP provides for meeting handling are:

1. boolean start_session() - initializes a session

2. cord session_id([string id]) - any returns the current session id or undertake the session id to be in use once the conference is created

3. boolean session_register(mixed given name [, an assortment of ...]) - registers variables to be hold on in the group discussion folder. Each constant quantity passed in the work is a segregated variable

4. boolean session_is_registered(string variable_name) - checks if a unsettled has been once registered to be hold on in the conference file

5. session_unregister(string varriable_name) - unregisters a uncertain from the session wallet. Unregistered variables are no long logical for citation in the session.

6. session_unset() - unsets all conference variables. It is big to facts that all the variables stay registered.

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

The close installment discusses how to do admin composer victimization PHP meeting manual labor functions once cookies are unfit...

Installment 4

PHP Session Handling - Without Cookies

If cookies are handicapped at the browser, the preceding taster cannot toil. This is because tho' the conference wallet that stores all the variables is unbroken at the server, a cookie is still requisite at the browser to stash the meeting ID that is nearly new to set the meeting and its associated meeting record. The peak agreed way about this would be to explicitly overrun the session ID support to the restaurant attendant from the witness as a inquiry parametric quantity in the URL.

For example, the PHP writing generates requests subsequent to the start_session give the name in the shadowing format:
[actual meeting ID]

The tailing are excerpts that exposit the discussion:

Manually construction the URL:

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

Building the URL exploitation SID:

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

arrow
arrow
    全站熱搜

    eroop 發表在 痞客邦 留言(0) 人氣()