title: isset ( $_SESSION['user_id'] ) vs Login
body: <p>I just finished making my first working login form. I can verify that I'm logged in with a particular username/ID or not logged in at all.</p>
  
  <p>However, the login page isn't working right. I believe the code below is supposed to display a login form (if the user isn't already logged in) or a link to a logout page (if you're already logged in)...</p>
  
  <pre><code>&lt;?php if( isset( $_SESSION['user_id'] ) ): ?&gt;
  &lt;h2&gt;Login Here&lt;/h2&gt;
  &lt;form action="login-submit.php" method="post"&gt;
  (etc.)
  &lt;?php else: ?&gt;
  &lt;h2&gt;Logout Here&lt;/h2&gt;
  &lt;p&gt;&lt;a href="logout.php"&gt;Log Out Link&lt;/a&gt;&lt;/p&gt;
  &lt;?php endif; ?&gt;
  </code></pre>
  
  <p>But it only displays the logout link, even when I'm logged in.</p>
  
  <p>I'm not sure where to begin troubleshooting this, because it's all unfamiliar to me. I have a database table with the field 'user_id' (along with 'username' and 'password'. In this example, the value for user_id is the numeral 1.</p>
  
  <p>I tried to echo a value for user_id without success, even when logged in...</p>
  
  <pre><code>echo $_SESSION['user_id'];
  </code></pre>
  
  <p>If I paste this at the top of the page...</p>
  
  <pre><code>session_start();
  </code></pre>
  
  <p>...then it works in reverse. If I'm logged in, it displays a login form, and it displays a logout link when I'm already logged out.</p>
  
  <p>Here's the (heavily edited) code from login_submit.php...</p>
  
  <pre><code>&lt;?php
  session_start();
  
  /*** check if the users is already logged in ***/
  if(isset( $_SESSION['user_id'] ))
  {
   $message = 'Users is already logged in';
  }
  /*** check that both the username, password have been submitted ***/
  if(!isset( $_POST['username']))
  {
   $message = 'Please enter a valid username and password';
  }
   else
  {
  /*** if we are here the data is valid and we can insert it into  database ***/
  $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
  $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
  
  /*** now we can encrypt the password ***/
  $password = sha1( $password );
  
  /*** connect to database ***/
  
  try
  {
      // Database stuff...
  
      /*** prepare the select statement ***/
      $stmt = $dbh-&gt;prepare("SELECT user_id, username, password FROM     g1_members WHERE username = :username AND password = :password");
      /*** bind the parameters ***/
      $stmt-&gt;bindParam(':username', $username, PDO::PARAM_STR);
      $stmt-&gt;bindParam(':password', $password, PDO::PARAM_STR, 40);
  
      /*** execute the prepared statement ***/
      $stmt-&gt;execute();
  
      /*** check for a result ***/
      $user_id = $stmt-&gt;fetchColumn();
  
      /*** if we have no result then fail boat ***/
      if($user_id == false)
      {
              $message = 'Login Failed';
      }
      /*** if we do have a result, all is well ***/
      else
      {
              /*** set the session user_id variable ***/
              $_SESSION['user_id'] = $user_id;
  
              /*** tell the user we are logged in ***/
              $message = 'You are now logged in';
      }
  
  
  }
   catch(Exception $e)
  {
      /*** if we are here, something has gone wrong with the database    ***/
      $message = 'We are unable to process your request. Please try again later.';
   }
  }
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;PHPRO Login&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p&gt;&lt;?php echo $message; ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>P.S. Taking a cue from the discuss @ <a href="https://stackoverflow.com/questions/19476873/isset-didnt-work-after-login-with-session-set-to-valid">isset() didn&#39;t work after login with $_SESSION set to valid</a> I inserted session_start(); at the top of the page, then added this script:</p>
  
  <pre><code>if(isset($_SESSION['valid'])) { echo 'logged in'; } else { echo 'not logged yet'; }
  </code></pre>
  
  <p>It says "not logged yet" even when I'm logged in.</p>