【问题标题】:Why is my cookie not being echoed into the html textbox?为什么我的 cookie 没有回显到 html 文本框中?
【发布时间】:2016-02-05 13:50:47
【问题描述】:

我有一个页面在一些用户输入后执行脚本。该脚本计算距离和成本。然后,我将成本转换为 cookie,以便传输并以表格形式显示在另一个页面上。在脚本运行并发生重定向后,我在表单页面上没有显示我的 cookie。不知道发生了什么。

表格sn-p:

<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>

脚本 sn-p:

$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

【问题讨论】:

  • 我认为它应该在像 $_COOKIE['$cost'] 这样的 qoutes 内
  • @Taha Dhailey 不幸的是这不起作用
  • 使用
  • @Guarav Rai 我有,但仍然一无所有
  • $_COOKIE['cost'] 你从成本中删除了 $ 吗??

标签: php html cookies


【解决方案1】:

所以原因是我也没有指定子域。正确的格式是:

setcookie($cost, $cost_value, time() + (86400), "/", "subdomain.com"); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多