我正在使用Cookie来存储用户首选项。当用户登录时,我想从数据库中获取他们的首选项并将其存储在cookie中。我希望将Cookie存储在固定日期,如下所示:
@model Project.Models.CheckoutItem
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Checkout - Project</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<style type="text/css">
body{ margin-top:20px; }
</style>
</head>
<body>
@using (Html.BeginForm( ))
{
<div class="container">
<div class="row form-group">
<div class="col-xs-12">
<ul class="nav nav-pills nav-justified thumbnail setup-panel">
<li class="active">
<a href="#step-1">
<h4 class="list-group-item-heading">Step 1</h4>
<p class="list-group-item-text">Your Details</p>
</a>
</li>
<li class="disabled">
<a href="#step-2">
<h4 class="list-group-item-heading">Step 2</h4>
<p class="list-group-item-text">Delivery Method</p>
</a>
</li>
<li class="disabled">
<a href="#step-3">
<h4 class="list-group-item-heading">Step 3</h4>
<p class="list-group-item-text">Payment Information</p>
</a>
</li>
</ul>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
Item: @Model.Item.ItemName
</div>
</div>
<div class="row setup-content" id="step-1">
<div class="col-xs-12">
<div class="col-md-12 well text-center">
<div class="col-md-4 col-md-offset-4">
@Html.ValidationSummary()
<!-- STEP 1-->
@Html.LabelFor(m => m.FirstName) @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", @required = "required" }) <br/>
@Html.LabelFor(m => m.LastName) @Html.TextBoxFor(m => m.LastName, new { @class = "form-control", @required = "required" })<br />
@Html.LabelFor(m => m.Email) @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @required = "required" })<br />
@Html.LabelFor(m => m.PhoneNumber) @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control", @required = "required" })<br />
@Html.LabelFor(m => m.MsgOnCard) @Html.TextAreaFor(m => m.MsgOnCard, new { @class = "form-control" })<br />
<button id="activate-step-2" class="btn btn-primary" type="button">Next</button>
</div>
</div>
</div>
</div>
<div class="row setup-content" id="step-2">
<div class="col-xs-12">
<div class="col-md-12 well text-center">
<div class="col-md-4 col-md-offset-4">
<!-- STEP 2 -->
@Html.LabelFor(m => m.ShippingMeth) @Html.EnumDropDownListFor(m => m.ShippingMeth) <br/> <br/>
<div id="shippingDetails" class="disabled">
@Html.LabelFor(m => m.deliveryDetails.Street) @Html.TextBoxFor(m => m.deliveryDetails.Street, new { @class = "form-control" }) <br/>
@Html.LabelFor(m => m.deliveryDetails.City) @Html.TextBoxFor(m => m.deliveryDetails.City, new { @class = "form-control" }) <br/>
@Html.LabelFor(m => m.deliveryDetails.State) @Html.TextBoxFor(m => m.deliveryDetails.State, new { @class = "form-control" }) <br/>
@Html.LabelFor(m => m.deliveryDetails.Postcode) @Html.TextBoxFor(m => m.deliveryDetails.Postcode, new { @class = "form-control" }) <br/>
</div>
<button id="activate-step-3" class="btn btn-primary" type="button">Next</button>
</div>
</div>
</div>
</div>
<div class="row setup-content" id="step-3">
<div class="col-xs-12">
<div class="col-md-12 well text-center">
<div class="col-md-4 col-md-offset-4">
<!-- STEP 3 -->
@Html.LabelFor(m => m.Card.CardHolder) @Html.TextBoxFor(m => m.Card.CardHolder, new { @class = "form-control" }) <br />
@Html.LabelFor(m => m.Card.CardNumber) @Html.TextBoxFor(m => m.Card.CardNumber, new { @class = "form-control" }) <br />
<b>Card Expiry</b> <br/> <br/> @Html.TextBoxFor(m => m.Card.ExpMonth, new { @class = "form-control" }) / @Html.TextBoxFor(m => m.Card.ExpYear, new { @class = "form-control" }) <br />
@Html.LabelFor(m => m.Card.Cvc) @Html.TextBoxFor(m => m.Card.Cvc, new { @class = "form-control" }) <br />
<input type="submit" value="Pay" class="btn btn-success btn-lg" name="Model" />
</div>
</div>
</div>
</div>
</div>
正如您所看到的,JavaScript方法将Cookie设置为在// I put in the constructor to initialize those variables in the class.
Item = new ItemModel();
上过期。我怎样才能在PHP中做同样的事情?
我存储cookie的方式还有一个问题是将cookie名称设置为用户名,并且首选项未加密,它们存储为纯文本。但他们没有任何敏感信息。以这种方式使用cookie是否安全?
答案 0 :(得分:0)
使用php strtotime()
setcookie("cookie_name", "cookie_value", strtotime('2030-31-12 23:59:59 GMT'));
答案 1 :(得分:0)
使用php time()
setcookie("cookiename", 'cookievalue', strtotime('2030-31-12 23:59:59 GMT'));