I am new to $_SESSIONS , but it needs to reuse variables between different php files. In the code below, I want to use the $word variable in another php file, but I'm not sure how to do this.
My php file is as follows:
<?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 '<pre>', print_r($row), '<pre>'; } } }?>
Wait for your offers.
--- UPDATE Sessions still not working on page 2.php? ---
I do not understand why $_SESSION does not work. One page1.php I can echo($_SESSION['word']) and get the correct value, but one page2.php I get ('$'."_SESSION['word'] isn't set because you had never been at file one");
I tested all the solutions below, but none of them worked = the same result on page 2.php.
My file is page1.php.
<?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']. "<br>"); var_dump($_SESSION); echo "<br>"; echo "link link <br>"; echo "<a href=\"../page2.php/\">new card</a> <br>"; echo "<a href=\"//cykelc/\">New Search</a>"; } else { echo $word, " bla bla text <br> Create card <br>"; echo "Edit info on: ", $word, "<br>"; echo "<a href=\"//cykelc/\">New Search</a> <br>"; while ($row = mysql_fetch_assoc($results)) { echo '<pre>', print_r($row), '<pre>'; } //$results->free(); } } // mysql_close($connect); ?>
My file is PAGE2.php.
<?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"); } ?>
I'm going crazy about this.
UPDATE - SOLVED
I tested all of the recommendations below, but none of them worked, which was strange because I could set and respond sesson_id() on page1.php and page2.php, but on page22.php I have another sesson_id() . I began to study the settings of the MAMP sessions, but everything was right. The solution was βsimpleβ to put session_start(); to the very top on page 2.php. And at the very top, I mean up to everything even <!DOCTYPE html> , etc.
Solved + lesson learned :-)