PHP - 登录后在标题中显示用户名

时间:2016-12-22 15:32:55

标签: php html sql

我刚学会了如何在脚本中创建简单的日志记录。

用户刚登录时,会将您重新定向到主页,但不会记录该信息。

我有这个标题: enter image description here

在小主页图标的左侧,我想要用户名来显示谁已登录。

index.php (这是该标题的位置)

<table class="header-container">
<tr>
    <td style="width: 40%; text-align: left;"><a href="index.php"><img class="hover-cursor" src="Images/TEAMS_Logo.png"></a></td>
    <td style="width: 10%;" class="custom-header custom-font">TEAMS 
        <span style="color: #cc2020" class="custom-font">W</span><span style="color: #1a488c" class="custom-font">I</span><span style="color: #2a9e56" class="custom-font">K</span><span style="color: #d87f0a" class="custom-font">I </span><span style="font-size: 30px;" class="glyphicon glyphicon-globe"></span></td>
    <td style="width: 40%;">

    <!-- Username to go here -->

        <table style="text-align: right; width: 100%">
            <tr>
                <td style="text-align: right;"><span style="font-size: 24px; text-align: right;" class="hvr-icon-grow"></span></td>
            </tr>
        </table>
    </td>
</tr>

当我点击登录时,它会运行 login_form.php

<?php include 'connectionDetails.php'; ?>

<?php session_start(); ?>

<?php 

if (isset($_POST['username']) and isset($_POST['password'])) 
{
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];


    $sql = "SELECT * FROM Users WHERE Username = ? AND Password = ?";

    $user = $username; 
    $pass = $password;

    $stmt = sqlsrv_prepare($conn, $sql, array(&$user, &$pass));

    if( !$stmt ) 
    {
        die( print_r( sqlsrv_errors(), true));
    }   

    sqlsrv_execute($stmt);

    if (sqlsrv_execute($stmt) === true) 
    {
        header('location: index.php');
    }

}

?>

所以3件事。

1)如何在登录用户的索引页面标题中显示用户名?

2)如果输入的凭据不正确,我将如何添加到login_form.php以显示警告框?

3)用户登录后是否有办法隐藏登录按钮?

2 个答案:

答案 0 :(得分:1)

您可以使用会话:https://secure.php.net/manual/en/book.session.php

  

1)如何在索引页面的标题中显示用户名   登录的人?

在登录时将用户名存储在会话中

  

2)我如何添加到login_form.php以显示一个警告框   输入的凭据不正确?

if (sqlsrv_execute($stmt) === true) 
{
    header('location: index.php');
}
else {
    // not logged
}
  

3)用户登录后是否有办法隐藏登录按钮?

检查会话是否存在

答案 1 :(得分:1)

1)您可以将用户名存储在$ _SESSION中,然后将其回显到您想要的位置。

2)您可以在会话中添加另一个值,或者在条件为false时发布,然后回显一些html

3)您可以在会话中存储一个变量,告知Loginbutton是否显示。小例子:

if ($userloggedin == true) {
   //do not echo the button
} else {
  //echo the button
}
相关问题