1. 程式人生 > >php實現簡單的登入

php實現簡單的登入

我用了bootstrap框架,不知為什麼一直表單不能正常排序顯示,不過主要學習php邏輯,先不糾結。

登入頁:test.php

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3個meta標籤*必須*放在最前面,任何其他內容都*必須*跟隨其後! --> <title>Bootstrap 101 Template</title> <!-- Bootstrap --> <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script> <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> <![endif]-->
</head> <body> <div class="row"> <div class="col-md-12"> <form class="form-horizontal" action="produce_action.php" method="post"> <div class="form-group"> <div class="col-2"> <label for="inputEmail3">使用者名稱</label> </div> <div class="col-10"> <input type="text" class="form-control" id="inputEmail3" name="name" placeholder="使用者名稱"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">密碼</label> <div class="col-sm-10"> <input type="password" class="form-control" id="inputPassword3" name="pwd" placeholder="密碼"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">登入</button> </div> </div> </form> </div> </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="bootstrap/js/bootstrap.min.js"></script> </body> </html>

處理頁:
produce_action.php

<?php
header("Content-Type: text/html;charset=utf-8");
$name = $_POST["name"];
$pwd = $_POST["pwd"];
if(is_null($name) || strlen($name) ==0 ){
    echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('請輸入使用者名稱!');</script>";
}else if(is_null($pwd)||strlen($pwd) == 0){
    echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('請輸入密碼!');</script>";
}else{
    echo "name= ".$name;
    echo "<br/>pwd=".$pwd;
    if(!is_null($name))
        $_SESSION['userId']=$name;
    echo "<br/> session=".$_SESSION['userId'];
}
?>

進階版,增加資料庫判斷:

<?php
header("Content-Type: text/html;charset=utf-8");
$name = $_POST["name"];
$pwd = $_POST["pwd"];
if(is_null($name) || strlen($name) ==0 ){
    echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('請輸入使用者名稱!');</script>";
}else if(is_null($pwd)||strlen($pwd) == 0){
    echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('請輸入密碼!');</script>";
}else{
    $conn=mysql_connect("localhost", "root","root");
    if(!$conn){
        echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('連線資料庫失敗!');</script>";
        return;
    }

    mysql_select_db("test",$conn);
    $sql = "select * from users where userName='".$name."'";
    $info1=mysql_query($sql,$conn);
    if($row = mysql_fetch_object($info1)){
            if($row->UserPwd==$pwd){
                echo "登入成功!";
                echo '<br>';
                break;
            }else{
                echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('密碼錯誤!');</script>";
                break;
            }
    }else{
        echo "<script LANGUAGE='Javascript'> location.href='test.php'; alert('不存在該使用者!');</script>";
    }
}
?>

注意,這裡的test要替換成資料庫名,資料庫裡面要新增資料方便測試