Checking cookies are enabled
Hello,
I've been working on a login class recently and one of the methods just checks if the user has cookies enabled or not.
Basically I've looked around online at how to check if cookies are enabled or not in PHP and if they are to set them up to be secure and made a function for it. I could do with someone looking over what I've done to see if it could be improved in anyway and if I've missed anything out.
Code:
//Stops the code being run multiple times as cookies should only need to be checked for once
if(ini_get('session.use_only_cookies') !== true || ini_get('session.hash_function') !== $this->hash || !isset($_COOKIE['nnspl_cookie']))
{
//Starts test to see if cookies are enabled on the client
if(!isset($_GET['tc']) && !isset($_COOKIE['nnspl_cookie']))
{
setcookie("nnspl_cookie", "test", 0);
header('Location: '.$_SERVER['PHP_SELF'].'?tc=1');
}
//If cookies are not enabled the user is forwarded to another page
if(isset($_GET['tc']) && !isset($_COOKIE['nnspl_cookie']))
header('Location: error.html');
//If they are enabled they are set to be secure and true is returned
else
{
//Filters to make sure variables are sanitised
$domain = filter_var($_SERVER['HTTP_HOST'], FILTER_VALIDATE_URL);
//Sets session cookies to be stored securely
ini_set('session.use_only_cookies', true);
ini_set('session.hash_function', $this->hash);
session_set_cookie_params(0, '/', $domain, isset($_SERVER["HTTPS"]), true);
return true;
}
}
Thank you in advance,
El Barto