matheusvmbruno
matheusvmbruno

Reputation: 2300

can't set cookie name

I'm having a problem to set the cookie name, i decided to post the methods here.

In my Main page i call the following methods:

 $CHAT->login($_POST['cliente']);
 $CHAT->entraFila();

The methods are:

public function login($cli, $senha){
     $this->cliente = $cli;
 }

 public function entraFila(){
    $fila_id = $this->DB->criaFila($this->cliente);
    if($fila_id){
         // -------------------------------- SETING THE COOKIE BELOW    <<<<< ---       
         setcookie("CHAT_FILA_ID", $fila_id, time() + 86400); 
        return true;
    } else {
        return false;
    }
 }

The "entraFila" method call "criaFila" method in another class:

 public function criaFila($cli){
    $insert = sprintf("INSERT INTO filas (cliente, data) VALUES (%s, NOW())", 
            GETSQLValueString($cli, "text")
    );
    $query = mysql_query($insert) or die(mysql_error());
    if($query){
        return mysql_insert_id($query);
    } else {
        return $query;
    }
 }

When i'm check the cookie in another pages, it always return false:

session_start();
if(!isset($_COOKIE['CHAT_FILA_ID'])){
    // CONDITION ALWAYS ENTER HERE
    header("location: login.php");
} 

In the chrome, i checked the name of the cookie, and it's setted like "PHPSESSID"

What's wrong?

Upvotes: 0

Views: 323

Answers (1)

Peter Kiss
Peter Kiss

Reputation: 9319

If you call setcookie after you have anything on the output your cookie will not set and if you have turned off error reporting you will not get any warning about this issue.

Other thing: Even is your setcookie() call is successful you will not have that cookie in your $_COOKIE array just after one new request.

No you don't have to start a session to use cookies.

Upvotes: 1

Related Questions