我使用 html 和 php 创建了一些页面。在主页中我有所有页面的链接。现在我想为每个页面添加身份验证。未经身份验证,该页面不应打开。
为此,我创建了一个登录页面来检查身份验证。 现在,从主页,如果单击任何页面的链接,我想打开登录页面,如果登录成功,我想打开单击链接的页面。
主页:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Questions</title>
</head>
<body>
<a href="chapter.php">Add a chapter</a><br><br>
<a href="MainPage.php">Upload a file</a><br><br>
<a href="videoFile.php">Upload a video</a><br><br>
<a href="mcq.php">Add a question</a><br><br>
<a href="chapterDelete.php">Delete chapters</a><br><br>
<a href="deleteFiles.php">Delete Files</a><br><br>
<a href="deleteVideoFiles.php">Delete video Files</a><br><br>
<a href="deleteQuestions.php">Delete questions</a>
</body>
</html>
登录页面:
<!DOCTYPE html>
<html>
<head>
<body>
<form action="Login.php" method="post" enctype="multipart/form-data">
Enter Username : <input name = "userName" type = "text"><br><br>
Enter Password : <input name = "pass" type = "text"><br><br>
<input name="submit" type="submit" value = "Submit"><br><br>
<?php
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=174.138.74;dbname=_pro','rpro', 'ro12345');
if(isset($_POST['submit'])) {
if (!empty($_POST['userName']) && !empty($_POST['pass'])) {
$stmt = $dbh->prepare("select * FROM `users` WHERE `username`= :uName and `pass` = :pass");
$stmt->bindParam("uName", $_POST['userName']);
$stmt->bindParam("pass", $_POST['pass']);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if ($count > 0) {
echo 'Login Successful.';
?>
<?php
} else {
echo 'Please enter correct username and password.';
}
}
else{
echo 'Please enter username and password.';
}
}
?>
</form>
</body>
</head>
</html>
我怎样才能实现这个目标?请帮忙。谢谢..
最佳答案
您可以像这样创建所有链接...
<a href="login.php?redirect=chapter">Add a chapter</a><br><br>
<a href="login.php?redirect=MainPage">Upload a file</a><br><br>
<a href="login.php?redirect=videoFile">Upload a video</a><br><br>
<a href="login.php?redirect=mcq">Add a question</a><br><br>
<a href="login.php?redirect=chapterDelete">Delete chapters</a><br><br>
<a href="login.php?redirect=deleteFiles">Delete Files</a><br><br>
<a href="login.php?redirect=deleteVideoFiles">Delete video Files</a><br><br>
<a href="login.php?redirect=deleteQuestions">Delete questions</a>
然后,当用户单击任何链接时,它会将他们重定向到 login.php
页面。在那里对用户凭据进行各种验证,如果验证结果为 TRUE,则从上一个链接获取 URL 的 redirect
部分,并通过
$destination_page = $_GET['redirect'];
& 将用户重定向到该页面,否则要求用户再次登录。
关于javascript - 如何对加载页面进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43676807/