如何使用php检测用户名正确并登录
2022-09-15 本文已影响0人
picassagirl
1、在 phpMyadmin下创建表mymarket
2、添加id、useName、userPassword字段
3、在phpStudy_pro目录下的www文件夹中添加index.html文件及checklogin.php文件
4、在index.html中form 表单的action为"checklogin.php",method为post
5、在checklogin.php文件中写入以下代码
<?php
header('Content-type:text/html;charset=utf-8');
$conn = @mysqli_connect('localhost','admin','Fromchina1','mymarket');
if(empty($_POST['user-name'])){
echo "<script>alert('请输入用户名');window.location.href='/index.html';</script>";
}
if(empty($_POST['user-password'])){
echo "<script>alert('请输入密码');window.location.href='/index.html';</script>";
}
$name=$_POST['user-name'];
$password = $_POST['user-password'];
$refer = mysqli_query($conn,"select * from mymarket where userName ='$name'");
$dat = mysqli_fetch_assoc($refer);
if($dat){
if($dat['userPassword']==$password){
echo "<script>alert('登录成功');window.location.href='/index1.html';</script>";
}
else{
echo "<script>alert('登录失败!请输入正确密码');window.location.href='/index.html';</script>";
}
}
?>