Sono nuovo a $_SESSIONS
ma ho bisogno di riutilizzare le variabili tra diversi file php. Nel codice seguente voglio usare la variabile $word
in un altro file php, ma non sono sicuro di come farlo.Utilizzare le variabili SESSIONS tra diversi file php
Il mio file php assomiglia a questo:
<?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>';
}
}
}?>
In attesa di vostri suggerimenti.
--- sessioni UPDATE ancora non funziona su page2.php? ---
Non capisco perché $_SESSION
non funzioni. Una pagina1.php Posso echo($_SESSION['word'])
e ottenere il valore corretto, ma una pagina2.php ottengo ('$'."_SESSION['word'] isn't set because you had never been at file one");
Ho provato tutte le soluzioni di seguito ma nessuna ha funzionato = stesso risultato su page2.php.
Il mio file 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);
?>
Il mio file 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");
}
?>
Sto diventando pazzo per questo.
AGGIORNAMENTO - RISOLTO
ho provato tutti i suggerimenti qui sotto, ma nessuno di loro ha lavorato, che era strano perché ho potuto impostare e eco il sesson_id()
su page1.php e page2.php, ma su page2. php ho ottenuto un diverso sesson_id()
. Ho iniziato a esaminare le impostazioni delle mie sessioni MAMP, ma tutto era impostato correttamente. La soluzione è stata "semplicemente" per posizionare lo session_start();
nella parte superiore di page2.php. E dalla cima voglio dire prima di tutto anche la lezione + Risolto <!DOCTYPE html>
ecc
imparato :-)
State vietato in Google? http://php.net/manual/en/session.examples.basic.php http://www.tizag.com/phpT/phpsessions.php – Cheery
Per favore, [non usare le funzioni 'mysql_ *'] (http : //stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), non vengono più gestiti e sono [ufficialmente deprecati] (https://wiki.php.net/ rfc/mysql_deprecation). Scopri invece [istruzioni preparate] (http://en.wikipedia.org/wiki/Prepared_statement) e usa [PDO] (http://us1.php.net/pdo) o [MySQLi] (http: // us1.php.net/mysqli). [Questo articolo] (http://php.net/manual/en/mysqlinfo.api.choosing.php) ti aiuterà a decidere. –
@Cherry I (sempre e) hanno già letto e testato in base al manuale php.Ma non poteva farlo funzionare. – Nomis