我是$_SESSIONS
的新手,但需要它在不同的php文件之間重新使用變量。在下面的代碼中,我想在另一個php文件中使用變量$word
,但我不確定如何執行此操作。在不同的php文件之間使用SESSIONS變量
我PHP文件看起來是這樣的:
<?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>';
}
}
}?>
期待您的建議。
--- UPDATE會話仍然無法在page2.php上工作? ---
我不明白爲什麼$_SESSION
不起作用。一個page1.php我可以echo($_SESSION['word'])
並得到正確的值,但一個page2.php我得到('$'."_SESSION['word'] isn't set because you had never been at file one");
我測試了所有下面的解決方案,但他們都沒有工作= page2.php相同的結果。
我的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);
?>
我則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");
}
?>
我對此感到瘋狂。
更新 - 解決
我測試了所有下面的建議,但沒有一次成功這件事很奇怪,因爲我可以設置和page1.php中則page2.php回聲出sesson_id()
,但第2頁。 PHP我有一個不同的sesson_id()
。我開始關注我的MAMP會話設置,但一切都是正確的。該解決方案「簡單地」將session_start();
置於page2.php的頂部。而到了極頂我的意思是以前一切甚至<!DOCTYPE html>
等
解決+教訓:-)
你在谷歌禁止? http://php.net/manual/en/session.examples.basic.php http://www.tizag.com/phpT/phpsessions.php – Cheery 2014-10-31 20:45:55
請[不要使用'mysql_ *'函數](http ://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php),他們不再維護,並[官方棄用](https://wiki.php.net/ RFC/mysql_deprecation)。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。 [本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定。 – 2014-10-31 20:46:56
@Cherry我(總是和)已經閱讀和測試的基礎上的PHP手冊。但不能使它的工作。 – Nomis 2014-10-31 20:48:30