【问题标题】:Cookie not being read by html formcookie 没有被 html 表单读取
【发布时间】:2013-05-16 19:15:49
【问题描述】:

我正在开发一个网站,人们可以在该网站上根据邮政编码进行搜索。 当第一次访问该页面时,会打开一个弹出对话框。您必须填写您的邮政编码和 x 公里的半径。

提交时,cookie 被设置为“邮政编码”和“半径”。

我在主页的搜索栏中打印出这些 cookie。但是当我想提交表单时,它没有得到 cookie 的值。它不是空的,但是当我手动重新输入它然后单击提交时它可以工作。可能是什么问题?

我正在使用 Pro6pp(一个在线邮政编码数据库),从中获取特定半径的邮政编码。我将它们保存在 SESSION 中。

我的对话:

<div id="dialog" class="hidden" title="Welkom bij OostWestRegioBest.nl">

    <p>Zoeken in uw regio.</p>
    <p>Voer een postcode in zonder letters. Voorbeeld: 1234.</p>
    <br/>
    <form method="post" action="">

        <input type="text" name="postcode" size="25" placeholder="Postcode">
        <select name="radius">
            <option disabled selected>Afstand</option>
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
        </select> 
        <br/><br/>
        <hr>
        <br/>
            <p style="float: right"><input type="submit" value="Opslaan"></p>
            <input type="hidden" name="submitted" value="true">
            <input type="hidden" name="afstand" value="true" />

</form>

<?php
    if(isset($_POST['postcode']))
    {
        setcookie('radius', $_POST['radius'], time() + (20 * 365 * 24 * 60 * 60));
        setcookie('postcode', $_POST['postcode'], time() + (20 * 365 * 24 * 60 * 60));
        header("location: {$_SERVER['PHP_SELF']}");
    };
?>

我的搜索表单:

    <form name="input" method="post" action="searchresults" class="pro6pp_range">
    <input type="search" onchange="validate()" placeholder="Zoeken..." name="search" size="70">
          <select class="range">
          <? $radius = $this->input->cookie('radius'); ?>
          <? $sesspc = $_SESSION['searched_post_code'] = $_COOKIE['postcode']; ?>
          <? $postcode = $sesspc ?>
            <option selected="selected" value="<?= $this->input->cookie('radius'); ?>"><?= $this->input->cookie('radius');?> km</option>
            <option value="">Kies een afstand</option>
            <option value="5">5 km</option>
            <option value="10">10 km</option>
            <option value="15">15 km</option>
            <option value="20">20 km</option>
            <option value="25">25 km</option>
            <option value="50">50 km</option>
          </select>
        <input type="search" required="required" name="searchpc" class="postcode" value="<?= $postcode ?>" placeholder="Postcode (1234)" maxlength="4">

        <input type="submit" value="Zoeken">
        <br/>
        <span class="message"></span>
        <br/>
    </form>

<?php

$searchpc = $this->input->post('searchpc');

if(empty($searchpc)){
        unset($_SESSION['postcodes']); 
    }
?>

表单与pro6pp数据库交互的代码:

<!--    <script>
    window.location.replace("home/searchresults");
    </script>
-->
<?php 

     session_start();

echo '<pre>';
     $new_post_code=$_GET['post_code'];
        if($new_post_code!='' && $new_post_code!=0){
        if(!isset($_SESSION['searched_post_code']) || 
        empty($_SESSION['searched_post_code'])){ 
        $_SESSION['searched_post_code']=$new_post_code; 
        $_SESSION['searched_post_code'] = $_COOKIE['postcode'];
        }elseif($_SESSION['searched_post_code']!=$new_post_code){ 

        $_SESSION['searched_post_code']=$new_post_code; 

        unset($_SESSION['postcodes']); 
        }
        }
     $string = implode($_SESSION['postcodes'], '|');
     $output=$_GET['output'];
     $_SESSION['postcodes'][]=$output;
     echo $output;
     print_r($_SESSION);
     print_r($_COOKIE);
echo '</pre>';

?>

我不知道为什么它不起作用。

【问题讨论】:

  • 不要使用短标签&lt;? ?&gt;
  • 为什么不呢?短标签有什么问题?
  • 因为在某些服务器短标签通过 php.ini 被禁用
  • 它不在我的服务器上。所以对我来说没问题。
  • 牢记在心,以备日后需要

标签: php html forms codeigniter cookies


【解决方案1】:

setcookie() 函数接受几个参数,其中之一是 PATH 我注意到你没有设置它。将 path 设置为 '/' 将使其在整个域中可用。它解决了你的问题。

例子

 setcookie("TestCookie", $value, time()+3600, '/');

TestCookie 的 $value 将在 1 小时后过期,并且可用于整个域。

来自手册

cookie 可用的服务器上的路径。如果设置为“/”,cookie 将在整个域中可用。如果设置为 '/foo/',则 cookie 将仅在 /foo/ 目录和域的 /foo/bar/ 等所有子目录中可用。默认值是设置 cookie 的当前目录。

您需要在 setcookie() 中为您的 cookie 设置正确的路径,它会起作用。

更多手册http://php.net/manual/en/function.setcookie.php

此外:

  1. 编辑 $_SESSION 和 $_COOKIE 的更改键,您设置的键与您检查的键不同。

  2. 如果您删除 $_SESSION 变量,首先使用 isset() 检查该变量是否存在

  3. 在下一次加载 cookie 应该可见的页面之前,cookie 不会变得可见。要测试 cookie 是否设置成功,请在 cookie 过期之前检查下一个加载页面上的 cookie。

  4. 不影响问题,但将onchange="validate()"改为onchange="return validate()"

您应该更改的代码

<?php
if(isset($_POST['postcode']))
{
    setcookie('radius', $_POST['radius'], time() + (20 * 365 * 24 * 60 * 60), '/');
    setcookie('postcode', $_POST['postcode'], time() + (20 * 365 * 24 * 60 * 60), '/');
    header("location: {$_SERVER['PHP_SELF']}");
};

?>


<form name="input" method="post" action="searchresults" class="pro6pp_range">
<input type="search" onchange="return validate()" placeholder="Zoeken..." name="search" size="70">
      <select class="range">
      <? $radius = isset($_COOKIE['radius']) ? $_COOKIE['radius'] : -1; ?>
      <? if(isset($_COOKIE['postcode'])) {$sesspc = $_COOKIE['postcode']; $_SESSION['postcode']= $_COOKIE['postcode'];} ?>
      <? $postcode = $sesspc ?>
      <? if(isset($_COOKIE['radius'])
        echo '<option selected="selected" value="'.$_COOKIE['radius'].'">'.$_COOKIE['radius'].' km</option>';
      ?>
        <option value="">Kies een afstand</option>
        <option value="5">5 km</option>
        <option value="10">10 km</option>
        <option value="15">15 km</option>
        <option value="20">20 km</option>
        <option value="25">25 km</option>
        <option value="50">50 km</option>
      </select>
    <input type="search" required="required" name="searchpc" class="postcode" value="<?= $postcode ?>" placeholder="Postcode (1234)" maxlength="4">

    <input type="submit" value="Zoeken">
    <br/>
    <span class="message"></span>
    <br/>
</form>

<?php

    if(isset($_POST['searchpc']) && isset($_SESSION['postcode']))
    {
       if(empty($_POST['searchpc']) ) unset($_SESSION['postcode']); 
    }
?>

【讨论】:

  • 没错,对我来说它也有效。也许您在浏览器中关闭了 cookie?
  • if(empty($searchpc)){ if(isset($_SESSION['postcode'])) unset($_SESSION['postcode']); } 试试这个并注意 $_SESSION 的正确键 在取消设置之前检查 $_SESSION 是否退出
  • :) 我很高兴能帮上忙
  • 你可以使用error_reporting()函数或者你可以使用ini_set("error_reporting", E_ALL &amp; ~E_NOTICE); 记住将这部分代码包含到每个站点,最好的位置是页面顶部。并且请不要写,这样做之后你仍然有错误,因为这是不可能的
  • 请编辑对问题的说明。如果您有答案,请将其发布为答案。 cmets 将被清除 -- 如果您想保留某些东西,请将其放在其他地方。
猜你喜欢
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2021-06-08
  • 2023-02-23
  • 2011-04-07
  • 2022-01-23
  • 1970-01-01
  • 2013-10-23
相关资源
最近更新 更多