php - Error when checking if a cookie is set -
i using in controller check if cookie set. if not, set it.
function x(){ if(!isset($_cookie['sg'])){ $this->load->model('generate_model'); $val=$this->generate_model->random(); setcookie('sg',$val, time()+3600*24*30*12*3,"/", "" ); } $this->load->model('model'); $data['cod']=$cod; $this->model->select($cod); $this->load->view('templates/header'); redirect('home','refresh'); }
if cookie not set, 2 errors:
a php error encountered
severity: notice
message: undefined index: sg
filename: models/select_gift_model.php
line number: 12
a database error occurred
error number: 1048
column 'uid' cannot null
insert
id
(uid
,cod
) values ( null, '35a5v0mogc')filename: c:\wamp\www\ci\system\database\db_driver.php
line number: 330
the cookie set, , if press , call again, function works normally.
how can check if cookie set , make function work properly?
in docs of php:
once cookies have been set, can accessed on next page load $_cookie or $http_cookie_vars arrays. note, superglobals such $_cookie became available in php 4.1.0. cookie values exist in $_request.
source: http://php.net/manual/en/function.setcookie.php
the best way make in current connection available folling:
setcookie('sg',$val, time()+3600*24*30*12*3,"/", "" ); $_cookie = $val;
Comments
Post a Comment