开发者

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!


As stated above you cannot read a cookie from the same page as it is set. I see you have tried tricking this using ajax but i do not believe that would be a valid trick as Ajax calls do not change the state of the page you are still on. so you can either do a full refresh or redirect OR at the same time you use setcookie you can also define the values you need in $_COOKIE so its available on the same page. like this:

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


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() ; 
?>


Try removing the path parameter from your setcookie() calls, maybe that's the issue.

Also, did you check that $data actually contains any data?


Propably you have really known problem with setting cookies and you have disabled error reporting about warnings.

Just try:

error_reporting(E_ALL);

You will propably see at your page something like "Cannot modify headers. Headers already sent". That because you need to SET cookies before you display anything on your page. So solution to resolve your problem is to implement your code to SET cookies at the bottom of your page or use ob_start/ob_clean.

Let me know if it helps :)


According to the "setcookie()" implementation in PHP, the cookie value check will not work until you move the control from the page that you are creating the cookie. So, your "SET" will create the cookie in one page and "sesion()" should be called from other page to check the value of the cookie that you set. Try it and hope it helps!

Try the following approach (please refine this as per your need). What I am trying here to refresh the page itself after setting the cookie and the "sesion()" function is a dynamic function that may or may not have any arguments. So, when you pass any argument to it, the the cookie will be set, otherwise it will be checked for existence. An accompanying function with func_num_args() is func_get_args(). It will help you to sanitize the expected arguments in the function.

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set("log_errors", 0);

session_start();

function sesion(){
  // func_num_args() number of arguments passed to the function
  if (func_num_args() == 0) { // if no arguments were passed, means the page is refreshed and cookie won't be set further
    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;
    }
  }
  else { // if number of args > 0, means you need to cookie here and refresh the page itself
    global $data; // set this to global as the $data will be available outside of this function
    setcookie("id",$data['id'], time()+3600*24*30,'/');
    setcookie("alias",$data['nombre'], time()+3600*24*30,'/');
    /**
     * refresh the page by javascript instead of header()
     * as header already being sent by the session_start()
     */
    echo '<script language="javascript">
    <!--
    window.location.replace("' . $_SERVER['PHP_SELF'] . '");
    //-->
    </script>';
    die();
  }
}

sesion(1); // passed an argument to set the cookie
?>

I think you will face issue with the JavaScript section, as it will change the page URL and I guess you are trying to include this script into the pages. So, I will take the help of call_user_func() and the final "else" part after the setcookie() lines will be changed with the following line:

call_user_func("sesion");

Hope this will make sense now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜