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

Fir*_*yah 2 php codeigniter

我认为我运行的代码是正确的,但在运行的某个时刻甚至出现错误'Call to undefined function delete_cookie()'

请指正:)

在控制器 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');
    }
  }
Run Code Online (Sandbox Code Playgroud)

u_m*_*der 5

您需要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');
}
Run Code Online (Sandbox Code Playgroud)