私は現在、自分の Web サイトで Cookie をいじっています。最初に行うことは、ユーザーが Cookie を持っているかどうかを確認することです。そうでない場合は、3 つのオプションを含むメニューを表示し、クリックすると Cookie が作成されますが、終了した場合は終了しますCookieが破棄されたブラウザ、これが私のコードです、
function createRandomId() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime() * 1000000);
$i = 0;
$unique = '';
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$unique = $unique.$tmp;
$i++;
}
return md5($unique);
}
function index() {
// $data is the array of data that is passed to views, setup it up
$data = array();
// We need to setup the cookie that will be used site, this will be used to cross reference
// The user with the options they have selected, to do this we first need to load the session model
// Check if the user has a cookie already, if they it means they have been to the site in the last 30 days.
if(!isset($_COOKIE['bangUser'])) {
// Get createRandomId() method and return a unique ID for the user
$unique = '';
// Setting the cookie, name = bangUser, the cookie will expire after 30 days
setcookie("bangUser", $unique, time() + (60*60*24*30));
$data['firstTime'] = TRUE;
} else {
$data['notFirstTime'] = TRUE;
}
// Load the view and send the data from it.
$this->load->view('base/index', $data);
}
function createCookie() {
// Function gets called when the user clicks yes on the firstTime menu.
// The purpose of this function is to create a cookie for the user.
// First we'll give them a unique ID
$unique = $this->createRandomId();
// With the unique ID now available we can set our cookie doing the same function as before
setcookie("bangUser", $unique, time() + (60*60*24*30));
// Now that the cookie is set we can do a 100% check, check that cookie is set and if it is redirect to
// to the homepage
if(isset($_COOKIE['bangUser'])) {
redirect('welcome');
}
}
基本的に、index() 関数がチェックを行い、createCookie が新しい Cookie を作成します。問題はありますか?