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 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了