I've written a php function and some helper functions that checks for cookies when somebody lands on a page but it's not so fast. I want to know is there any way to make it faster or improve it i know it's possible to check for cookies client side using javascript but what if it is disabled also so i need a pure server side solution.
<?php
function check_cookie(){
if(is_cookie_disabled()){
header("location:cookie_is_disabled.php");
exit;
}
}
function is_cookie_disabled(){
$curr_url = $_SERVER['PHP_SELF'];
if(isset($_COOKIE['testCookie']) && !(isset($_COOKIE['cookie']))){
$original_url = original_url ($curr_url);
header("location: ".$original_url);
setcookie("cookie", 'enabled');
exit;
} elseif(!(isset($_COOKIE['testCookie']))) {
if(isset($_GET['temp'])){
return true;
} else{
if(if_parameters_exist_in_url($curr_url)){
$url = $curr_url."&temp=temp";
} else {
$url = $curr_url."?temp=temp";
}
header("location: ".$url);
setcookie("testCookie", 'test');
exit;
}
}
return false;
}
function original_url ($curr_url){
if (!empty($_GET)){
if (isset($_GET['temp'])) {
if(count($_GET)>0){
return removeqsvar($curr_url, 'temp');
} else {
return removeallvars($curr_url);
}
}
}
return $curr_url;
}
function removeqsvar($url, $varname) {
return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url);
}
function removeallvars($url){
return $url = strtok($url, '?');
}
function if_parameters_exist_in_url($url){
if (strpos($url, '=')) {
return true;
} else {
return false;
}
}
//at the top of your pages
check_cookie();
?>