로그인 양식으로 로그인하면 authentication.php는 입력 데이터가 데이터베이스에 있는지 확인합니다. 일치하는 항목이있을 때 사용자는 자신의 역할에 대한 페이지로 이동하여 사용자가 admin이라고 말하면 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();
?>
HTTP 응답에서 클라이언트가 '위치'헤더를 건너 뛸 수 있다는 점에 주목하십시오. 이것은 브라우저를 리다이렉트하도록 "묻는"방법 일 뿐이지 만, "듣는"것을 의미하지는 않습니다. 사용자가 코드에서 그 지점을 지나갈 수 없도록하려면'header ("Location : some_uri")'뒤에'die()'또는'exit()'를 사용하십시오. – Max
좋습니다! 나쁘게 그 미래를 사용하십시오 :-) –