Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


function check_cookie(){

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');
    } elseif(!(isset($_COOKIE['testCookie']))) {
            return true;
        } else{
                $url = $curr_url."&temp=temp";
            } else {
                $url = $curr_url."?temp=temp";
            header("location: ".$url);
            setcookie("testCookie", 'test');
    return false;

function original_url ($curr_url){
    if (!empty($_GET)){
        if (isset($_GET['temp'])) {
                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

share|improve this question

migrated from Feb 9 '12 at 15:57

This question came from our site for professional and enthusiast programmers.

How slow is it? How fast do you need it to be? – sarnold Feb 9 '12 at 3:36
Look, people with cookies and JavaScript both disabled are few and far between. You're spending more time than it's worth dealing with. – ceejayoz Feb 9 '12 at 3:38
@sarnold as fast as possible – pouya Feb 9 '12 at 4:16
yeah but both cookies and javascript are crucial to website performance and i wanna have complete control on them – pouya Feb 9 '12 at 4:16

It could be slow from all of the location redirects. But I've never done anything like that so I can't be sure.

Is it possible to lazily set the cookies. For instance, set a session and cookie on the first page load. And then, on the second page load check if the cookie is present.

This is how you could check if the page has loaded.

$_SESSION['first_load'] = isset($_SESSION['first_load']) ? false : true;

if( !$_SESSION['first_load'] && isset($_COOKIE['testCookie']) )
  echo 'cookies enabled!'
  echo 'no cookies for you!'
share|improve this answer

It's certainly not slow. Measure the time it takes:

  • to execute this code (using microtime(true)),
  • and for your browser to receive the page (eg. Chrome has a "Network" tab in the Developers Tool).

You'll see that optimizing this code won't yield any improvement, since this PHP code probably takes a few milliseconds to execute while the page load can take up to a second for small pages.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.