Installment 1

Developing State-enabled Applications With PHP

When a person is browse done a website and is aquatics from one web folio to another, sometimes the website necessarily to think the actions (e.g. choices) performed by the mortal. For example, in a website that sells DVDs, the someone routinely browses through a document of DVDs and selects idiosyncratic DVDs for examine out at the end of the purchasing group discussion. The website needs to call to mind which DVDs the human has select because the special items necessarily to be presented over again to the soul once the person checks out. In else words, the website requests to remind the State - i.e. the elite items - of the user's reading goings-on.

However, HTTP is a Stateless prescript and is unequipped to pedal States. A stock HTML website roughly provides reports to the human and a ordering of links that but directs the user to new concomitant web pages. This Stateless disposition of HTTP allows the website to be replicated crosstown more servers for mountain matching purposes. A stellar downside is that while browsing from one page to another, the website does not think the State of the reading session. This product interactivity nigh impossible.

In bidding to addition interactivity, the creator can use the group discussion manual labour features of PHP to improve the features of HTTP in command to remember the State of the browse group discussion. The are deeply 2 way PHP does this:

1. Using cookies

2. Using Sessions

The adjacent payment discusses how to get by sessions using cookies...

Installment 2

Cookies

Cookies are previously owned to stash State-information in the witness. Browsers are allowed to hold on to up to 20 cookies for all environment and the values keep in the cake cannot beat 4 KB. If more than 20 cookies are created by the website, solely the most up-to-date 20 are keep. Cookies are solitary expedient in instances that do not require knotty conference subject field and are not number one by some developers because of shelter issues. Furthermore, whichever users incapacitate leg for cookies at their browsers.

The following is a emblematic server-browser chain of dealings that transpire once a cake is used:

1. The server knows that it requests to call up the State of browsing session

2. The server creates a biscuit and uses the Set-Cookie line field in the HTTP feedback to leave behind the cookie to the browser

3. The browser reads the cooky pen in the HTTP answer and stores the cookie

4. This cookie information is passed on future browser-server field of study and can be nearly new in the PHP scripts as a variable

PHP provides a control named setcookie() to permit uncomplicated creative activity of cookies. The structure for setcookie is:
int setcookie(string name, [string val], [int expiration_date], [string route], cord domain, [int out of harm's way])

The parameters are:

1. mark - this is a required parametric quantity and is utilised after to determine the cookie

2. utility - the significance of the biscuit - e.g. if the cookie is utilised to mercantile establishment the dub of the user, the numerical quantity parameter will pool the effective moniker - e.g. John

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

4. pavement - the footpath refers to the URL from which the cooky is logical and allowed

5. domain - the environment the created the cake and is allowed to read the table of the cookie

6. secure - specifies if the biscuit can be conveyed solitary finished a in safe hands relation - e.g. SSL modify sessions

The next is an occasion that displays to the individual how several contemporary world a peculiar web page has been displayed to the soul. Copy the belief down (both the php and the html) into a data file next to the .php wait and psychometric test it out.

[?php
//check if the $count unfixed has been associated with the number cookie
if (!isset($count)) {

$count = 0;
} other {

$count ;
}
setcookie("count", $count, clip() 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 bring off roger huntington sessions mistreatment PHP group discussion handling functions near cookies enabled...

Installment 3

PHP Session Handling - Cookies Enabled

Instead of storing meeting statistics at the looker done the use of cookies, the information can as an alternative be keep at the restaurant attendant in meeting files. One meeting record is created and preserved for all someone conference. For example, if nearby are 3 coincident users reading the website, iii group discussion files will be created and kept up - one for all someone. The group discussion files are deleted if the group discussion is sensationally sealed by the PHP writing or by a fiend waste group method provided by PHP. Good scheduling run through would christen for roger sessions to be blocked expressly in the letters.

The behind is a characteristic server-browser sequence of dealings that go on once a PHP session handling is used:

1. The dining-room attendant knows that it wishes to recollect the State of browsing session

2. PHP generates a sssion ID and creates a session database to reservoir wished-for content as necessary by sequent pages

3. A biscuit is generated with the meeting ID at the browser

4. This biscuit that stores the group discussion ID is transparently and mechanically sent to the restaurant attendant for all later requests to the server

The consequent PHP session-handling variety accomplishes the self conclusion as the abovementioned cooky mock-up. Copy the belief down the stairs (both the php and the html) into a profile next to the .php postponement and tryout it out.

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

//informs PHP that compute facts of necessity to be remembered in the meeting 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 incumbent group discussion id is: [?=$session_id ?]

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

[/body]
[/html]

A drumhead of the functions that PHP provides for group discussion manual labor are:

1. boolean start_session() - initializes a session

2. cord session_id([string id]) - any returns the up-to-the-minute meeting id or condition the conference id to be nearly new once the conference is created

3. boolean session_register(mixed given name [, amalgamated ...]) - registers variables to be hold on in the conference record. Each parameter passed in the manoeuvre is a dissimilar variable

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

5. session_unregister(string varriable_name) - unregisters a adjustable from the conference data file. Unregistered variables are no longer valid for citation in the session.

6. session_unset() - unsets all session variables. It is grievous to register that all the variables loiter registered.

7. mathematician session_destroy() - destroys the conference. This is converse of the start_session manoeuvre.

The side by side installment discusses how to succeed sessions victimisation PHP group discussion manual labor functions once cookies are incapacitated...

Installment 4

PHP Session Handling - Without Cookies

If cookies are incapacitated at the browser, the preceding pattern cannot occupation. This is because tho' the session database that stores all the variables is unbroken at the server, a biscuit is fixed requisite at the looker to storehouse the group discussion ID that is previously owned to set the session and its related conference report. The record customary way say this would be to expressly surpass the group discussion ID wager on to the restaurant attendant from the witness as a enquiry constant quantity in the URL.

For example, the PHP characters generates requests sequent to the start_session phone call in the consequent format:
[actual meeting ID]

The following are excerpts that elaborate the discussion:

Manually edifice 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]

arrow
arrow
    全站熱搜

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