title: How to extract the current user_id of the user logged in that session?
body: <p><strong>Login.php</strong><br>
  What I have been trying to do is extract the session id of the user who is logged in and call it on another page in order to store user_id in the comments table. However, when I tried to do that, it outputs only 0, rather than the user's ID, in the next page. </p>
  
  <pre><code>&lt;?php
  session_start();
  require "../layouts/header.php";//including header
  require '../database/db.php';//db connection
  
      if (isset($_POST['login'])) {
          if(empty($_POST["username"]) || empty($_POST["password"]))
          {
              $msg = '&lt;label style="color:red;"&gt; Missing fields!&lt;/label&gt;';
          }
          else
          {
              //checking if the provided credential match with the database
              $query = "SELECT * FROM users where username=:username";
              $statement = $db-&gt;prepare($query);
              //criteria for successful login
              $condition = [
                  'username'=&gt;$_POST['username']
  
              ];
              $statement-&gt;execute($condition);
  
              $count = $statement-&gt;rowCount();
              if($count &gt; 0)
              {                   
                  $users=$statement-&gt;fetch(PDO::FETCH_OBJ);
                  $pwd = $_POST['password'];
                      //verification of password
                      if (password_verify($pwd, $users-&gt;password))
                      {
                          $type = $_POST['type'];
                          $_SESSION['username'] = $_POST['username'];
                          foreach( $users as $view):
                          $_SESSION['user_id'] = $view['user_id'];
  
                          if($type==$view['type'] &amp;&amp; $type == 1)
                          {
                              header("location:../admin/index.php"); 
                          }
                          if($type==$view['type'] &amp;&amp; $type == 0)
                          {
                              header("location:../client-side/index.php?user_id=&lt;?= $view-&gt;user_id; ?&gt;");
                          }
                          else
                          {
                              echo "Invalid!";
                          }    
                          endforeach; 
                      }
                      else{
                          echo '&lt;label style="color:red;"&gt; Invaid Entry!&lt;/label&gt;';
                      }
              }
              else
              {
                  $msg = '&lt;label style="color:red;"&gt; Invaid Entry!&lt;/label&gt;';
              }
          }
      }
  </code></pre>
  
  <p>?>
  <strong>Login Form</strong></p>
  
  <pre><code>&lt;main class="home" style="margin-bottom: 30px; margin-top:30px;"&gt;
      &lt;h2 style="text-align: center"&gt;Login&lt;/h2&gt;
      &lt;hr&gt;
  
      &lt;form action="login.php" method="POST"&gt;
  
          &lt;label&gt;Username:&lt;/label&gt;
          &lt;input type="text" name="username" placeholder="Enter Username"&gt;&lt;br&gt;
          &lt;label&gt;Password:&lt;/label&gt;
          &lt;input type="password" name="password" placeholder="Enter Password"&gt;&lt;br&gt;
          &lt;label&gt;&lt;strong&gt;Please select one:&lt;/strong&gt;&lt;/label&gt; 
          &lt;label&gt;Admin&lt;/label&gt;
          &lt;input type="radio" name="type" id="Admin" value="1" style="margin-top: 33px; height:20px;width:20px;"&gt;&lt;br&gt;
          &lt;label&gt;Client&lt;/label&gt;
          &lt;input type="radio" name="type" id="Client" value="0" style="margin-top: 33px; height:20px;width:20px;"&gt;&lt;br&gt;
          &lt;input type="submit" name="login" value="Login"&gt;
      &lt;/form&gt;
  
      &lt;div style="height: 200px;"&gt;&lt;/div&gt;
  
      &lt;br&gt;
  &lt;/main&gt;
  
      &lt;?php            
      require "../layouts/footer.php"; 
      ?&gt;
  </code></pre>
  
  <p><strong>index.php</strong> the index page only outputs 0 rather than the user_id</p>
  
  <pre><code>&lt;?php
      if(isset($_SESSION["user_id"]))
      {
          echo '&lt;h1&gt; Welcome - '.$_SESSION["user_id"].'&lt;/h1&gt;';
  
      }
  ?&gt;
  </code></pre>