title: When a $_SESSION variable changes randomly after a page, what is the best way to troubleshoot it?
body: <p>I have been trying to fix this code for the last 4 hours and cannot seem to get it to work. The $_SESSION variables are set when the user log's in (or creates an account) and are destroyed when logging out. Yet, when I submit a certain form, suddenly $_SESSION variables which were working before are throwing an undefined variable error. My apologies for the large amount of content, but in due diligence I've come to the conclusion I cannot find it myself and must ask someone for help.</p>
  
  <p>Relevant code in the order a user's actions call the code.</p>
  
  <pre><code>&lt;?php
  include 'db_connect.php';
  include 'functions.php';
  session_start();
  
  
  // Define $myusername and $mypassword 
  $myusername=$_POST['myusername']; 
  $mypassword=$_POST['mypassword'];
  $_SESSION['sessionInitialize'] = false;
  // To protect mysqli injection (more detail about mysqli injection)
  //$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  $mypassword = md5(md5("SaLt".$mypassword."SaLt"));
  
  $query = "SELECT * FROM secure_login.members WHERE username='" . $myusername . "' and password='" . $mypassword . "'";
  $result = $mysqli-&gt;query($query) or die ($mysqli-&gt;error.__LINE__);
  
      // If result matched $myusername and $mypassword, table row must be 1 row
      if($result-&gt;num_rows == 1){
          initializeSessionVariables();
          // Register $myusername, $mypassword and redirect to file "acct.php"
          $_SESSION['currentUser']=$_POST['myusername']; 
          //$_SESSION['mypassword']=$_POST['mypassword']; 
          header("location:myAcct.php");
          }
      else {
          echo "Wrong Username or Password";
          header("location:index.php");
          }
  
  $mysqli-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>The function for initializing relevant session variables for the user.</p>
  
  <pre><code>function initializeSessionVariables(){
  
      $_SESSION['currentUser'] = $_SESSION['currentUser'];
      $_SESSION['currentUserAcctId'] = "";
      $_SESSION['currentUserSummonerId'] = "";
      $_SESSION['currentUserLeagues'] = "";
      $_SESSION['currentUserEmail'] = "";
      $_SESSION['currentUserAvatarURL'] = "";
      $_SESSION['currentUserSummName'] = "";
      $_SESSION['currentUserRealName'] = "";
      $_SESSION['currentUserBday'] = "";
      $_SESSION['currentUserSecondEmail'] = "";
  }
  </code></pre>
  
  <p>The account page.</p>
  
  <pre><code>&lt;?php
      $summonerName=$_SESSION['currentUser'];
      echo "Current User: " . $_SESSION['currentUser'] . "&lt;br&gt;";
      echo "&lt;br&gt;Current User \$summonerName: " . $summonerName; 
      //Create prepared statement.
      $query = "SELECT * FROM `stats`.`summoners` WHERE `summoners`.`name`='" . $summonerName . "'";
      $result = $mysqli-&gt;query($query) or die ($mysqli-&gt;error.__LINE__);
      echo "&lt;br&gt;test&lt;br&gt;&lt;br&gt;";
      Print_r ($result);
          //Run query if query object returned
          if ($result-&gt;num_rows == 0){
              echo "&lt;h1&gt;=0&lt;/h1&gt;";
              //Free the result so it can be used in the following functions
              $result-&gt;free();
              getSummonerData(); //defined in functions.php
              injectSummonerData(); //defined in functions.php
              $query = "SELECT * FROM `stats`.`summoners` WHERE name='" . $summonerName . "'";
              $result = $mysqli-&gt;query($query) or die ($mysqli-&gt;error.__LINE__);
              //Get associative array for $result
              $row = $result-&gt;fetch_assoc();
              $_SESSION['currentUserAcctId'] = $row['acctId'];
              //Print data.
              printf ("&lt;h3&gt;Summoner Name: %s\n &lt;br&gt; Summoner Level: %s\n &lt;br&gt; AcctID: %s\n &lt;br&gt; SummonerID: %s&lt;/h3&gt;", $row['name'], $row['summonerLevel'], $row['acctId'], $row['summonerId']);
              //Close result object
              //$result-&gt;close();
              echo "&lt;br&gt;";
              //Close DB Connection
              $mysqli-&gt;close();
          }
          else if($result-&gt;num_rows == 1){
              echo "&lt;h1&gt;=1&lt;/h1&gt;";
              $row = $result-&gt;fetch_assoc();
              $_SESSION['currentUserAcctId'] = $row['acctId'];
              printf ("&lt;h3&gt;Summoner Name: %s\n &lt;br&gt; Summoner Level: %s\n &lt;br&gt; AcctID: %s\n &lt;br&gt; SummonerID: %s&lt;/h3&gt;", $row['name'], $row['summonerLevel'], $row['acctId'], $row['summonerId']);
              echo "&lt;br&gt;&lt;h4&gt;This data is already in the database. Did nothing.&lt;/h4&gt;";
          }
      echo "&lt;br&gt;" . $_SESSION['currentUserAcctId'];
  ?&gt;
  </code></pre>
  
  <p>Profile.php</p>
  
  <pre><code>&lt;p&gt;Profile info&lt;/p&gt;
              &lt;?php
                  displayProfileInformation($_SESSION['currentUser']);
              ?&gt;
  </code></pre>
  
  <p>displayProfileInformation Function</p>
  
  <pre><code>function displayProfileInformation($currentUser){
      include 'dbstat_connect.php';
      $query =  "SELECT * FROM `stats`.`userAccount` where `userAccount`.`profName` = '" . $currentUser ."'";
      if ($result = $mysqli-&gt;query($query) or die ($mysqli-&gt;error.__LINE__)){
          if ($result-&gt;num_rows == 1){
              $row = $result-&gt;fetch_assoc();
              echo "User Name: "; if(isset($currentUser)){echo $currentUser . "&lt;br&gt;";}
              echo "Email: "; if(isset($row['email'])){echo $row['email'] . "&lt;br&gt;"; $_SESSION['currentUserEmail'] = $row['email'];}
              echo "Avatar URL: "; if(isset($row['avatarURL'])){echo $row['avatarURL'] . "&lt;br&gt;"; $_SESSION['currentUserAvatarURL'] = $row['avatarURL'];}
              echo "Summoner Name: "; if(isset($row['summName'])){echo $row['summName'] . "&lt;br&gt;"; $_SESSION['currentUserSummName'] = $row['summName'];}
              echo "Real Name: "; if(isset($row['realName'])){echo $row['realName'] . "&lt;br&gt;"; $_SESSION['currentUserRealName'] = $row['realName'];}
              echo "Birthdate: "; if(isset($row['bday'])){echo $row['bday'] . "&lt;br&gt;"; $_SESSION['currentUserBday'] = $row['bday'];}
              echo "Secondary Email: "; if(isset($row['secondEmail'])){echo $row['secondEmail'] . "&lt;br&gt;"; $_SESSION['currentUserSecondEmail'] = $row['secondEmail'];}
              echo "&lt;br&gt;Dafuq yo =1";
          }
          else if ($result-&gt;num_rows == 0){
              echo "Dafuq yo =0";
              echo "User Name: " . $currentUser . "&lt;br&gt;";
              echo "Email: &lt;br&gt;";
              echo "Avatar URL: &lt;br&gt;";
              echo "Summoner Name: &lt;br&gt;";
              echo "Real Name: &lt;br&gt;";
              echo "Birthdate: &lt;br&gt;";
              echo "Secondary Email: &lt;br&gt;";
              echo "&lt;h2&gt;Enter what information you like by editing your profile below.&lt;/h2&gt;&lt;br&gt;";
          }
          else {
              echo "Critical Error: Contact Admin.";
          }
  
  
      }
  }
  </code></pre>
  
  <p>editProfile.php</p>
  
  <pre><code>Edit Profile
          &lt;?php editProfileInformationForm($_SESSION['currentUser']); ?&gt;
  </code></pre>
  
  <p>editProfileInformationForm Function</p>
  
  <pre><code>function editProfileInformationForm($currentUser){
      echo "&lt;form action='processEditProfile.php' method='post'&gt;";
          echo "Profile Name: " . $_SESSION['currentUser'] . "&lt;br&gt;";
          echo "Account ID: " . $_SESSION['currentUserAcctId'] . "&lt;br&gt;";
          if (isset($_SESSION['currentUserEmail'])){
              echo "Email: &lt;input name='email' type='text' id='email' value='" . $_SESSION['currentUserEmail'] . "'/&gt;&lt;br /&gt;";
          }
          else {
              echo "Email: &lt;input name='email' type='text' id='email' value=''/&gt;&lt;br /&gt;";
          }
          if (isset($_SESSION['currentUserEmail'])){
              echo "Secondary Email: &lt;input name='secEmail' type='text' id='secEmail' value='" . $_SESSION['currentUserSecondEmail'] . "' /&gt;&lt;br /&gt;";
          }
          else {
              echo "Secondary Email: &lt;input name=secEmail type='text' id=secEmail value=''/&gt;&lt;br /&gt;";
          }
          if (isset($_SESSION['currentUserEmail'])){
              echo "Real Name: &lt;input name='realName' type='text' id='realName' value='" . $_SESSION['currentUserRealName'] . "' /&gt;&lt;br /&gt;";
          }
          else {
              echo "Real Name: &lt;input name='realName' type='text' id='realName' value=''/&gt;&lt;br /&gt;";
          }
          if (isset($_SESSION['currentUserAvatar'])){
              echo "Avatar: &lt;input name='avatar' type='text' id='avatar' value='" . $_SESSION['currentUserAvatarURL'] . "'/&gt;&lt;br /&gt;";
          }
          else {
              echo "Avatar: &lt;input name='avatar' type='text' id='avatar' value=''/&gt;&lt;br /&gt;";
          }
          if (isset($_SESSION['currentUserSummName'])){
              echo "Summoner Name: &lt;input name='summName' type='text' id='summName' value='" . $_SESSION['currentUserSummName'] . "'/&gt;&lt;br /&gt;";
          }
          else {
              echo "Summoner Name: &lt;input name='summName' type='text' id='summName' value=''/&gt;&lt;br /&gt;";
          }
          if (isset($_SESSION['currentUserBday'])){
              echo "Birthday: &lt;input name='bday' type='text' id='bday' value='" . $_SESSION['currentUserBday'] . "'/&gt;&lt;br /&gt;";
          }
          else {
              echo "Birthday: &lt;input name='bday' type='text' id='bday' value=''/&gt;&lt;br /&gt;";
          }
          echo "&lt;small&gt;(Bday Format~ YYYY-MM-DD)&lt;small&gt;";
      echo "&lt;input type='submit' name='submit' value='Submit'&gt;";
      echo "&lt;/form&gt;";
  }   
  </code></pre>
  
  <p>Finally, this is where the error is thrown with the 2 $_SESSION variables. There doesn't seem to be any reason for this, thus why I've come to you all.</p>
  
  <pre><code>&lt;?php
  include 'dbstat_connect.php';
  //include 'functions.php';
  //echo $_SESSION['currentUser'] . "&lt;br&gt;";
  //If stmt valid, prepare to insert profile info into `userAccount`
  if ($stmt = $mysqli-&gt;prepare("INSERT INTO `stats`.`userAccount` values (". $_SESSION['currentUserAcctId'] . ", " 
      . $_SESSION['currentUser'] . ", ?, ?, ?, ?, ?, ?, NULL, NULL, NULL, NULL, NULL)")){
      //Bind paramaters
      if($stmt-&gt;bind_param('ssssss', $_POST['email'], $_POST['avatar'], $_POST['summName'],
          $_POST['realName'], $_POST['bday'], $_POST['secEmail'])){
  
          //Execute the query. If true, show proof. If false, display error.
          if($stmt-&gt;execute()){
              //Show proof of insertion
              echo "&lt;h4&gt;Your data has been inserted.&lt;/h4&gt;";
          }
          //Check if stmt returned an error.
          else{
              Print_r ($stmt-&gt;get_warnings());
          }
          //Close statement
          $stmt-&gt;close();
      }
      //If $stmt statement returns an error, say so
      else if(!$stmt){
          printf ("Error: %s", $mysqli-&gt;error);
      }
      //Close DB Connection
      $mysqli-&gt;close();
  }
  
  
  echo $_POST['email'];
  
  
  ?&gt;
  </code></pre>
  
  <p>I really hope someone can help me. I've been stuck here for quite some time now.</p>