【问题标题】:Error when checking if a cookie is set检查是否设置了 cookie 时出错
【发布时间】:2013-05-19 14:05:41
【问题描述】:

我在我的控制器中使用它来检查是否已经设置了 cookie。如果没有,我设置它。

function x(){
    if(!isset($_COOKIE['sg'])){
        $this->load->model('generate_model');
        $val=$this->generate_model->random();
        setcookie('sg',$val, time()+3600*24*30*12*3,"/", "" );
    }
    $this->load->model('model');
    $data['cod']=$cod;
    $this->model->select($cod);
    $this->load->view('templates/header');
    redirect('Home','refresh');
}

如果没有设置 cookie,我会收到两个错误:

遇到了 PHP 错误

严重性:通知

消息:未定义索引:sg

文件名:models/select_gift_model.php

行号:12

发生数据库错误

错误号:1048

列 'uid' 不能为空

插入id(uid,cod)值(NULL,'35A5V0Mogc')

文件名:C:\wamp\www\ci\system\database\DB_driver.php

行号:330

cookie设置好了,如果我按回再调用,功能就正常了。

如何检查是否设置了 cookie 并使功能正常工作?

【问题讨论】:

    标签: php codeigniter cookies


    【解决方案1】:

    在 php 的文档中:

    Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST.

    来源:http://php.net/manual/en/function.setcookie.php

    使其在当前连接中也可用的最佳方法是执行以下操作:

    setcookie('sg',$val, time()+3600*24*30*12*3,"/", "" );
    $_COOKIE = $val;
    

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 2015-07-23
      • 1970-01-01
      相关资源
      最近更新 更多