title: Trying to display welcome message when user logs in
body: <p>I want to display 'Welcome "username"' when a user logs in but it doesn't work. I might be doing something wrong with sessions.</p>
  
  <p>It gives an error message of:</p>
  
  <blockquote>
    <p>Notice: Array to string conversion</p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>   &lt;?php
      require("config.php");
      if(empty($_SESSION['user'])) 
      {
          header("Location: index.php");
          die("Redirecting to index.php"); 
      }
      $user = $_SESSION['user'];
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;title&gt;uvoluntary&lt;/title&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;meta name="viewport" content="width=device-width"&gt;
  &lt;link href="font/stylesheet.css" rel="stylesheet" type="text/css"&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div class="navbar navbar-fixed-top navbar-inverse"&gt;
    &lt;div class="navbar-inner"&gt;
      &lt;div class="container"&gt;
        &lt;a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"&gt;
          &lt;span class="icon-bar"&gt;&lt;/span&gt;
          &lt;span class="icon-bar"&gt;&lt;/span&gt;
          &lt;span class="icon-bar"&gt;&lt;/span&gt;
        &lt;/a&gt;
        &lt;a class="brand"&gt;Logged In&lt;/a&gt;
        &lt;div class="nav-collapse"&gt;
          &lt;ul class="nav pull-right"&gt;
            &lt;li&gt;&lt;a href="eventsignup.php"&gt;Create Event&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;My Events&lt;/a&gt;&lt;/li&gt;
            &lt;li class="divider-vertical"&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="logout.php"&gt;Log Out&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!-- MAIN CONTENT --&gt;
  &lt;div class="container content container-fluid" id="home"&gt;
  
  &lt;h1 style="text-align: center;"&gt;Admin Control Panel&lt;/h1&gt;
    &lt;!-- HOME --&gt;
  &lt;h2&gt;Welcome &lt;?php echo $user; ?&gt; &lt;/h2&gt;
  
  &lt;/div&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Config.php</strong></p>
  
  <pre><code>    &lt;?php 
  
      // These variables define the connection information for your MySQL database 
      $username = "root"; 
      $password = ""; 
      $host = "localhost"; 
      $dbname = "uvoluntarytest"; 
  
      $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); 
      header('Content-Type: text/html; charset=utf-8'); 
      session_start(); 
  ?&gt;
  </code></pre>
  
  <p><strong>Index.php</strong></p>
  
  <pre><code>    &lt;?php 
      require("config.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: secret.php"); 
              die("Redirecting to: secret.php"); 
          } 
          else{ 
               echo '&lt;h4 style="color:red;font-weight:bold; margin-top:40px; margin-left:850px;"&gt;Username And/or Password Incorrect. Please try again.&lt;/h4&gt;';
  
  
         }
      } 
  
  ?&gt; 
  </code></pre>