【问题标题】:Print Username on each HTML Page在每个 HTML 页面上打印用户名
【发布时间】:2015-10-29 23:52:11
【问题描述】:

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

这一行:

<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>

【问题讨论】:

  • 你开始会话了吗?
  • 用户什么时候登录?我也会添加该代码
  • 您尝试输出会话数据的位置。这一页; &lt;div id="usernameDiv"&gt;&lt;?php echo $_SESSION['username']; ?&gt;&lt;/div&gt;.
  • 我的 index.html 文件。我在上面也加了
  • 您是否将php 配置为在.html 上执行?

标签: php html database echo


【解决方案1】:

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

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

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

* 所有这一切都假设您有 PHP 设置以在 .html 文件中执行,或者您的文件实际上是 index.php 而不是 index.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 2017-10-11
    • 2012-12-29
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2017-02-09
    • 2015-10-03
    相关资源
    最近更新 更多