PHP 登录示例

  • 登录页面

    登录页面应如下所示,并根据会话工作。如果用户关闭会话,它将擦除会话数据。

    登录界面login.php

    <?php
    ob_start();
    session_start();
    ?>
    
    <?
    // error_reporting(E_ALL);
    // ini_set("display_errors", 1);
    ?>
    
    <html lang = "en" >
    
    <head>
            <title>jc2182.com</title>
            <link href = "bootstrap.min.css" rel = "stylesheet">
    
            <style>
                    body {
                            padding-top: 40px;
                            padding-bottom: 40px;
                            background-color: #ADABAB;
                    }
    
                    .form-signin {
                            max-width: 330px;
                            padding: 15px;
                            margin: 0 auto;
                            color: #017572;
                    }
    
                    .form-signin .form-signin-heading,
                    .form-signin .checkbox {
                            margin-bottom: 10px;
                    }
    
                    .form-signin .checkbox {
                            font-weight: normal;
                    }
    
                    .form-signin .form-control {
                            position: relative;
                            height: auto;
                            -webkit-box-sizing: border-box;
                            -moz-box-sizing: border-box;
                            box-sizing: border-box;
                            padding: 10px;
                            font-size: 16px;
                    }
    
                    .form-signin .form-control:focus {
                            z-index: 2;
                    }
    
                    .form-signin input[type="email"] {
                            margin-bottom: -1px;
                            border-bottom-right-radius: 0;
                            border-bottom-left-radius: 0;
                            border-color:#017572;
                    }
    
                    .form-signin input[type="password"] {
                            margin-bottom: 10px;
                            border-top-left-radius: 0;
                            border-top-right-radius: 0;
                            border-color:#017572;
                    }
    
                    h2{
                            text-align: center;
                            color: #017572;
                    }
            </style>
    
    </head>
    
    <body>
    
    <h2>输入用户名和密码</h2>
    <div class = "container form-signin">
    
            <?php
            $msg = '';
    
            if (isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password'])) {
    
                    if ($_POST['username'] == 'jc2182' && $_POST['password'] == '123456') {
                            $_SESSION['valid'] = true;
                            $_SESSION['timeout'] = time()+ 2400;  // 一个小时过期
                            $_SESSION['username'] = $_POST['username'];
    
                            echo '您输入了有效的用户名和密码';
    
                            // 两秒后跳转到 welcome.php
                            header('Refresh: 2; URL = welcome.php');
                    }else {
                            $msg = '错误的用户名或密码';
                    }
            }
            ?>
    </div> <!-- /container -->
    
    <div class = "container">
    
            <form class = "form-signin" role = "form"  action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);  ?>" method = "post">
                    <h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
                    <input type = "text" class = "form-control"  name = "username" placeholder = "username = jc2182" required autofocus></br>
                    <input type = "password" class = "form-control"  name = "password" placeholder = "password = 123456" required>
                    <button class = "btn btn-lg btn-primary btn-block" type = "submit"  name = "login">登录</button>
            </form>
    
    
    </div>
    
    </body>
    </html>
    
    上面的代码加载了一个样式文件bootstrap.min.css,是前端界面框架bootstrap的样式文件,下载地址:》》》》》》》》

    登录成功页面:welcome.php

    <html lang = "en" >
    <?php
    session_start();
    if (isset($_SESSION["username"]) && $_SESSION['valid'] == true){  // 登录成功的
            echo '欢迎您:::<span style="color: green">'.$_SESSION['username'].'</span><br/>';
    ?>
            点击这里<a href = "logout.php" tite = "退出登录">退出登录.</a>
    <?php
    }else{ //未经登录直接访问这个页面的
            echo '请您先登录<br/>';
            // 两秒后跳转到 login.php
            header('Refresh: 2; URL = login.php');
    }
    ?>
    </html>   
    

    退出登录页面:logout.php

    <?php
    session_start();
    unset($_SESSION["username"]);
    unset($_SESSION["password"]);
    unset($_SESSION['valid']);
    
    echo '您清除了会话';
    // 两秒后跳转到 login.php
    header('Refresh: 2; URL = login.php');
    ?>
    
    提示:一般用户信息都保存在数据库,登录的时候回跟数据库的数据校检,还用用PHP进行一些必要的防SQL注入处理。