php用户名和密码的简单验证
html页面提交form表单<html>
<head></head>
<body>
<form action = "login.php" method = "post">
username:<input type = "text" name = "username"></br>
password:<input type = "password" name = "password"></br>
<input type = "submit" value = "提交">
<input type = "reset" value = "重置">
</form>
</body>
</html>
php页面接收form表单,并进行处理
<?php
//设置用户名和密码
$username = "1111";
$password = "2222";
//接收从表单提交过来的值,并进行判断:
$user = isset($_POST['username'])?$_POST['username']:"";
$pwd = isset($_POST['password'])?$_POST['password']:"";
//用户名和密码的验证:
if($user == $username){
if($pwd == $password){
// echo "登陆成功";
}else{
die("密码错误"); //die是停止程序运行
}
}else {
die("用户名错误");
}
?>
注意若是账号密码错误,不跳转到php页面,仍返回到登录,那么请把die输出命令换成echo输出命令,因为die会直接显示验证结果,不会返回到用户名和密码输入画面。
//用户名和密码的验证:
if($user == $username){
if($pwd == $password){
// echo "登陆成功";
}else{
echo "<script>alert('密码错误,登陆失败!');history.go(-1);</script>";
}
}else {
echo "<script>alert('用户名错误,登陆失败!');history.go(-1);</script>";
}
很简单 密码为123456 你可以在login.php里面改
index.html 将以下代码放入 <body></body>里面就可以
<form method="post" action="login.php">
密码: <input type="password" name="password"/><br>
<input type="submit" name="submit" value="登陆">
</form>
login.php 完整的login.php内容
<?php
$password = isset($_POST['password']) ? $_POST['password'] : '';
if($password == '123456'){
echo "<script>alert('登陆成功。');location.href='in.html';</script>";
//不需要提示 则把 alert('登陆成功。'); 删除掉
}else{
echo "<script>alert('密码错误 登陆失败。。');location.href='error.html';</script>";
//不需要提示 则去掉 同上
}
?>
OK 了 简单吧。
页:
[1]