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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c