我創建了一個登錄頁面,允許您使用用戶名和密碼登錄。然而,目前是這樣,您總是被重定向到“個人資料”頁面,然后由 PHP 檢查登錄資料是否正確,因為資料在資料庫中。因此,我有一個想法,直接在登錄頁面上使用驗證功能來檢查資料是否正確,然后我只需創建一個會話。有誰知道我該怎么做?這是代碼:
<?php session_start();?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Login</title>
<link href="bootstrap_style.css" rel="stylesheet" id="bootstrap-css"> <!-- maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <!-- //maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js or bootstrap.js-->
<script src="jquery.js"></script> <!-- //cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper fadeInDown">
<div id="formContent">
<!-- Tabs Titles -->
<!-- Icon -->
<div class="fadeIn first">
<img src="default_logo2.png" id="icon" alt="User Icon" onclick="window.open('url', '_blank');"/>
</div>
<!-- Login Form -->
<form action="searchpage.php" method="post" onsubmit="validateForm(event);">
<input type="text" id="login" class="fadeIn second" name="username" placeholder="username">
<input type="password" id="password" class="fadeIn third" name="password" placeholder="password">
<input type="submit" class="fadeIn fourth" value="Log In">
</form>
<!-- Remind Passowrd -->
<div id="formFooter">
<a class="underlineHover" style="color:red" id="warning"></a>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
function validateForm(event) {
var input_username = document.getElementById("login");
var input_password = document.getElementById("password");
if (input_username.value == '' || input_username.value.charAt(0) == ' ' || input_password.value == '') {
event.preventDefault();
document.getElementById("warning").innerHTML = "Fill empty fields!";
}
else {
return true;
}
}
</script>
有了這個,我到達了資料庫:
$pdo = new PDO('mysql:host=localhost;dbname=ausleihe', 'root', '');
這就是選擇:
$login_session = "SELECT * FROM login WHERE username = '".$hereshouldbetheusernamefromform."' AND password = '".herethepasswordfromform."'";
如果您需要更多代碼或有疑問,請問我!謝謝
uj5u.com熱心網友回復:
客戶端JavaScript 不能直接訪問主機端PHP 程式,除非通過 AJAX 呼叫。只有主機端軟體可以訪問主機端資料庫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330457.html
標籤:javascript php 数据库 验证 pdo
