1. 程式人生 > >PHP PDO+MySQL實現登入註冊頁面

PHP PDO+MySQL實現登入註冊頁面

connect.php——連線資料庫,以後要連資料庫直接include,不用再一寫一大堆

<?php
header("Content-type: text/html;charset=utf-8");
$dbh = new PDO("mysql:host=localhost;dbname=DatabaseName", "username", "password");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

login介面

<!doctype html>
<html
>
<head> <meta charset="utf-8"> <title>LOGIN</title> <link href="login_css.css" rel="stylesheet" type="text/css"> </head> <body> <!-- 用table排版--> <table> <form name="login" method="post"> <tr> <td >
使用者名稱</td> <td> <input type="text" name="username"> </td> </tr> <tr> <td>密碼</td> <td> <input type="password" name="password"
>
</td> </tr> <!-- username和password 的輸入可以再加一個js來過濾注入資料庫 --> <tr> <td>&nbsp;</td> <td>&nbsp; <input type="submit" name="submit" value="submit" width="100%"> &nbsp; <input type="reset" value="reset"> &nbsp; <input type="button" value="註冊" onClick="window.location.href='register.php'"> </button></td> </tr> </form> </table> <?php if(isset($_POST["submit"]))//只用submit存在才能執行 { include("conect.php"); $usr=$_POST["username"]; $pwd=$_POST["password"]; // $_POST[" NAME "] 獲取post到php的name="NAME"的值 // $_GET[" NAME"] 同上,為get的值 $cusr=$dbh->query("select username from user where username='$usr';"); $cpwd=$dbh->query("select username from user where username='$usr' and password='$pwd';"); //執行查詢語句 $row1=$cusr->fetch(PDO::FETCH_BOTH); //$row1為cusr執行後將返回結果轉換成行陣列格式 $row2=$cpwd->fetch(PDO::FETCH_BOTH); if(empty($row1[0]))//若為空則表示沒有匹配到任何條目 { $dbh=null;//斷開資料庫 ?> <script> alert ("使用者名稱不存在"); login.onreset(); </script> <?php } else if(empty($row2[0]))//同上 { $dbh=null; ?> <script> alert ("使用者名稱或密碼錯誤"); login.onreset(); </script> <?php } else { $dbh=null; ?> <script> alert ("歡迎! <?php echo $usr;?>"); window.location.href="home_page"; </script> <?php } } ?> </body> </html>

註冊

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>註冊</title>
<script type="text/javascript" src="register_files/register_js.js"></script>
<link href="register_files/register_css.css" rel="stylesheet" type="text/css">
</head>
<body>
    <form onSubmit="return check()" action="" method="post" name="info" >
    <!-- 每個輸入都用js正則表示式過濾 -->
        <table>
            <tr>
                <td>*使用者名稱:</td>
                <td><input type="text" name="username" required="required" onChange="check_usr()"></td>
                <td><span>使用者名稱長度6-16位字母數字下劃線組合,第一位必須為字母 </span></td>
            </tr>
            <tr>
                <td><span id="usr"> </span></td>
            </tr>
            <tr>
                <td>*密碼:</td>
                <td><input type="password" name="password" required="required" onChange="check_pwd()"></td>
                <td><span>密碼長度6-16位字母數字下劃線 </span></td>
            </tr>
            <tr>
                <td><span id="pwd"> </span></td>
            </tr>
            <tr>
                <td>*再次輸入密碼</td>
                <td><input type="password" name="password_again" required="required" onChange="check_same()"></td>
                <td><span id="not_same"> </span></td>
            </tr>
            <tr>
                <td>*QQ:</td>
                <td><input type="text" name="qqnum" required="required" onChange="check_qq()"></td>
                <td><span id="qq"> </span></td>
            </tr>
            <tr>
                <td>*姓名:</td>
                <td><input type="text" name="name" required="required" ></td>
                <td><span> </span></td>
            </tr>
            <tr>
                <td></td>
                <td><center>
                        <input type="submit" name="submit" width="100%" value="submit">
                    </center></td>
            </tr>
        </table>
    </form>
<?php
if(isset($_POST["submit"]))
{
    include("connect.php");
    mb_http_input("utf-8");
mb_http_output("utf-8");
    $usr=$_POST["username"];
    $pwd=$_POST["password"];
    $qq=$_POST["qqnum"];
    $name=$_POST["name"];

    $sql = "insert into test.user values('$usr','$pwd',$qq,'$name');";
    $stmt=$dbh->query("select username from user where username='$usr';");
    //執行查詢語句
    $row=$stmt->fetch(PDO::FETCH_BOTH);
    if(empty($row[0]))//判斷是否存在
    {
        $dbh->exec($sql);
        $dbh = null;
?>
        <script>
        alert ("註冊成功!");
        window.location.href="index.php";
        </script>
<?php   
    }
    else
    {
        $dbh = null;
?>
        <script>
        alert ("使用者名稱已存在!");
        </script>
<?php
    }
}
?>
</body>
</html>

註冊input檢查js

function check()
{
    if(check_usr()&&check_pwd()&&check_same()&&check_qq())
    return true;
    else
    return false;
    //return false就會不執行submit
}
function check_usr()
{
    var x;
    x=document.forms["info"]["username"].value;
    var pat=/^[a-zA-Z]\w{5,15}$/;  //匹配正則表示式
    if(x.search(pat)==-1)
    {
        var txt=document.getElementById("usr").innerHTML;
        txt=txt.replace(txt,"使用者名稱格式錯誤!");
        document.getElementById("usr").innerHTML=txt;
        return false;
    }
    else
    {
        var txt=document.getElementById("usr").innerHTML;
        txt=txt.replace(txt," ");
        document.getElementById("usr").innerHTML=txt;
        return true;
    }
}
function check_pwd()
{
    var x;
    x=document.forms["info"]["password"].value;
    var pat=/^\w{6,16}$/;
    if(x.search(pat)==-1)
    {
        var txt=document.getElementById("pwd").innerHTML;
        txt=txt.replace(txt,"密碼格式不正確!");
        document.getElementById("pwd").innerHTML=txt;
        return false;
    }
    else
    {
        var txt=document.getElementById("pwd").innerHTML;
        var tmp=txt.replace(txt," ");
        document.getElementById("pwd").innerHTML=tmp;
        return true;
    }
}
function check_same()
{
    var x=document.forms["info"]["password"].value;
    var y=document.forms["info"]["password_again"].value;
    //從表中獲取input資訊
    //document.forms["表單名"]["表單中一項的名"].value;
    if(x!=y)
    {
        var txt=document.getElementById("not_same").innerHTML;
        txt=txt.replace(txt,"兩次輸入不一致!");
        document.getElementById("not_same").innerHTML=txt;
        return false;
    }
    else
    {
        var txt=document.getElementById("not_same").innerHTML;
        txt=txt.replace(txt," ");
        document.getElementById("not_same").innerHTML=txt;
        return true;
    }
}
function check_qq()
{
    var x;
    x=document.forms["info"]["qqnum"].value;
    pat=/[1-9][0-9]{4,}/;
    if(x.search(pat)==-1)
    {
        var txt=document.getElementById("qq").innerHTML;
        txt=txt.replace(txt,"QQ格式不正確!");
        document.getElementById("qq").innerHTML=txt;
        return false;
    }
    else
    {
        var txt=document.getElementById("qq").innerHTML;
        txt=txt.replace(txt," ");
        document.getElementById("qq").innerHTML=txt;
        return true;
    }
}

html js css php學習筆記

css

@charset "utf-8";
/* CSS Document */
.hhh{
    text-align:center;/*文字對齊方式*/
}
.testone{/*對所有class="testone"的標籤應用*/
    color:#D61AF0;
    margin-left:10%;/*縮排*/
}
#one{/*對所有id="one"的標籤應用*/
    color:#EF0F13;
}
table{/*對所有table標籤應用*/
    width:60%;
}
td{/*對所有td標籤應用*/
    text-align: justify;    /*文字的對齊方式*/
}
*{/*對所有標籤應用*/
    font-family:Constantia, "Lucida Bright", "DejaVu Serif", Georgia, serif;
    /*font-family  依次使用這些字型,因為不同客戶端字型庫不同*/
    font-size:xx-large;
    font-style:italic;
    font-weight:bold;
    /*傾斜在font-style 裡面,而加粗在font-weight裡面*/
}
/*每一條屬性後面都要有分號,最後一條可以不需要分號,但是作為習慣還是加上分號好*/

JavaScript

function check()/*定義函式:function 函式名(引數)*/
{
    if(check_usr()&&check_pwd()&&check_same()&&check_qq())
    return true;
    else
    return false;
}
function check_usr()
{
    var x;//宣告變數
    x=document.forms["info"]["username"].value;
//  從表單中根據name屬性獲得值
    var pat=/^[a-zA-Z]\w{5,15}$/;
    //正則表示式
    if(x.search(pat)==-1)
    {
    /*
    在字串中呼叫search方法應用正則表示式,返回第一個匹配到的位置,
    如果沒匹配到則返回-1
    */
        var txt=document.getElementById("usr").innerHTML;
        /*
        獲取id="usr"標籤中的內容,變數名為txt
        */
        txt=txt.replace(txt,"使用者名稱格式錯誤!");
        document.getElementById("usr").innerHTML=txt;
        /*標籤id="usr"中的內容變為新的txt*/
        return false;
    }
    else
    {
        var txt=document.getElementById("usr").innerHTML;
        txt=txt.replace(txt," ");
        document.getElementById("usr").innerHTML=txt;
        return true;
    }
}