将不同页面的php变量显示到html页面
我在一个名为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.php在navbar.php与<?php include('account-process.php') ?>
,然后访问导航栏的html中的变量,但是当我尝试这个时,页面会变成空白。
navbar.php只是基本的脚本为固定的导航栏与一些信息。为什么当我尝试在其中包含php文件时会变成空白?
感谢
更改导航栏的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
*“会话数据存储在名为PHPSESSID的Cookie中,在浏览会话结束后过期。”*只有某些内容正确,会话ID位于服务器上的会话数据所在的cookie(或url)中 – 2014-08-31 03:05:19
*“使用session_start()函数启动或恢复会话,如果页面包含非PHP生成的HTML,则必须在之前调用它。”*再次不准确,只需在调用之前调用它即可任何输出 – 2014-08-31 03:06:30
感谢您的深入响应,我将尝试一下,看看它是如何工作的。 – applemavs 2014-08-31 03:19:58
因为你没有指示Apache的治疗'.html'文件作为PHP它变成空白。将其命名为“navbar.php”,它将起作用。 – 2014-08-31 02:49:26
对不起,navbar实际上是一个php。它应该是navbar.php。让我编辑这个问题。 – applemavs 2014-08-31 02:50:37
好东西,我不觉得过分自信;) – 2014-08-31 02:51:00