A Viewstate for PHP

Source: Internet
Author: User
One of the ASP. Attractive features is the viewstate. It is a convienent-to-store arbitrary data in a persistent mechanism. In fact, it's rather like PHP ' s $_session array.

There is, however, one difference. $_session is the unique to a user (reused) across pages. With only one window to your website, the preform the same function. But if a user have multiple tabs or windows, $_session can change in between what would otherwise is the successive page Lo Ads.

The ViewState has a functions:

Storing properties for controls and forms, storing arbitrary data

The viewstate is responsible for ensuring a red label was still red (and doesn ' t default back to black) between page loads. Controls and forms manage this transparently from the developer (excluding a Non-obtrusive ViewState property on controls ). There is also a viewstate on the Page object. It would act like a dictionary, allowing-to-save arbitrary data to it.

The viewstate is a magical thing, and it achieves that through indirection. It's what separates a browser form submission from a user actually preforming an action. Prado and Fortitude both duplicate this functionality in PHP.

For a full example, take a look at Fortitude Form ' s code. But for today's purposes, I just wanted to demonstrate what to create a simple viewstate in PHP.

!--? phpif (array_key_exists ("ViewState", $_post)) {$viewstate = Unserialize (base64 _decode ($_post["viewstate")); $viewstate--->count++;} else {$viewstate = new Stdobject; $viewstate->count = 0;}? >page-load count:!--? = $viewstate--->count;? 

It preforms very similarily to $_session. Although I used an object, the it works just as well with an array. But it accomplishes a separate the scope for each page. Frequently in PHP, pagination results is placed in $_session, and that's means only one set can is used at a time without B Utchering the rest. In a page scope, any manipulations can is done at would. And that exposes the real difference between the two:global things, like login operation or options, should is set in $_s Ession. Local, page-level or temporary things should is set in the page ' s viewstate. Opening or three or even one hundred twenty copies of that script would maintain that many instances of $viewstate Count. $_session maintains only one.

Note that the security problems the Extremeexperts article mentions is just as true with PHP as ASP. A manipulative user could set ViewState to a custom crafted value and disrupt the applications expectations. But that just means judicious amounts of validation is required before consuming viewstate on postbacks. Both ASP. NET and PHP ' s viewstate can be made secure.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.