viewstate 解码 php,A Viewstate for PHP

One of ASP.net’s most attractive features is the viewstate. It is a convienent way 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 unique to a user but is reused across pages. With only one window to your website, the two preform the same function. But if a user has multiple tabs or windows, $_SESSION can change in between what would otherwise be two successive page loads.

The viewstate has two functions:

Storing properties for controls and forms, Storing arbitrary data

The viewstate is responsible for ensuring a red label is 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 will act like a dictionary, allowing you to save arbitrary data to it.

The viewstate is a magical thing, and it achieves that through indirection. It is 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 how to create a simple viewstate in PHP.

count++;} else {$viewstate = new stdObject;$viewstate->count = 0;}?>Page-load count: count;?>

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET ViewState 是一种用于在 Web 应用程序中跨请求存储数据的机制。以下是一个使用 ViewState 的示例: 假设您有一个页面,其中包含一个文本框和一个按钮。用户在文本框中输入一些文本,然后单击按钮。在单击按钮时,将在服务器端处理程序中使用 ViewState 存储文本框中的值,并在页面上显示它。 以下是一个简单的 ASP.NET 页面代码示例,它演示了如何使用 ViewState 存储和检索文本框中的值: ```html <%@ Page Language="C#" %> <!DOCTYPE html> <html> <head runat="server"> <title>ViewState Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label> </div> </form> </body> </html> ``` 在按钮单击事件处理程序中,我们将文本框中的值存储在 ViewState 中,并将其显示在页面上: ```csharp protected void Button1_Click(object sender, EventArgs e) { string text = TextBox1.Text; ViewState["myText"] = text; Label1.Text = "Text saved: " + text; } ``` 在页面加载事件处理程序中,我们检索存储在 ViewState 中的值,并将其显示在页面上: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (ViewState["myText"] != null) { string text = (string)ViewState["myText"]; Label1.Text = "Text retrieved: " + text; } } } ``` 通过这种方式,我们可以在页面上保留用户在文本框中输入的值,即使用户单击其他按钮或导航到其他页面。请注意,ViewState 可能会增加页面大小,并增加网络传输时间。因此,我们应该谨慎使用 ViewState,并仅在必要时使用它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值