PHP 中的甜蜜警报
Sweet Alert in PHP
我有 2 个文件,一个是 login.php,另一个是 loginprocess.php,我希望发生的是当我单击 login.php 中的提交按钮时,一个SweetAlert 通知将弹出它是一个成功的登录,然后重定向到另一个。php 页面,当凭据不正确时,它会显示 swal 错误并返回再次登录。
登录表单:
这是我的代码:
login.php
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<script src="sweet\node_modules\sweetalert\dist\sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="sweet\node_modules\sweetalert\dist\sweetalert.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
</head>
<body>
<form action="student_logprocess.php" method="POST">
<div class="container">
<h2><center>Student Login Form</center></h2><hr>
<h4><center>Please login to continue</center></h4>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<center><img src="img/user.png" class="img-circle img-thumbnail" width="200" alt="UserPhoto" />
</center>
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" placeholder="Username" name="txtusername" id="user">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="Password" name="txtpassword" id="pw">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button onclick="showmsg();" type="submit" class="btn btn-primary btn-lg btn-block" id="submit">Login</button>
</div>
</div>
</div>
</form>
<script type="text/javascript">
function showmsg()
{
var user = 'txtusername';
var pw = 'txtpassword';
var userName = document.getElementById('username').value;
var passWord = document.getElementById('password').value;
if((username == userName) && (pw == passWord)) {
swal("Good job!", "Login Success!", "success");
}
else{
swal("Oops...", "Invalid credentials!", "error");
}
}
</script>
<script src="jquery-3.1.1.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
loginprocess.php
<?php
require_once('student_conn.php');
$fname = $_POST['txtusername'];
$password = $_POST['txtpassword'];
$sql=$db->prepare("SELECT * FROM lgn_student WHERE fname=:txtusername AND password=:txtpass");
$sql->bindParam(':txtusername',$fname);
$sql->bindParam(':txtpass',$password);
$sql->execute();
If($row=$sql->rowCount()<>0){
session_start();
$_SESSION['account']=true;
header('location:student_main.php');
}else{
header('location:student_login.php');
}
?>
这里的问题是,我无法让它工作。我试过在这里搜索,但我不明白给出的代码。它对我不起作用。我在数据库中使用 xampp 和 navicat。
lgn_student 是 table 登录凭据
提前致谢!
** 编辑(使用 Michael S. 的代码) **
学生_login.php
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<script src="sweet\node_modules\sweetalert\dist\sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="sweet\node_modules\sweetalert\dist\sweetalert.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
</head>
<body>
<form action="student_logprocess.php" method="POST">
<div class="container">
<h2><center>Student Login Form</center></h2><hr>
<h4><center>Please login to continue</center></h4>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<center><img src="img/user.png" class="img-circle img-thumbnail" width="200" alt="UserPhoto" />
</center>
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" placeholder="Username" name="txtusername" id="user">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="Password" name="txtpassword" id="pw">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-primary btn-lg btn-block" id="submit">Login</button>
</div>
</div>
</div>
</form>
<script type="text/javascript">
$(function() {
var username = $('#user').val(),
password = $('#pw').val(),
smb = $('#submit');
smb.on('click', function(e){
e.preventDefault();
$.post( "/htdocs/0205_stud/student_logprocess.php",
{txtusername: username, txtpassword: password},
function( data ) {
var_dump( $sql->fetch() );die()
if(data.state == 1) {
swal("Good job!", "Login Success!", "success");
window.location.href = "student_main.php";
}
else
swal("Oops...", "Invalid credentials!", "error");
});
});});
</script>
<script src="jquery-3.1.1.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
student_logprocess.php
<?php
require_once('student_conn.php');
session_start();
$fname = $_POST['txtusername']; //Retrieve AJAX data
$password = $_POST['txtpassword']; //Retrieve AJAX data
$sql=$db->prepare("SELECT * FROM lgn_student WHERE fname=:txtusername AND password=:txtpass");
$sql->bindParam(':txtusername',$fname);
$sql->bindParam(':txtpass',$password);
$sql->execute();
if( $sql->fetch() ){
$_SESSION['account']=true;
$data['state'] = 1;
}
else{
$data['state'] = 0;
}
header("Content-type: application/json; charset=utf-8");
echo json_encode($data);
你不应该/不能只检查脚本代码中的用户名/密码是否正确,因为任何人都可以访问它。
如果符合您的逻辑,您可以使用 Ajax 和 Php 来执行此操作。
- 单击表单的“发送”按钮后,您的 Ajax 逻辑将捕获该操作,您可以使用该函数调用远程 php 文件来检查您的凭据,即student_logprocess.php
- php 文件 return 为您提供了答案,通常是带有消息的布尔状态。
- 在 Ajax 的 return 函数中,处理 php 响应以显示甜蜜的消息并重定向到另一个页面。
我没有在这里展示代码,因为 Whosebug 上已经回答了类似的问题,可以帮助你:
看看这个线程:PHP + Ajax Login
此外,如何使用 jQuery 执行 Ajax 调用:http://api.jquery.com/jquery.ajax/
希望对您有所帮助。
编辑
我评论了我对您的代码所做的编辑,以帮助您了解它应该如何工作。我将使用 jQuery 和 Ajax 对您的 php 文件执行异步调用。
从您的按钮中删除 showmsg() 函数:
<button type="submit" class="btn btn-primary btn-lg btn-block" id="submit">Login</button>
用下面这样的 Ajax 调用替换您的脚本内容,并在您的脚本之前调用 jQuery 而不是在
之后
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script type="text/javascript">
$(function() { //create a function that waits for the DOM to be ready
var username = $('#user').val(),
password = $('#pw').val(),
smb = $('#submit');
smb.on('click', function(e){ //Capture the submit button click
e.preventDefault(); //prevent the form to be sent when you click
//perform an Ajax POST. More info here : https://api.jquery.com/jquery.post/
$.post( "/path/to/your/student_logprocess.php",
{u: username, p: password},
function( data ) {
//In case of success, data will return what you defined in your php script. That'll allow you to trigger your swal notification
if(data.state == 1) {
swal("Good job!", "Login Success!", "success");
//redirect here
window.location.href = "http://example.com/account/";
}
else
swal("Oops...", "Invalid credentials!", "error");
});
});});
</script>
编辑您的 PHP 脚本以反映 AJAX 调用
<?php
session_start(); //Start session at the beginning of your script
$fname = $_POST['u']; //Retrieve AJAX data
$password = $_POST['p']; //Retrieve AJAX data
$sql=$db->prepare("SELECT * FROM lgn_student WHERE fname=:txtusername AND password=:txtpass");
$sql->bindParam(':txtusername',$fname);
$sql->bindParam(':txtpass',$password);
$sql->execute();
if( $sql->fetch() ){ //Fetch Single Result with pdo, use php function count to see if you have found something
$_SESSION['account']=true;
$data['state'] = 1;
}
else{
$data['state'] = 0;
}
header("Content-type: application/json; charset=utf-8"); //inform the browser we're sending JSON data
echo json_encode($data); //echoing JSON encoded data as the response for the AJAX call
?>
改进
在访问数据库之前检查您的用户数据。即使准备好的语句保护您免受对待,测试您的应用程序用户输入的内容(例如空字段等)也是一个很好的行为。例如,如果 $fname
或 $password
为空,您可以return $data['state'] = 0
直接跳过数据库请求。
您的数据库中的用户密码似乎很清楚,没有任何哈希值,这是一个严重的安全漏洞。为了改进您的代码,我鼓励您阅读 php 中的密码哈希,将哈希存储到您的 lng_student 密码列而不是直接存储密码:
我有 2 个文件,一个是 login.php,另一个是 loginprocess.php,我希望发生的是当我单击 login.php 中的提交按钮时,一个SweetAlert 通知将弹出它是一个成功的登录,然后重定向到另一个。php 页面,当凭据不正确时,它会显示 swal 错误并返回再次登录。
登录表单:
这是我的代码:
login.php
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<script src="sweet\node_modules\sweetalert\dist\sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="sweet\node_modules\sweetalert\dist\sweetalert.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
</head>
<body>
<form action="student_logprocess.php" method="POST">
<div class="container">
<h2><center>Student Login Form</center></h2><hr>
<h4><center>Please login to continue</center></h4>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<center><img src="img/user.png" class="img-circle img-thumbnail" width="200" alt="UserPhoto" />
</center>
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" placeholder="Username" name="txtusername" id="user">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="Password" name="txtpassword" id="pw">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button onclick="showmsg();" type="submit" class="btn btn-primary btn-lg btn-block" id="submit">Login</button>
</div>
</div>
</div>
</form>
<script type="text/javascript">
function showmsg()
{
var user = 'txtusername';
var pw = 'txtpassword';
var userName = document.getElementById('username').value;
var passWord = document.getElementById('password').value;
if((username == userName) && (pw == passWord)) {
swal("Good job!", "Login Success!", "success");
}
else{
swal("Oops...", "Invalid credentials!", "error");
}
}
</script>
<script src="jquery-3.1.1.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
loginprocess.php
<?php
require_once('student_conn.php');
$fname = $_POST['txtusername'];
$password = $_POST['txtpassword'];
$sql=$db->prepare("SELECT * FROM lgn_student WHERE fname=:txtusername AND password=:txtpass");
$sql->bindParam(':txtusername',$fname);
$sql->bindParam(':txtpass',$password);
$sql->execute();
If($row=$sql->rowCount()<>0){
session_start();
$_SESSION['account']=true;
header('location:student_main.php');
}else{
header('location:student_login.php');
}
?>
这里的问题是,我无法让它工作。我试过在这里搜索,但我不明白给出的代码。它对我不起作用。我在数据库中使用 xampp 和 navicat。
lgn_student 是 table 登录凭据
提前致谢!
** 编辑(使用 Michael S. 的代码) **
学生_login.php
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<script src="sweet\node_modules\sweetalert\dist\sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="sweet\node_modules\sweetalert\dist\sweetalert.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
</head>
<body>
<form action="student_logprocess.php" method="POST">
<div class="container">
<h2><center>Student Login Form</center></h2><hr>
<h4><center>Please login to continue</center></h4>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<center><img src="img/user.png" class="img-circle img-thumbnail" width="200" alt="UserPhoto" />
</center>
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" placeholder="Username" name="txtusername" id="user">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="Password" name="txtpassword" id="pw">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-primary btn-lg btn-block" id="submit">Login</button>
</div>
</div>
</div>
</form>
<script type="text/javascript">
$(function() {
var username = $('#user').val(),
password = $('#pw').val(),
smb = $('#submit');
smb.on('click', function(e){
e.preventDefault();
$.post( "/htdocs/0205_stud/student_logprocess.php",
{txtusername: username, txtpassword: password},
function( data ) {
var_dump( $sql->fetch() );die()
if(data.state == 1) {
swal("Good job!", "Login Success!", "success");
window.location.href = "student_main.php";
}
else
swal("Oops...", "Invalid credentials!", "error");
});
});});
</script>
<script src="jquery-3.1.1.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
student_logprocess.php
<?php
require_once('student_conn.php');
session_start();
$fname = $_POST['txtusername']; //Retrieve AJAX data
$password = $_POST['txtpassword']; //Retrieve AJAX data
$sql=$db->prepare("SELECT * FROM lgn_student WHERE fname=:txtusername AND password=:txtpass");
$sql->bindParam(':txtusername',$fname);
$sql->bindParam(':txtpass',$password);
$sql->execute();
if( $sql->fetch() ){
$_SESSION['account']=true;
$data['state'] = 1;
}
else{
$data['state'] = 0;
}
header("Content-type: application/json; charset=utf-8");
echo json_encode($data);
你不应该/不能只检查脚本代码中的用户名/密码是否正确,因为任何人都可以访问它。 如果符合您的逻辑,您可以使用 Ajax 和 Php 来执行此操作。
- 单击表单的“发送”按钮后,您的 Ajax 逻辑将捕获该操作,您可以使用该函数调用远程 php 文件来检查您的凭据,即student_logprocess.php
- php 文件 return 为您提供了答案,通常是带有消息的布尔状态。
- 在 Ajax 的 return 函数中,处理 php 响应以显示甜蜜的消息并重定向到另一个页面。
我没有在这里展示代码,因为 Whosebug 上已经回答了类似的问题,可以帮助你: 看看这个线程:PHP + Ajax Login
此外,如何使用 jQuery 执行 Ajax 调用:http://api.jquery.com/jquery.ajax/
希望对您有所帮助。
编辑
我评论了我对您的代码所做的编辑,以帮助您了解它应该如何工作。我将使用 jQuery 和 Ajax 对您的 php 文件执行异步调用。
从您的按钮中删除 showmsg() 函数:
<button type="submit" class="btn btn-primary btn-lg btn-block" id="submit">Login</button>
用下面这样的 Ajax 调用替换您的脚本内容,并在您的脚本之前调用 jQuery 而不是在
之后<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> <script type="text/javascript"> $(function() { //create a function that waits for the DOM to be ready var username = $('#user').val(), password = $('#pw').val(), smb = $('#submit'); smb.on('click', function(e){ //Capture the submit button click e.preventDefault(); //prevent the form to be sent when you click //perform an Ajax POST. More info here : https://api.jquery.com/jquery.post/ $.post( "/path/to/your/student_logprocess.php", {u: username, p: password}, function( data ) { //In case of success, data will return what you defined in your php script. That'll allow you to trigger your swal notification if(data.state == 1) { swal("Good job!", "Login Success!", "success"); //redirect here window.location.href = "http://example.com/account/"; } else swal("Oops...", "Invalid credentials!", "error"); }); });}); </script>
编辑您的 PHP 脚本以反映 AJAX 调用
<?php session_start(); //Start session at the beginning of your script $fname = $_POST['u']; //Retrieve AJAX data $password = $_POST['p']; //Retrieve AJAX data $sql=$db->prepare("SELECT * FROM lgn_student WHERE fname=:txtusername AND password=:txtpass"); $sql->bindParam(':txtusername',$fname); $sql->bindParam(':txtpass',$password); $sql->execute(); if( $sql->fetch() ){ //Fetch Single Result with pdo, use php function count to see if you have found something $_SESSION['account']=true; $data['state'] = 1; } else{ $data['state'] = 0; } header("Content-type: application/json; charset=utf-8"); //inform the browser we're sending JSON data echo json_encode($data); //echoing JSON encoded data as the response for the AJAX call ?>
改进
在访问数据库之前检查您的用户数据。即使准备好的语句保护您免受对待,测试您的应用程序用户输入的内容(例如空字段等)也是一个很好的行为。例如,如果
$fname
或$password
为空,您可以return$data['state'] = 0
直接跳过数据库请求。您的数据库中的用户密码似乎很清楚,没有任何哈希值,这是一个严重的安全漏洞。为了改进您的代码,我鼓励您阅读 php 中的密码哈希,将哈希存储到您的 lng_student 密码列而不是直接存储密码: