【发布时间】:2014-07-12 21:37:31
【问题描述】:
我有一个登录脚本,其中重定向页面是根据用户的角色制作的,所以如果它是管理员,它会转到 admin.php,如果它的测试员转到 tester.php,所以在第一个会话中用户角色已给出,并且在我的另一个名为 login 的会话中,我给出了用户名。在我的数据库中,我还有一些来自用户配置文件的内容,我想要的是用户登录时显示的名字和姓氏。
在这里你可以看到我的验证脚本。
<?php
session_start();
$mysqli=new MySQLi("localhost", "root", "root", "portfolio");
$role="";
$username=filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password=filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
if($query=$mysqli->prepare("SELECT `role` FROM members WHERE username=? AND password=?"))
{
$query->bind_param("ss", $username, $password);
$query->execute();
$query->bind_result($role);
$query->fetch();
}
else
{
echo "Errors in the Query. ".$mysqli->error;
die();
}
if($role!="")
{
$_SESSION['ingelogt']=$username;
$_SESSION['user_role']=$role;
$location="$role.php"; // If role is admin this will be admin.php, if student this will be student.php and more.
header("location: $location"); // Redirect to the respective pages.
}
else
{
echo "Invalid password, username combination";
}
?>
这里是管理员成功登录后将被重定向的页面
<?php
session_start();
if(!isset($_SESSION['ingelogt']))
{
header("location: index.php"); // The user is not logged in. Redirect him to the login page.
}
$page_role="admin"; // This must be admin for admin.php and student for student.php and similar
$role=$_SESSION['user_role'];
if($role!=$page_role) // If student come to admin page by mistake or admin to student and similar
{
echo "You are not supposed to be here.";
die();
}
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile("admin.html");
libxml_use_internal_errors(false);
if($_SESSION['user_role']) {
$oUl = $dom->getElementById('navUl');
$oList = $dom->createElement('li');
$oLink = $dom->createElement('a');
$oLink->setAttribute('href','logout.php');
$oI = $dom->createElement('i');
$oI->setAttribute('class','icon-logout');
$oLink->appendChild($oI);
$oList->appendChild($oLink);
$oUl->appendChild($oList);
}
echo $dom->saveHTML();
?>
所以我想要的是当用户登录时,他的名字和姓氏将被显示。 我希望有人可以帮助我
更新的文件 验证.php
<?php
session_start();
// Making a connection with the database.
$mysqli=new MySQLi("localhost", "root", "root", "portfolio");
$role="";
// Declaring the username and password input.
$username=filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password=filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
// If role from members where username and password from inputs exicts in database bind parameters.
// If given parameters not excists in database die
if($query=$mysqli->prepare("SELECT `id`,`role` FROM members WHERE username=? AND password=?")) {
$query->bind_param("ss", $username, $password);
$query->execute();
$query->bind_result($id, $role);
$query->fetch();
} else {
echo "Errors in the Query. ".$mysqli->error;
die();
}
// If $role is filled make session for username to check if logged in and session role for redirect page.
// If $role and $username is not filled invalid password, username combination.
if($role!="") {
$_SESSION['ingelogt']=$username;
$_SESSION['user_role']=$role;
$_SESSION['user_id']=$id;
$location="$role.php";
header("location: $location");
} else {
echo "Invalid password, username combination";
}
?>
和 admin.php
<?php
session_start();
// If session is not ingelogt lead back to index.php.
if(!isset($_SESSION['ingelogt'])) {
header("location: index.php");
}
// The role that has access to this page.
$page_role="admin";
$role=$_SESSION['user_role'];
// If a user with a different role visits wrong page.
if($role!=$page_role)
{
echo "You are not supposed to be here.";
die();
}
// Start new DOMDocument and load html file.
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile("admin.html");
libxml_use_internal_errors(false);
// If user is logged in add logg out icon in the menu.
if($_SESSION['ingelogt']) {
$oUl = $dom->getElementById('navUl');
$oList = $dom->createElement('li');
$oLink = $dom->createElement('a');
$oLink->setAttribute('href','logout.php');
$oI = $dom->createElement('i');
$oI->setAttribute('class','icon-logout');
$oLink->appendChild($oI);
$oList->appendChild($oLink);
$oUl->appendChild($oList);
}
// Save DOMDocument with html document.
echo $_SESSION['user_id'];
echo $dom->saveHTML();
?>
【问题讨论】:
-
您的会员表是否有名字和姓氏?
-
是的,我的成员表包含以下内容。 id、名字、姓氏、用户名、密码和角色