当您通过我的登录表单登录时,authentication.php将检查输入中的数据是否存在于数据库中。当匹配时,用户将被定向到其角色的页面,因此可以说用户是管理员,他将被定向到admin.php。当用户成功登录后,我想显示一条消息,例如welcome firstname lastname。在我的数据库中,我有一个名为firstname的字段和一个名为lastname的字段。我希望有人可以帮助我,因为我似乎无法弄清楚:(
authentication.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 `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 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;
$location="$role.php";
header("location: $location");
} else {
echo "Invalid password, username combination";
echo "<br/><a href='login.html'>Click to go back</a>";
}
?>
管理员将被定向到的页面名为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 $dom->saveHTML();
?>
如果我以任何方式误解了您,请给我一个提示,我将删除此答案。尽管我假设您要执行的操作是根据用户的名和姓将问候语打印在页面上的某个位置。
基本上,一旦声明了$_SESSION
-element,就可以在不同的页面上进行访问(类似于$_COOKIE
,但不相同)。因此,最好的解决方案是$_SESSION
使用从数据库接收到的名字和姓氏来初始化变量,然后在所需的页面上打印这些变量(与角色所使用的方法相同)。
首先,你需要获取数据库中,可以通过改变来完成的名字if
在语句来authentication.php以下内容:
if($query=$mysqli->prepare("SELECT `role`, `firstname`, `lastname` FROM members WHERE username=? AND password=?")) //assuming that your columns are called `firstname` and `lastname`
要获取这些内容,您还需要将行进一步更改为:
$query->bind_result($role, $first, $last);
fetch
在下一行使用时,您的变量将被放入其相应的绑定变量中。因此,在该语句之后,您可以执行以下操作(最好在之后$_SESSION['user_role']=$role;
):
$_SESSION["firstname"] = $first;
$_SESSION["lastname"] = $last;
此后,您可以echo
在任何位置使用名字和姓氏(取决于您希望放置的位置...)。例如,如果您希望它显示在admin.php的顶部,则可以将其放在前面$dom = new DOMDocument();
:
echo "Hello " . $_SESSION["firstname"] . " " . $_SESSION["lastname"] . "!";
如果您不知道在哪里放置东西,请尝试重新阅读给定的说明。我的大多数示例只是要替换的东西(在这种情况下,您只需要找到相应的代码),否则,我尝试将您重定向。尽管意识到在不掌握正确代码的情况下了解此类事情很重要,但是我建议您尝试理解。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句