title: Undefined index in php (_SESSION['id'] and ['user'])
body: <p>I'm new to php and I'm following a tutorial to make a login panel. It works fine on the demo website but when I download the code and run on my machine, 5 notices popped up. They all look like: "Notice: Undefined index: submit in C:\xampp\htdocs\myfolder\demo.php on line 24". </p>
  
  <p>From other programming experiences I think that these means I didn't define the variables before using them. However, these variables seem to be existing in the system (from what I understand reading other questions >-&lt;).</p>
  
  <p>I attached my code below and marked the undefined index on the right. Can anyone help me explain what's wrong with the code and how can I solve it? Thanks a lot!</p>
  
  <pre><code>&lt;?php
  session_name('Login');
  session_start();
  
  if($_POST['submit']=='Login')                                     //undefined submit
  {
      $err = array();
  
      if(!$_POST['username'] || !$_POST['password'])
          $err[] = 'All the fields must be filled in!';
  
      if(!count($err))
      {
          $_POST['username'] = mysql_real_escape_string($_POST['username']);
          $_POST['password'] = mysql_real_escape_string($_POST['password']);
  
          // Escaping all input data
  
          $row = mysql_fetch_assoc(mysql_query("SELECT id,user FROM writers WHERE user='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));
  
          if($row['user'])
          {       
                      $_SESSION['user']=$row['user'];
              $_SESSION['id'] = $row['id'];
              // Store some data in the session
          }
          else $err[]='Wrong username and/or password!';
      }
  
      if($err)
      $_SESSION['msg']['login-err'] = implode('&lt;br /&gt;',$err);
      // Save the error messages in the session
  
      header("Location: demo.php");
      exit;
  }
  $script = '';
  
  if($_SESSION['msg'])
  {
      // The script below shows the sliding panel on page load
      ... 
  }
  ?&gt;
  
  &lt;head&gt;
      ......
  &lt;/head&gt;
  
  &lt;body&gt;   
  &lt;!-- Panel --&gt;
  &lt;div id="toppanel"&gt;
      &lt;div id="panel"&gt;
          &lt;div class="content clearfix"&gt;
          &lt;?php   
              if(!$_SESSION['id']):                     //undefined id
              ?&gt;
  
              &lt;div class="left"&gt;
                  &lt;form class="clearfix" action="" method="post"&gt;
                      &lt;h1&gt;Writer Login&lt;/h1&gt;
                      &lt;?php
                          if($_SESSION['msg']['login-err'])      //undefined login-err
                          {
                              echo '&lt;div class="err"&gt;'.$_SESSION['msg']['login-err'].'&lt;/div&gt;';
                              unset($_SESSION['msg']['login-err']);
                          }
                      ?&gt;
  
                      //Login form
                  &lt;/form&gt;
              &lt;/div&gt;
  
              &lt;?php
              endif;
              ?&gt;
          &lt;/div&gt;
      &lt;/div&gt; &lt;!-- /login --&gt;  
  
      &lt;!-- The tab on top --&gt; 
      &lt;div class="tab"&gt;
          &lt;ul class="login"&gt;
              &lt;li class="left"&gt;&amp;nbsp;&lt;/li&gt;
              &lt;li&gt;Hello &lt;?php echo $_SESSION['user'] ? $_SESSION['user'] : 'Guest';?&gt;!&lt;/li&gt;                      //undefined user
              &lt;li class="sep"&gt;|&lt;/li&gt;
              &lt;li id="toggle"&gt;                            
                  &lt;a id="open" class="open" href="#"&gt;&lt;?php echo $_SESSION['id']?'Open Panel':'Log In';?&gt;&lt;/a&gt;                             //undefined id
                  &lt;a id="close" style="display: none;" class="close" href="#"&gt;Close Panel&lt;/a&gt;         
              &lt;/li&gt;
              &lt;li class="right"&gt;&amp;nbsp;&lt;/li&gt;
          &lt;/ul&gt; 
      &lt;/div&gt; &lt;!-- / top --&gt;
  
  &lt;/div&gt; &lt;!--panel --&gt;
  </code></pre>
  
  <p>Sorry for the long code! I really not sure which ones will be relevant to the problem and do not dare to delete more. Thank you for your patience!</p>