skip to Main Content

I have this php script in my header.php file, sending HTTP headers to a python script, and setting a GUID as a session cookie:

 <?php
  ob_start();
  function guid(){
    if (function_exists('com_create_guid')){
        return com_create_guid();
    }else{
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
        return $uuid;
    }
  }
  $UA = $_SERVER['HTTP_USER_AGENT'];
  $REF = $_SERVER['HTTP_REFERER'];
  $IP =   $_SERVER['REMOTE_ADDR'];
  $GUID = guid();
  $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  if (!isset($_COOKIE["name"]))
    setcookie('name',$GUID);
  //echo $UA;
  //echo $REF;
  //echo $IP;
  //echo $GUID;
  echo exec("python /var/www/html/oscommerce-2.3.3.4/header_sender.py -u '$UA' -r '$REF' -i '$IP' -c '$GUID' -p '$actual_link' 2>&1");
  ob_end_flush();
  ?>

The problem I’m seeing is that this generates a new GUID for every page load in the same session – the session cookie seems to be not stored.

What could the problem be?
Thanks

3

Answers


  1. Seems like you don’t declare session_start();

    EDIT :

    Since you’re not using $_SESSION[" "] but instead $_COOKIE[" "].

    It seems like you don’t define how long the cookie will be stored on your browser by time() + x seconds

    setcookie('name',$value,time()+..);
    
    Login or Signup to reply.
  2. When you want use session so first need you start in session_start(), it should function used in first line in page after start php tag because if you used in between or end or any other place it is occur error header already sent,this error occur reason is when you used session_start() function in between code and you leave any space between code so this error has occur

    Login or Signup to reply.
  3. Not the issue here, but my search for PHP cookie not setting lead me here so answering for anyone else that comes along.

    Also pay attention to how you are setting expiry (if you are). I originally was setting like this:

    setcookie(self::$tokenCookie, $response['refreshToken'], 86400 * 365, "/");
    

    The intent was to set it for 1 year in the future (86400 * 365). However this makes it 1 year from epoch start (1970). To get 1 year from NOW use this:

    setcookie(self::$tokenCookie, $response['refreshToken'], time() + (86400 * 365), "/");
    

    Kind of a duh moment, but it tripped me up and caused some frustration for a bit so hoping this will help someone else save that frustration.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search