U
U
ukod2015-02-25 20:30:59
PHP
ukod, 2015-02-25 20:30:59

Why are cookies deleted after re-entering the page?

I send cookies and initiate the session.

<?php
session_start();
if(!isset($user)){
$s = file_get_contents('http://ulogin.ru/token.php?token=' . $_POST['token'] . '&host=' . $_SERVER['HTTP_HOST']);
$user = json_decode($s, true);
}
if(isset($user)){
    $first_name = $user['first_name'];
    $last_name = $user['last_name'];
    $network = $user['network'];
    $identity = $user['identity'];
    $page_adress = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $_SESSION['user']=$user;
    $_SESSION['page_adress'] = $page_adress;
    $life_time = time()+(60 * 60 * 24 * 7);
    $access_path = "/";
    $access_domain = "domain.com";
    setcookie('first_name', $first_name, $life_time, $access_path, $access_domain);
    setcookie('last_name', $last_name, $life_time, $access_path, $access_domain);
    setcookie('network', $network, $life_time, $access_path, $access_domain);
    setcookie('identity', $identity, $life_time, $access_path, $access_domain);
    setcookie('page_adress', $page_adress, $life_time, $access_path, $access_domain);
}
?>

After that, another scenario accepts cookies first. If the cookies are not recorded, or the user has deleted them, then I try to extract the information from the session.
$boolCheckCookie = false;
     $pageAdressCheck = false;
     if(isset($_COOKIE['first_name'])){ 
          $username['first_name'] = $_COOKIE['first_name']; 
          $username['last_name'] = $_COOKIE['last_name'];
          $username['network'] = $_COOKIE['network'];
          $username['identity'] = $_COOKIE['identity'];
          $page_adress = $_COOKIE['page_adress'];
          $boolCheckCookie = true;
          echo "Отработали куки <br>";
     }
     else{
          if($boolCheckCookie == false){
               session_start();
               if(isset($_SESSION['user'], $_SESSION['page_adress'])){
               $username = $_SESSION['user'];
               $page_adress = $_SESSION['page_adress'];
               echo "Отработала сессия <br>";
               }
          }
     }

The problem is this: after I run the first script again, the browser cookies are deleted, and the session passes empty fields. Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Barsukov, 2015-02-26
@slavabars

At you at each call of the top code cookies are redefined.
if(!isset($user)) always returns True in your case.
I also hope you change the domain name in the code to your own. And if you use a subdomain or www, then put a dot before the domain.

B
Boris Benkovsky, 2015-02-25
@benbor

Tried for all files at the very beginning to include session_start ()? Maybe PHP forgets to set cookies in the second file if session_start fails

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question