在每个HTML页面上打印用户名

时间:2015-10-29 23:52:12

标签: php html database echo

我的主页的HTML文件中有以下行。为什么在登录时不会打印出当前用户的姓名?我在html文件的body部分中有一行。我想把它放在我的所有页面上,但它不会显示。用户确实存在,因为它通过我的php echo mesage成功登录。

这一行:

<div id="usernameDiv"><?php echo $_SESSION['username']; ?></div> 

这是登录页面:

<?php
function SignIn() {
    require_once("constants.php"); //Now constants will be accessible
    session_start(); 
    try {
        $link = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD);
        $username = $_POST['username']; //no need to esaping as we will use prepared statements
        $password = $_POST['password'];
        if (!empty($username) && !empty($password)) {
            //You need to define a new column named "id" which will be int auto_increment and it will be your primary key

            $sql = "SELECT id, username, password FROM users where username = :username AND password = :password";
            //Prepare your query
            $stmt = $link->prepare($sql);
            //Execute your query binding variables values
            $stmt->execute(array(':username'=>$username, ':password'=>$password));
            //Fetch the row that match the criteria
            $row = $stmt->fetch();

            if (!empty($row['username']) && !empty($row['password'])) {
                $_SESSION['is_logged'] = true; //Now user is considered logged in
                $_SESSION['username'] = $row['username'];
                $_SESSION['id'] = $row['id'];

                //Never store passwords in $_SESSION

                echo "Welcome to your User Account for CSIT Conference. Click to go home: ";
                echo '<a href="index.html"> Home Page </a>. ';
                echo "Or here to go to your assigned papers: ";
                echo '<a href="assigned.php"> Assigned Papers </a>. ';
            } else {
                echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY...";
            }

            $link = null;
        } else {
            echo 'Please enter username and password.';
        }
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
}

if (isset($_POST['submit'])) {
    SignIn();
}
?>

这是主页。最终我想在所有页面上使用它。

<!DOCTYPE html>
<html>


<head>
  <title>Home Page</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link rel="import" href="navigation.html">




  </head>

<body>
<center> <b>World Congress CS-IT Conferences 2016</center>
<div id="horizontalmenu">
  <ul>
   <li><a href="index.html" title="Home Page">Home<br/></a></li>
   <ul> <li><a href="information.html">General Information</a> <ul> 
        <li><a href="about.html">About</a></li> 
        <li><a href="fee.html"> Conference Fee</a></li> 
        <li><a href="hotel.html">Hotel</a></li> </ul>
   <li><a href="keynote.html" title="Speakers">Keynote Speakers<br/></a></li>
   <li><a href="call.html" title="Call for Papers">Call for Papers<br/></a></li>
   <li><a href="dates.html" title="Important Dates">Important Dates<br/></a></li>
   <li><a href="major.html" title="Major Areas">Major Areas<br/></a></li>
   <li><a href="paper.html" title="Submit a Paper">Paper Submission<br/></a></li>
   <li><a href="reviewer.html" title="Login">Login<br/></a></li>
   <li><a href="register.html" title="Register online">Registration<br/></a></li>
   <li><a href="conference.html" title="Conference">Conference Program<br/></a></li>
   <li><a href="guidelines.html" title="Guidelines">Guidelines<br/></a></li>
   <li><a href="comments.html" title="Comments and Feedback">Comments<br/></a></li>
  </ul>


</nav></b>


<div id="usernameDiv"><?php echo $_SESSION['username']; ?></div> 

<br><br>
<div class="zoom pic">
 <center> <img src="images/technology.png" alt="portrait"> <center>
</div>


</body>

</html>

1 个答案:

答案 0 :(得分:1)

您正在SignIn()功能内启动会话。移除session_start(),然后将以下内容放在文件顶部以开始会话:

if (!session_id()) @session_start();

您还必须在要使用会话数据的任何位置包含上述行(例如,它应该是索引文件中的第一行。)

*所有这些假设您要么在PHP文件中执行PHP设置,要么您的文件实际上是index.php而不是index.html