为什么我在Codeigniter上收到错误'调用未定义函数delete_cookie()'?

时间:2017-05-17 07:23:47

标签: php codeigniter

我认为我运行的代码是正确的,但运行中的一个时刻甚至出现错误'调用未定义的函数delete_cookie()'

请更正:)

在Controller Product_Ref.php中

public function index()
    {
    $ref = $this->input->get('id');
    $getIdOrder = $this->product_model->getIdOrder($ref);
    if ($getIdOrder) {
      $this->load->helper('cookie');
      $cookie = array(
        'name'   => 'refProductcookie',
        'value'  => $ref,
        'expire' => '43200'
      );
      $this->input->set_cookie($cookie);
      echo get_cookie('refProductcookie').'<br>';
    }else {
      echo "Sorry this product has not been registered yet";
      delete_cookie('refProductcookie');
    }
  }

2 个答案:

答案 0 :(得分:3)

您需要为cookie个案例加载帮助if-else -

// load BEFORE `if`
$this->load->helper('cookie');
if ($getIdOrder) {
  $cookie = array(
    'name'   => 'refProductcookie',
    'value'  => $ref,
    'expire' => '43200'
  );
  $this->input->set_cookie($cookie);
  echo get_cookie('refProductcookie').'<br>';
}else {
  echo "Sorry this product has not been registered yet";
  delete_cookie('refProductcookie');
}

答案 1 :(得分:1)

问题是在else语句中没有加载cookie帮助器,在else语句中添加此行或者在$this->load->helper('cookie');

中添加更好的行