title: PHP, MySQL - login\logout issue, login only after second attempt
body: <p>I have a very strange logout issue.</p>
  
  <p>The flow of the work:</p>
  
  <ol>
  <li>Log in page</li>
  <li>Main view (choosing a task)</li>
  <li>Task initialization that prepares the task in the background </li>
  <li>Task view (submitting some answer and clicking on submit)</li>
  <li>updates in the DB.  initialization of the next line (go to number 3)</li>
  </ol>
  
  <p>I am using Session and cookies in order to login.<br/></p>
  
  <p>The problem occurs only one time after logout the user tries to login.<br/>
  He succeffully reaches his Main view (different for each user)<br/>
  Successfully choose a task, go to the task view, enters submit.<br/>
  Then instead of the next page he receives "log out, please login" statement, meaning that my "checkUser" didnt find a session or a cookie and kicked him out.
  When he makes the login the next time, everything is working correctly. </p>
  
  <p>I dont understand where to begin to look for this issue.</p>
  
  <p>My login page relevant code:</p>
  
  <pre><code>session_start();
   $error_msg = "";
  //Do you have Session or cookies?
  if (isset($_SESSION['user_id']) &amp;&amp; isset( $_SESSION['user_role'])) 
  {
      If ($_SESSION['user_role']=='DM')
          header('Location: DMView.php');
      else if ($_SESSION['user_role']=='Vendor')
          header('Location: VendorView.php');
      exit;
  }
   //If you dont - Did you enter sumbit?
   if (!isset($_SESSION['user_id']) &amp;&amp; isset($_POST['submit'])) 
   {
        // Grab the user-entered log-in data
    $user_username = mysqli_real_escape_string($con, trim($_POST['username']));
    $user_password = mysqli_real_escape_string($con, trim($_POST['password']));
  
    if (!empty($user_username) &amp;&amp; !empty($user_password)) {
      // Look up the username and password in the database
      $query = "SELECT * FROM Users WHERE UserName = '$user_username' AND UserPassword = SHA('$user_password')";
      $data = mysqli_query($con, $query);
  
      if (mysqli_num_rows($data) == 1) {
        // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
        $row = mysqli_fetch_array($data);
        $_SESSION['user_id'] = $row['UserID'];
        $_SESSION['username'] = $row['UserName'];
        setcookie('user_id', $row['UserID'], time() + (60 * 60 * 24 * 30));    // expires in 30 days
        setcookie('username', $row['UserName'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
        $user_role = $row['UserRole'];
        $_SESSION['user_role'] = $row['UserRole'];
        setcookie('user_role', $row['UserRole'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
        $_SESSION['user_group'] = $row['UserGroup'];
        setcookie('user_group', $row['UserGroup'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
          If ($user_role=='DM')
              header('Location: DMView.php');
          else
              header('Location: VendorView.php');
      }
      else {
        // The username/password are incorrect so set an error message
        $error_msg = 'Sorry, you must enter a valid username and password to log in.';
      }
    }
    else {
      // The username/password weren't entered so set an error message
      $error_msg = 'Sorry, you must enter your username and password to log in.';
    }
  }
  </code></pre>
  
  <p>My checkUser file:</p>
  
  <pre><code>if (session_status() == PHP_SESSION_NONE) 
  {
      session_start();
  }
    // If the session vars aren't set, try to set them with a cookie
    if (!isset($_SESSION['user_id'])) {
  if (isset($_COOKIE['user_id']) &amp;&amp; isset($_COOKIE['username'])) {
    $_SESSION['user_id'] = $_COOKIE['user_id'];
    $_SESSION['username'] = $_COOKIE['username'];
    $_SESSION['user_role']=$_COOKIE['user_role'];
    $_SESSION['user_group'] = $_COOKIE['user_group'];
  }
   }
  
     if ((!isset($_SESSION['user_id'])) )  {
  echo '&lt;p&gt;Please &lt;a href="index.php"&gt;log in&lt;/a&gt; to access this page.&lt;/p&gt;';
  
  exit();}
  </code></pre>
  
  <p>and my Logout file:</p>
  
  <pre><code>// If the user is logged in, delete the session vars to log them out
    session_start();
    if (isset($_SESSION['user_id'])) {
  // Delete the session vars by clearing the $_SESSION array
  $_SESSION = array();
  
      // Delete the session cookie by setting its expiration to an hour ago (3600)
  if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time() - 3600);
  }
  
      // Destroy the session
  session_destroy();
    }
      // Delete the user ID and username cookies by setting their expirations to an hour ago (3600)
    setcookie('user_id', '', time() - 3600);
    setcookie('username', '', time() - 3600);
  
    // Redirect to the home page
  
   header('location: index.php');
  </code></pre>
  
  <p>The submit file (simplified with only one query):</p>
  
  <pre><code>&lt;?php
  require('connection.php');
  require ('checkUser.php');
  
  
  $task=$_POST['task_id'];
  $row=$_POST['row_id'];
  if( $_POST['answer']==1 )
  {
      $query="UPDATE ecnmatchingdetails SET RowStatus=2,Agent='".$uName."' , VendorAnswer='Yes', VendorComment='".$_POST['comments']."' , end_tag='".date("Y-m-d H:i:s")."' where TaskID=".$task." and RowId=".$row;
      mysqli_query($con, $query);
  }
  else...
  }
  
  if( isset( $_POST['answer'])) {
      header( 'Location: http://dub-entas-124/tool/TT/WorkOnTask.php?id='.$task . '&amp;start_task=0&amp;prevID='.$Ebay_PRD_ID);
      exit();
  }
  ?&gt;
  </code></pre>