Been busy working on a site for the past few days that I’m hoping to have up in the near future. It’s just a hobbyist style site, hopefully it’ll be useful to some people, who knows. But, while working on it I came to the conclusion that Cookies would be my best way to save some of my data. I didn’t want to go through the effort of having people create user’s so I would have a unique ID for each of them just so I could save some checklists for their things. I also thought it would be easiest for this site if I was in control of all data going into the DB and it was all static, so I decided on Cookies.
Now, I’ve only messed with cookies a little bit on my own and a bit via CakePHP so it was time to form my own class. It’s nothing special, but it gets the job done. So, I’m just going to make short discussion over what I chose to do.
PHP’s Cookie storing function looks like so:
setcookie(name, value, expire, path, domain, secure)
With everything after name being optional. I’m not going to go over what each parameter is here, just over what I did with my class. I called it simply enough ‘cookieHelper’ and it had each one of these things as a property. So, it’s constructor appropriately enough accepted all of them:
//constructor
function cookieHelper($cName = '', $cValue = '', $cExpire = '', $cPath = '', $cDomain = '', $cSecure = false)
{
$this->setValues($cName, $cValue, $cExpire, $cPath, $cDomain, $cSecure);
}
You’ll notice though that the constructor simply calls another function called ‘setValues’. This is so that all the values can be set at once on an object instead of having to create another cookieHelper. So, set values does the actual storing:
//set the values of the cookie
function setValues($cName = null, $cValue = null, $cExpire = null, $cPath = null, $cDomain = null, $cSecure = null)
{
//only store non-null values
if ($cName != null){ $this->name = $cName; }
if ($cValue != null){ $this->value = $cValue; }
if ($cExpire != null){ $this->expires = $cExpire; }
if ($cPath != null){ $this->path = $cPath; }
if ($cDomain != null){ $this->domain = $cDomain; }
if ($cSecure != null){ $this->secure = $cSecure; }
}
However, for the time being I haven’t really been sending in all these things. None of them are required except for name, but I’ve just been sending ‘name’, ‘value’ and occasionally ‘expire’. The reason I only send ‘expire’ sometimes is because I’ve created a handy function to make setting it a little bit easier on me.
//set the time to expire the cookie
function setToExpireIn($amount = 0, $units = '')
{
$expire = $amount;
switch(strtolower($units))
{
case 'days':
case 'day':
$expire *= 24;
case 'hours':
case 'hour':
$expire *= 60;
case 'minutes':
case 'minute':
$expire *= 60;
case 'seconds':
case 'second':
//stays as is
}
$this->expires = time() + $expire;
$this->store();
}
This function takes 2 simple parameters. The amount of time and the unit your sending in. I use a switch statement to check the unit you sent in (obviously this is a limited function for expiration), then it simply multiplies the amount sent in to get the resultant time which it stores in ‘expires’ and then calls another function I haven’t shown off yet; the ‘store’ function. This is just a wrapper to the ‘setcookie’ function that PHP has built in. But since it’s part of the class it’s easy for us to call with any cookieHelper object by just saying:
$cookieHelperObject->store();
The store function:
//store the cookie in the browser
function store()
{
setcookie($this->name, $this->value, $this->expires, $this->path, $this->domain, $this->secure);
}
My class consists of 2 more functions. I’m sure there are more that could be written, but for my intents, this was all I needed. One, was a delete function. I overlooked at first the simplicity of deleting a cookie by simple setting it’s expiration date to a time that has already passed:
function delete()
{
$this->value = '';
$this->expires = time() - 3600;
$this->store();
}
This makes deleting a cookie as simple as:
$cookieHelperObject->delete();
Actually the delete function could be even simpler by calling ‘setToExpireIn(’-1’, ‘hours’)’ or something along those lines. But, it’s fine as is. Like I said, there is one last function in this class. This function has 2 uses, first is to simply grab the data of a previously stored cookie. That’s it’s main purpose, however if you pass in ‘true’ for the second parameter it will create a new cookie if one isn’t found with the name specified. Kind of handy if you’re looking for something that you are just going to make if you don’t find it. So here it is:
function get($name, $create = false)
{
//check first for the name of the cookie
if (isset($_COOKIE[$name]))
{
//set the values based on the stored cookie
$this->setValues($name, $_COOKIE[$name]);
return true;
}
//the cookie was not found
else
{
//create the cookie
if ($create)
{
$this->setValues($name);
$this->store();
$this->get($name, false);
}
return false;
}
}
And that’s it! Well, that was kind of long actually. But that’s the whole class I wrote and it seems to be working pretty good for me so far. There may be more elegant ways to do some of it, but like I said, I’m kind of new with cookies so any suggestions would be appreciated. Enjoy.
