15621857753

PHP实例4:会员登录成功与失败的完整代码

来源:齐鲁CMS 栏目:PHP 阅读: 日期:2024-09-29

PHP实例4:会员登录成功与失败的完整代码

HTML代码:

<form action="index.php" method="post">
    <label for="name">账号:</label>
    <input type="text" id="name" name="name" required> <br>
    <label for="password">密码:</label>
    <input type="password" id="password" name="password" required> <br>
    <input type="submit" name="submit" value="登录">
</form>

PHP代码:

header('Content-Type: text/html; charset=utf-8');

// 假设数据库有如下登录账号
$userinfo = array('admin' => 'admin123', 'demo' => 'demo123', 'test' => 'test123',);

// 接收用户提交的登录数据
$uname = $_POST['name'];
$upassword = $_POST['password'];

// 将用户提交的数据与数据库登录账号相对比,正确则显示否则登录成功
foreach ($userinfo as $key => $val) {
    if ($uname == $key && $upassword == $val) {
        echo "登录成功!欢迎 {$uname}回家!";
        exit;
    }
}
echo '登录失败,请返回重试!';

实现效果:

PHP,PHP会员登录