Monday 15 February 2010

Use SESSIONS variables between different php files -



Use SESSIONS variables between different php files -

i new $_sessions needs re-use variables between different php files. in below code want utilize variable $word in php file, unsure how this.

my php file looks this:

<?php if (isset($_post["search"])) { //include database connection $word = mysql_real_escape_string($_post["search"]); $word = htmlentities($word); $sql = ("select task_id, task_date client bring together task on customer.id = task.customer_id mobil = $word order 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>'; } } }?>

looking forwards suggestions.

---update sessions still not working on page2.php?---

i not understand why $_session not work. 1 page1.php can echo($_session['word']) , right value, 1 page2.php ('$'."_session['word'] isn't set because had never been @ file one");

i tested below solutions none of them worked = same result on page2.php.

my page1.php file.

<?php session_start(); //if got 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 database $sql = ("select task_id, task_date client bring together task on customer.id = task.customer_id mobil = $word order task_date desc limit 0, 10"); // 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 page2.php file.

<?php session_start(); if(isset($_session['word'])) { $word = $_session['word']; echo($word); } else { die('$'."_session['word'] isn't set because had never been @ file one"); } ?>

i going insane on this.

update - solved tested below suggestions none of them worked weird because set , echo out sesson_id() on page1.php , page2.php, on page2.php got different sesson_id(). began mamp sessions settings, right set. solution "simply" place session_start(); on top on page2.php. , top mean before everything <!doctype html> etc. solved + lesson learned :-)

first must start seesion via session_start(); straight after opening php 'tag' (<?php session_start();... ?>)

then must save variable session. can utilize $_session['word'] = $word; purpose.

and in other file must utilize session_start(); @ first after <?php 'tag'.

then access old variable via $word = $_session['word'];.

you can utilize $word in sec file. can utilize if it's set (and @ first file before).

file one:

<?php session_start(); if (isset($_post["search"])) { //include database connection $word = mysql_real_escape_string($_post["search"]); $word = htmlentities($word); $_session['word'] = $word; $sql = ("select task_id, task_date client bring together task on customer.id = task.customer_id mobil = $word order 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>'; } } }?>

file two:

<?php session_start(); if(isset($_session['word'])) { $word = $_session['word']; } else { die('$'."_session['word'] isn't set because had never been @ file one"); } echo $word; ?>

hope helps ;)

php session

No comments:

Post a Comment