2014-08-31 116 views
-2

我在一個名爲navbar.php的文件上有我的導航欄腳本。我將該文件包含在我網站的所有其他頁面的頂部。我現在要做的是自定義導航欄名稱誰登錄。假設我有一個名爲帳戶process.php與變量$name = 'Bob'的PHP文件。我無法使這個變量顯示在導航欄中。將不同頁面的php變量顯示到html頁面

這裏是我的帳戶process.php

<?PHP 
//VARS: gets user input from previous sign in page and assigns it to local variables 
//$_POST['signin-email']; 
//$_POST['signin-pass']; 
$signin_email = $_POST['signin-email']; 
$signin_pass = $_POST['signin-pass']; 

// connects to MySQL database 
include("config.inc.php"); 
$link = mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db($db_name,$link); 

// checking for user input in the table 
$result = mysql_query("SELECT * FROM table WHERE email = '$signin_email' AND password = '$signin_pass'"); 
if (mysql_num_rows($result) > 0) { // if the username and password exist and match in the table 
    echo "Account Found"; 
    $account_arr = mysql_fetch_array($result); // contains all the data from the user's MySQL row 
    echo print_r($account_arr); 
} 
else { // if username and password don't match or they aren't found in the table 
    echo "The email or password you entered is incorrect."; 
} 
?> 

我試圖訪問navbar.php$account_arr變量,所以我可以在頂部顯示用戶的名字。我已經嘗試過,包括account-process.phpnavbar.php<?php include('account-process.php') ?>,然後訪問導航欄的html中的變量,但是當我嘗試這個時,頁面會變成空白。

navbar.php只是基本的腳本爲固定的導航欄與一些信息。爲什麼當我嘗試在其中包含php文件時會變成空白?

感謝

+2

因爲你沒有指示Apache的治療'.html'文件作爲PHP它變成空白。將其命名爲「navbar.php」,它將起作用。 – 2014-08-31 02:49:26

+0

對不起,navbar實際上是一個php。它應該是navbar.php。讓我編輯這個問題。 – applemavs 2014-08-31 02:50:37

+0

好東西,我不覺得過分自信;) – 2014-08-31 02:51:00

回答

1

更改導航欄的PHP文件和使用會話。 -EDIT-花了很長時間發佈。保持它爲PHP。

帳戶process.php:

<?php 
session_start(); 
//VARS: gets user input from previous sign in page and assigns it to local variables 
//$_POST['signin-email']; 
//$_POST['signin-pass']; 
$signin_email = $_POST['signin-email']; 
$signin_pass = $_POST['signin-pass']; 

// connects to MySQL database 
include("config.inc.php"); 
$link = mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db($db_name,$link); 

// checking for user input in the table 
$result = mysql_query("SELECT * FROM table WHERE email = '$signin_email' AND password = '$signin_pass'"); 
if (mysql_num_rows($result) > 0) { // if the username and password exist and match in the table 
    echo "Account Found"; 
    $account_arr = mysql_fetch_array($result); // contains all the data from the user's MySQL row 
    echo print_r($account_arr); 
    $_SESSION['name']=$account_arr['username']; 
} 
else { // if username and password don't match or they aren't found in the table 
    echo "The email or password you entered is incorrect."; 
} 

?> 

navbar.php:

<?php 
session_start(); 
echo "<div><p>Hello, my name is " . $_SESSION['name'] . ".</p></div>"; 
?> 

會話數據被存儲在稱爲cookie的PHPSESSID該瀏覽會話結束後過期。

您可以使用session_start()函數啓動或恢復會話。如果頁面包含非PHP生成的HTML,則必須在<!DOCTYPE html>之前調用此項。

數據存儲在稱爲$_SESSION的超全局關聯數組中。可以在任何調用了session_start的頁面上將信息發送到/從這個變量進行修改。

如果您不想使用會話,您可以創建自己的cookie並使用超全局的$_COOKIE

進一步信息:

http://php.net/manual/en/function.session-start.php

http://www.w3schools.com/php/php_sessions.asp

http://www.w3schools.com/php/php_cookies.asp

+0

*「會話數據存儲在名爲PHPSESSID的Cookie中,在瀏覽會話結束後過期。」*只有某些內容正確,會話ID位於服務器上的會話數據所在的cookie(或url)中 – 2014-08-31 03:05:19

+0

*「使用session_start()函數啓動或恢復會話,如果頁面包含非PHP生成的HTML,則必須在<!DOCTYPE html>之前調用它。」*再次不準確,只需在調用之前調用它即可任何輸出 – 2014-08-31 03:06:30

+0

感謝您的深入響應,我將嘗試一下,看看它是如何工作的。 – applemavs 2014-08-31 03:19:58