2014-10-31 24 views
6

我是$_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>
解決+教訓:-)

+1

你在谷歌禁止? http://php.net/manual/en/session.examples.basic.php http://www.tizag.com/phpT/phpsessions.php – Cheery 2014-10-31 20:45:55

+1

請[不要使用'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

+0

@Cherry我(總是和)已經閱讀和測試的基礎上的PHP手冊。但不能使它的工作。 – Nomis 2014-10-31 20:48:30

回答

2

首先你要打開PHP「標籤」(<?php session_start();... ?>

則必須在變量保存到會議結束後直接通過session_start();啓動SEESION。 您可以使用$_SESSION['word'] = $word;來達到此目的。

而在另一個文件中,您還必須在<?php「標記」之後的第一個使用session_start();

然後,您可以通過$word = $_SESSION['word'];訪問舊變量。

您現在還可以在第二個文件中使用$word。但是,如果它已設置(並且您在第一個文件之前的位置),則只能使用它。

文件之一:

<?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 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>'; 
    } 
    } 
}?> 

文件中的兩個:

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

希望這有助於;)在PHP文件的開頭

+0

我測試了你的建議,但起初它不起作用。原來我只需要在我的PAGE2.php文件中放置'<?php session_start();?>第一個。首先,我的意思是在<!DOCTYPE html> '等之前。 – Nomis 2014-11-03 09:08:50

2

使用PHP的會議,你會做以下:

發起會話,session_start();

注意:session_start();必須是您文件中的第一行PHP。

創建會話,$_SESSION['word'] = $word;

要訪問其他網頁:

發起會話,session_start();

訪問會話,$word = $_SESSION['word'];

0

session_start()意味着你正在使用會話變量,確保它位於頁面的頂部。要進行會話,請執行以下操作:$_SESSION['word'] = [some value]。只要頂部有session_start(),就可以在頁面之間使用。如果未設置初始化,請確保先設置它。

<?php session_start(); ?> 

... 

<?php 
    if (isset($_SESSION['word'])) { 
     $_SESSION['word'] = /* change existing session value */; 
    } else { 
     $_SESSION['word'] = /* new session value */; 
    } 
?> 
0

您首先應該使用函數'session_start()'開始會話。 將它放在你的index.php/bootstrap.php文件中(這是加載你的網站時總是加載的文件)。

之後,您可以使用'$ _SESSION'全局設置數據。

//In your index.php/boostrap.php 
session_start(); 

在你想保存$字的文件:

$_SESSION['myword'] = $word; 

,並從該點,你可以在另一頁上使用這個變量。

//On another page, after the variable is being set 
echo $_SESSION['myword']; 

注意,當您正在使用共享的虛擬主機,會話數據通常存儲在web服務器上的一個全球性的文件夾,可以通過服務器上的每個網站使用。爲了防止出現這種情況,您應該使用'session_save_path()'函數或通過創建自己的會話處理程序來更改會話保存路徑。

0

呼叫session_start();,如果有什麼是集合$_SESSION超級全局數組,您可以重新訪問它。

如果沒有設置你可以設置它:
$_SESSION['a name']= 'some thing';

因此,對於你的例子:
PHP文件1

<?php 
session_start(); 
$_SESSION['word'] = $_POST['word'] 
?> 

PHP FILE2

<?php 
session_start(); 
echo $_SESSION['word']; 
?> 
0

有一個重做許多方法來完成這一切取決於你想如何使用它。您可以將文件包含在該文件中,您可以使用require或require_once,或者可以使用會話超級全局。

$ _SESSION超級全局將可以訪問您的應用程序中的所有文件。你需要確保你做的唯一事情就是使用頁面上的session_start()作爲該頁面上的第一件事。如果在任何輸出到瀏覽器後使用session_start(),它將不起作用。通常你會想在你的index.php文件中運行session_start()作爲第一行。然後你可以使用...

<?php 
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 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>'; 
    } 
    } 
}?> 

然後在任何頁面,您希望獲得它只是CAL起來......

<?php 
    echo $_SESSION['word']; 

希望幫助

相關問題