我创建了一个登录页面和一个主页面,我喜欢在登录后回显我的用户名。我不确定要修复什么,或者添加什么。
以下是我的代码:
的login.php
<?php
session_start();
require_once './config/config.php';
//If User has already logged in, redirect to dashboard page.
if (isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in'] === TRUE) {
$_SESSION['first_name'] = $_POST['first_name'];
header('Location:index.php');
}
//If user has previously selected "remember me option", his credentials are stored in cookies.
if(isset($_COOKIE['username']) && isset($_COOKIE['password']))
{
//Get user credentials from cookies.
$username = filter_var($_COOKIE['username']);
$passwd = filter_var($_COOKIE['password']);
$db->where ("user_name", $username);
$db->where ("passwd", $passwd);
$row = $db->get('admin_accounts');
if ($db->count >= 1)
{
//Allow user to login.
$_SESSION['user_logged_in'] = TRUE;
$_SESSION['admin_type'] = $row[0]['admin_type'];
header('Location:index.php');
exit;
}
else //Username Or password might be changed. Unset cookie
{
unset($_COOKIE['username']);
unset($_COOKIE['password']);
setcookie('username', null, -1, '/');
setcookie('password', null, -1, '/');
header('Location:login.php');
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en-EN">
<h1>Login</h1>
<div class="work n5">
<div id="login">
<p style="font-size:130%;"><strong>Welcome.</strong> Please login.</p>
<form method="POST" action="authenticate.php">
<fieldset style="border:none;">
<p><input type="text" name="username" required="required" required value="Username" onBlur="if(this.value=='')this.value='Username'" onFocus="if(this.value=='Username')this.value='' "></p>
<p><input type="password" name="passwd" required="required" required value="Password" onBlur="if(this.value=='')this.value='Password'" onFocus="if(this.value=='Password')this.value='' "></p>
<p><a onclick="toggleModal()">Forgot Password?</a></p>
<?php
if(isset($_SESSION['login_failure'])){ ?>
<div class="alert alert-danger alert-dismissable fade in">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<?php echo $_SESSION['login_failure']; unset($_SESSION['login_failure']);?>
</div>
<?php } ?>
<p><input type="submit" style="font:inherit; font-size:13px;" value="Login"></p>
</fieldset>
</form>
</div>
</div>
Index.php(登录后的页面)
<?php
session_start();
require_once './config/config.php';
require_once 'includes/auth_validate.php';
//Get DB instance. function is defined in config.php
$db = getDbInstance();
//Get Dashboard information
$numCustomers = $db->getValue ("customers", "count(*)");
?>
<!DOCTYPE html>
<html lang="en-EN">
<div class="infoWrapper">
<div class="title">
<span>Welcome Back</span>
</div>
<div class="counter">
<span> <?php echo $_SESSION['user_name'];?> - Tech</span>
</div>
authenticate.php
<?php
require_once './config/config.php';
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$username = filter_input(INPUT_POST, 'username');
$passwd = filter_input(INPUT_POST, 'passwd');
$remember = filter_input(INPUT_POST, 'remember');
$passwd= md5($passwd);
//Get DB instance. function is defined in config.php
$db = getDbInstance();
$db->where ("user_name", $username);
$db->where ("passwd", $passwd);
$row = $db->get('admin_accounts');
if ($db->count >= 1) {
$_SESSION['user_logged_in'] = TRUE;
$_SESSION['admin_type'] = $row[0]['admin_type'];
if($remember)
{
setcookie('username',$username , time() + (86400 * 90), "/");
setcookie('password',$passwd , time() + (86400 * 90), "/");
}
header('Location:index.php');
exit;
} else {
$_SESSION['login_failure'] = "Invalid username or password";
header('Location:login.php');
exit;
}
}