我有一个页面在一些用户输入后执行脚本。该脚本计算距离和成本。然后,我将成本转换为cookie,以便转移并以另一页面的形式显示。运行脚本并重定向发生后,我在表单页面上没有显示我的cookie。不确定发生了什么。
表格摘要:
<div class="col-md-10" style="margin-top:12px;">
<div class="form-group">
<label for="input-Default" class="col-md-4 control-label" style="text-align:right;">Job Pay<span style="color:red;">*</span> :</label>
<div class="col-md-8">
<input type="text" name="jobpay" value="<?php echo $_COOKIE[$cost]; ?>"/>
<em class="error slotsError" style="color:red;"></em>
</div>
</div>
</div>
脚本片段:
$start = $_POST["origin"];
$end = $_POST["destination"];
$value = strtolower(str_replace(' ', '+', $start));
$value2 = strtolower(str_replace(' ', '+', $end));
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=
{$value}&destinations={$value2}&mode=driving&language=English-
en&key=$key";
$json = file_get_contents($url); // get the data from Google Maps API
$result = json_decode($json, true); // convert it from JSON to php array
$result2 = $result['rows'][0]['elements'][0]['distance']['text'];
$value3 = strtolower(str_replace(',', '', $result2));
$value4 = strtolower(str_replace('km', '', $value3));
$value5 = strtolower(str_replace(' ', '', $value4));
$pay = "0";
if($value5 <="10") {
$pay = "10";
}
elseif($value5 >= "10" && $value5 <= "15") {
$pay = "15";
}
else {
$far="too far";
}
$cost = "payamount";
$cost_value = $pay;
setcookie($cost, $cost_value, time() + (86400), "/"); // 86400 = 1 day
答案 0 :(得分:1)
原因是我没有指定子域名。正确的格式是:
setcookie($cost, $cost_value, time() + (86400), "/", "subdomain.com");