Search code examples
phpcookiessession-cookiessetcookie

cookie won't set


This is a question regarding an old one of mine: cookie won't unset: cookie wont unset where I had problems unseting the cookie (but it was set 'properly'),

Now that the problem is solved; the cookie doesn't seem to SET

cookie 'set': (does not work)

setcookie("id",$data['id'], time()+3600*24*30,'/');
setcookie("alias",$data['nombre'], time()+3600*24*30,'/');

cookie check: (seems to work)

    function sesion(){

    if(isset($_COOKIE['id']) && isset($_COOKIE['alias'])){
                    $_SESSION['logueado'] = true;
                    $_SESSION['id'] = $_COOKIE['id'];
                    $_SESSION['alias'] = $_COOKIE['alias'];

                    return true;  //THIS IS NEVER RETURNING TRUE
                }
if(isset($_SESSION['id']) && isset($_SESSION['logueado']) && $_SESSION['logueado'] == true){

                    return true;
                }
                 else{ return false;
    }



    }

cookie unset: (works)

function cerrar_sesion(){
  session_start();
  $_SESSION['logueado']= false;
  $_SESSION['id']= NULL;
  session_unset();
  session_destroy();
  setcookie("id",false,time()-3600,"/");
  setcookie("alias",false,time()-3600,"/");
  unset($_COOKIE['id']);
  unset($_COOKIE['alias']);
}

What happens is that login is working only through $_SESSION so after 30 minutes of no activity the user is no longer logged in,

Any idea what I'm doing wrong? Thanks a lot!


Solution

  • set cookie lines work fine with me.

    as for }else if(isset($_COOKIE['id']) && i since you return if you remove the else here is still okay, if there was no return above you would have to keep the else here in order not to evaluate this block generally speaking I am not sure that elseif is the same with else if in all cases

    The way the function session is build will act like this:

    • On the first load it will show: no cookie, no session because you cannot see a cookie until reload (which I guess you already know). -On second load you will see cookie alive session set. -after the second load you always see session is set.

    All I want to say that session works exactly as expected to work, so I don't really see any problem.

    <?php
    
    $data='Hello'; 
    setcookie("id",$data['id'], time()+3600*24*30,'/');
    setcookie("alias",$data['nombre'], time()+3600*24*30,'/');
    
    session_start(); 
    
    function sesion()
         {
    
            if(isset($_SESSION['id']) && isset($_SESSION['logueado']) 
                && $_SESSION['logueado'] == true)
            {
                echo 'SESSION IS SET<br>';
                return true;
            } 
            if(isset($_COOKIE['id']) && isset($_COOKIE['alias']))
            {
                $_SESSION['logueado'] = true;
                $_SESSION['id'] = $_COOKIE['id'];
                $_SESSION['alias'] = $_COOKIE['alias'];
                echo 'COOKIE is alive and session set'.$_SESSION['alias'].'<br>';
                return true;  //THIS IS NEVER RETURNING TRUE
             }
            else
            {  
                echo 'NO SESSION,   NO COOKIE YET, WAIT UNTIL REFRESH<br>';
                return false;
            } 
    } 
     sesion() ; 
    ?>