在html页面上显示不同页面中的php变量

在html页面上显示不同页面中的php变量,php,html,mysql,web,php-include,Php,Html,Mysql,Web,Php Include,我在一个名为navbar.php的文件中有一个我的navbar脚本。我把这个文件放在我网站所有其他页面的顶部。我现在要做的是用登录者的名字定制导航栏。假设我有一个名为account-process.php的php文件,其中有一个变量$name='Bob'。我很难让这个变量显示在导航栏中 下面是我的account-process.php: <?php session_start(); //VARS: gets user input from previous sign in page and

我在一个名为navbar.php的文件中有一个我的navbar脚本。我把这个文件放在我网站所有其他页面的顶部。我现在要做的是用登录者的名字定制导航栏。假设我有一个名为account-process.php的php文件,其中有一个变量
$name='Bob'
。我很难让这个变量显示在导航栏中

下面是我的account-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.";
}

?>
更改PHP文件的导航栏并使用会话-编辑-发布花费了很长时间。保持它为PHP

account-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.";
}

?>

它变为空白,因为您没有指示Apache将
.html
文件视为PHP。将它命名为
navbar.php
,它就会工作。对不起,navbar实际上是一个php。它应该是navbar.php。让我来编辑这个问题。幸好我没有感到过于自信;)死亡白色页面,错误报告\显示关闭,打开
error\u reporting(E\u ALL);ini设置(“显示错误”,1)旁注/Q:这是一个实时站点还是只是为了学习?如果是实时的,请告诉我您正在对密码进行哈希运算。“会话数据存储在一个名为PHPSESSID的cookie中,该cookie在浏览会话结束后过期。”只有部分正确,会话id在cookie(或url)中,会话数据在服务器上“您使用会话启动()启动或恢复会话”函数。如果页面包含非PHP生成的HTML,则必须在调用之前调用该函数。“同样,这不是很准确,只需在任何输出之前调用它。感谢您的深入响应,我将尝试一下,看看它是如何工作的。它看起来很有效!非常感谢你。我可能应该在一个单独的问题中问这个问题,但以防万一有一个快速的解决方案:是否有一种方法可以根据用户是否登录来修改导航栏?(将某些元素替换为其他元素)例如,我希望我的“登录”按钮替换为用户名。applemavs没问题@龙,谢谢你纠正这一点
<?php
session_start();
echo "<div><p>Hello, my name is " . $_SESSION['name'] . ".</p></div>";
?>