duanleiliu7373 2014-01-21 04:07
浏览 34
已采纳

如何创建cookie

I have a problem regarding of creating cookies in PHP.

The scenario is this. I created a voting system in my site. And every user that will vote should be restricted up to 10 votes only. If the site detected that user it will automatically expired his/her cookies. And lastly after 3 months, all the user data (cookies) for that user will automatically destroyed.

How can I do that? Please help me I am in the middle of the project and I am a beginner in PHP. Thanks.

Controller

public function vote_photo() {  
  $vote = $this->contest_m->vote_photo($pid, fn_get_user('id'));
  $code = random_string('alnum', 42);

  if( ! empty($vote)){
    echo $vote['msg'];
    $cookie = array(
      'name'   => 'contest_cookies',
      'value'  => $code,
      'expire' => '7776000',
      'domain' => $this->input->server('HTTP_HOST'),
      'path'   => '/',
      'prefix' => 'sg_'
    );    
    $this->input->set_cookie($cookie);    
  }
  $data = array(
    'machine_id' => $_SERVER['REMOTE_ADDR'],
    'cookie' => $code ,
    'date' => '1',
    'photo_id' => '1',
    'contest_id' => '1'
  );
  $this->contest_m->save_cookie_count_ip($data);
  $this->input->set_cookie($cookie); 
}
}
  • 写回答

2条回答 默认 最新

  • duankange2433 2014-01-21 04:22
    关注

    Syntax:

    setcookie(name, value, expire, path, domain);

    Example:

    In the example below, we will create a cookie named "user" and assign the value "Ayushman-ashish" to it. We also specify that the cookie should expire after one hour:

    <?php
    setcookie("user", "Ayushman-ashish", time()+3600*24*30*3); // this is for 3 months
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?