title: PHP / MySQL Get Username of Current Logged In User
body: <p>I am trying to get the username of the current logged in user so I can do things such as display the username on a welcome screen or check to see if that user has access to a page.</p>
  
  <p>$logedInUsername is returning "Array" which I would have thought returned "Bob" since that is the user that I logged in as.</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
  // Start Require Login
  require("common.php");
  if(empty($_SESSION['user']))
  {
      header("Location: login.php");
      die("Redirecting to login.php");
  }
  
  // get current logged in user   
  $logedInUsername = $_SESSION['user'];
  echo $logedInUsername;
  
  // check if the username is equal to admin
  if($logedInUsername == "admin")
  {
    echo "You are a admin!";
  } 
  else 
  {
    echo "You are NOT a admin!";
  }
  // End Require Login
  
  // ... html code below here ...
  </code></pre>
  
  <p><strong>common.php</strong></p>
  
  <pre><code>&lt;?php 
  $username = "username"; 
  $password = "password"; 
  $host = "localhost"; 
  $dbname = "db"; 
  
  $options = array(PDO::MYSQL_ATTR_INIT_COMMAND =&gt; 'SET NAMES utf8'); 
  
  try 
  { 
      $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
  } 
  catch(PDOException $ex) 
  { 
      die("Failed to connect to the database: " . $ex-&gt;getMessage()); 
  } 
  
  $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
  
  $db-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
  
  if(function_exists('get_magic_quotes_gpc') &amp;&amp; get_magic_quotes_gpc()) 
  { 
      function undo_magic_quotes_gpc(&amp;$array) 
      { 
          foreach($array as &amp;$value) 
          { 
              if(is_array($value)) 
              { 
                  undo_magic_quotes_gpc($value); 
              } 
              else 
              { 
                  $value = stripslashes($value); 
              } 
          } 
      } 
  
      undo_magic_quotes_gpc($_POST); 
      undo_magic_quotes_gpc($_GET); 
      undo_magic_quotes_gpc($_COOKIE); 
  } 
  
  header('Content-Type: text/html; charset=utf-8'); 
  
  session_start();
  </code></pre>
  
  <p><strong>login.php</strong></p>
  
  <pre><code>&lt;?php 
  require("common.php"); 
  
  $submitted_username = ''; 
  
  if(!empty($_POST)) 
  { 
      $query = " 
          SELECT 
              id, 
              username, 
              password, 
              salt, 
              email 
          FROM users 
          WHERE 
              username = :username 
      "; 
  
      $query_params = array( 
          ':username' =&gt; $_POST['username'] 
      ); 
  
      try 
      { 
          $stmt = $db-&gt;prepare($query); 
          $result = $stmt-&gt;execute($query_params); 
      } 
      catch(PDOException $ex) 
      {  
          die("Failed to run query: " . $ex-&gt;getMessage()); 
      } 
  
      $login_ok = false; 
  
  
      $row = $stmt-&gt;fetch(); 
      if($row) 
      { 
  
          $check_password = hash('sha256', $_POST['password'] . $row['salt']); 
          for($round = 0; $round &lt; 65536; $round++) 
          { 
              $check_password = hash('sha256', $check_password . $row['salt']); 
          } 
  
          if($check_password === $row['password']) 
          { 
              $login_ok = true; 
          } 
      } 
  
  
      if($login_ok) 
      { 
          unset($row['salt']); 
          unset($row['password']); 
  
          $_SESSION['user'] = $row; 
  
          header("Location: index.php"); 
          die("Redirecting to: index.php"); 
      } 
      else 
      { 
          print("Login Failed."); 
  
          $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
      } 
  } 
  
  ?&gt; 
  &lt;h1&gt;Login&lt;/h1&gt; 
  &lt;form action="login.php" method="post"&gt; 
      Username:&lt;br /&gt; 
      &lt;input type="text" name="username" value="&lt;?php echo $submitted_username; ?&gt;" /&gt; 
      &lt;br /&gt;&lt;br /&gt; 
      Password:&lt;br /&gt; 
      &lt;input type="password" name="password" value="" /&gt; 
      &lt;br /&gt;&lt;br /&gt; 
      &lt;input type="submit" value="Login" /&gt; 
  &lt;/form&gt; 
  </code></pre>