title: Use SESSIONS variables between different php files
body: <p>I am new to <code>$_SESSIONS</code> but needs it to re-use variables between different php files. In the below code I want to use the variable <code>$word</code> in another php file, but I am unsure how to do this. </p>
  
  <p>My <strong>php file</strong> looks like this:</p>
  
  <pre><code>&lt;?php
    if (isset($_POST["search"])) {    
  
      //include database connection
  
   $word = mysql_real_escape_string($_POST["search"]);
   $word = htmlentities($word);
  
   $sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10");
  
  $results = mysql_query($sql);
   if (mysql_num_rows($results)==0) {
    echo $word, " text bla";
  }else {
    echo $word, " text bla bla";
     while ($row = mysql_fetch_assoc($results)) {
      echo '&lt;pre&gt;', print_r($row), '&lt;pre&gt;';
     }
    } 
  }?&gt;
  </code></pre>
  
  <p>Looking forward to your suggestions.</p>
  
  <hr>
  
  <p>---<strong>UPDATE Sessions still not working on page2.php?</strong>---</p>
  
  <p>I do not understand why <code>$_SESSION</code> do not work. One page1.php I can <code>echo($_SESSION['word'])</code> and get the correct value, but one page2.php I get <code>('$'."_SESSION['word'] isn't set because you had never been at file one");</code> </p>
  
  <p>I tested all the below solutions but none of them worked = same result on page2.php.</p>
  
  <p><strong>My page1.php file.</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  
  //if we got something through $_POST
      if (isset($_POST["search"])) {  
  
      // include database connection
          $connect = mysql_connect('localhost', 'root', 'NomiS123') or die(mysql_error());
          mysql_select_db('workcard');
  
      // sanitize user input
          $word = mysql_real_escape_string($_POST["search"]);
          $word = htmlentities($word);
  
      // build search query to the database
          $sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10");
  
          // get results
          $_SESSION['word'] = $word;
          $results = mysql_query($sql);
          if (mysql_num_rows($results)==0) {
              $_SESSION['word'] = $word;
              echo($_SESSION['word']. "&lt;br&gt;");
              var_dump($_SESSION);
              echo "&lt;br&gt;";
              echo "link link &lt;br&gt;";
              echo "&lt;a href=\"../page2.php/\"&gt;new card&lt;/a&gt; &lt;br&gt;";
              echo "&lt;a href=\"//cykelc/\"&gt;New Search&lt;/a&gt;";
          }   else {
                  echo $word, " bla bla text &lt;br&gt; Create card &lt;br&gt;";
                  echo "Edit info on: ", $word, "&lt;br&gt;";
                  echo "&lt;a href=\"//cykelc/\"&gt;New Search&lt;/a&gt; &lt;br&gt;";
  
                  while ($row = mysql_fetch_assoc($results)) {
                      echo '&lt;pre&gt;', print_r($row), '&lt;pre&gt;';
                      }
                      //$results-&gt;free();
              }   
          }
  // mysql_close($connect);
  ?&gt;
  </code></pre>
  
  <p><strong>My PAGE2.php file.</strong></p>
  
  <pre><code>&lt;?php 
  session_start();
  if(isset($_SESSION['word'])) {
      $word = $_SESSION['word'];
      echo($word);
  } else {
      die('$'."_SESSION['word'] isn't set because you had never been at file one");
  }
  ?&gt;
  </code></pre>
  
  <p>I am going insane over this.</p>
  
  <hr>
  
  <p><strong>UPDATE - SOLVED</strong><br> 
  I tested all the below suggestions but none of them worked which was weird because I could set and echo out the <code>sesson_id()</code> on page1.php and page2.php, but on page2.php I got a different <code>sesson_id()</code>. I began to look into my MAMP sessions settings, but everything was correct set. The solution was "simply" to place the <code>session_start();</code> on the very top on page2.php. And by the very top I mean before <strong>everything</strong> even the <code>&lt;!DOCTYPE html&gt;</code> etc. <br> Solved  + lesson learned :-)</p>