码迷,mamicode.com
首页 > Web开发 > 详细

用PHP&JS实现的ID&密码校验程序

时间:2018-06-11 02:02:13      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:BMI   link   put   amp   ext   UI   页面   else   数据   

声明:本程序纯粹是本人在学习过程中突发奇想做的,并未考虑任何可行性,实用性,只是留下来供以后参考。

前端页面

sign.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset=‘utf-8‘>
        <title>QQ注册&登录</title>
        <link rel=‘stylesheet‘ type=‘text/css‘ href=‘./sign.css‘>
        <script src="http://cdn.bootcss.com/blueimp-md5/1.1.0/js/md5.js"></script>
        <script src=‘./sign.js‘ type=‘text/javascript‘> </script> 
    </head>
    <body>
        <div class=‘header‘>QQ注册&登录</div>
        <div class=‘container‘>
        <h3>注册</h3>
        <form name=‘signup‘ action=‘./signup.php‘ onsubmit=‘return validateForm1()‘ method=‘post‘>
            QQ:<input type=‘text‘ name=‘uid‘ id=‘userid1‘>
            密码:<input type=‘password‘ name=‘upd‘ id=‘userpassword1‘>
            <input type=‘submit‘ onsubmit="" value=‘注册‘>
        </form>
        <h3>登录</h3>
        <form name=‘signin‘ action=‘./signin.php‘ onsubmit=‘return validateForm2();‘ method=‘post‘>
            QQ:<input type=‘text‘ name=‘uid‘ id=‘userid2‘>
            密码:<input type=‘password‘ name=‘upd‘ id=‘userpassword2‘>
            <input type=‘submit‘ onsubmit="" value=‘登录‘>
        </form>
        </div>
    </body>
</html>

CSS

sign.css

* {
    margin: 0 0 0 0;
    padding: 0 0 0 0;
}

body {
    background:whitesmoke;
    font-size: 1em;
}
.header {
    height:3em;
    font-size: 2.5em;
    line-height: 3em;
    color:gray;
    border-bottom:solid black 1px;
    text-align: center;
}
.container {
    text-align: center;
}

JS

sign.js

function validateForm1(){
    var id = this[‘userid1‘].value;
    var password = this[‘userpassword1‘].value;
    if ( !id.match(/^\d{5,10}$/) ) {
            alert (‘ID必须为5至10位数字‘);
            return false;
        }
    if ( !password.match(/^[\d\w]{6,18}$/) ) {
            alert (‘密码必须为6至32位数字和(或)字母‘);
            return false;
        }
    this[‘userpassword1‘].value =  md5(password);
}

function validateForm2(){
    var id = this[‘userid2‘].value;
    var password = this[‘userpassword2‘].value;
    if ( !id.match(/^\d{5,10}$/) ) {
            alert (‘ID必须为5至10位数字‘);
            return false;
        }
    if ( !password.match(/^[\d\w]{6,18}$/) ) {
            alert (‘密码必须为6至18位数字和(或)字母‘);
            return false;
        }
    this[‘userpassword2‘].value =  md5(password);
}

服务器端脚本

signin.php

<?php
$uid = $_POST[‘uid‘];
$upd = $_POST[‘upd‘];

$upd = md5($upd);

$conn = mysqli_connect(‘localhost‘, ‘myqq‘, ‘myqq‘, ‘qq‘);
if (!$conn)
{
    die ("数据库连接失败" . mysqli_connect_error());
}

$sql_select = <<<EOF
select id,password from user where id = ‘$uid‘
EOF;
$result = $conn->query($sql_select);
if ( $result->num_rows == 1){
    //echo ‘用户存在‘;
    $dbupd = ($result->fetch_assoc())[‘password‘];
    if ( $dbupd == $upd ){
        echo "登录成功";
    }
    else {
        echo "密码错误";
        //echo "<br>" . $upd . "<br>" . $dbupd;
    }
    
}
else {
    echo "用户不存在";
}
?>

signup.php

<?php
$uid = $_POST[‘uid‘];
$upd = $_POST[‘upd‘];

$upd = md5($upd);
//echo $upd . ‘<br>‘;
//echo $uid . $upd;
$conn = mysqli_connect( ‘localhost‘, ‘myqq‘, ‘myqq‘, ‘qq‘ );
if (!$conn){
    die ("数据库连接失败" . mysqli_connect_error() );
}
 
$sql_select = <<<EOF
select id,password from user where id = ‘$uid‘
EOF;
//echo $sql_select;
$result = $conn->query( $sql_select ) ;
if ( $result->num_rows <= 0){
    $sql_insert = <<<EOF
insert into user values($uid, ‘$upd‘)
EOF;
    if ( $conn->query($sql_insert) === TRUE )
    {
        echo ‘注册成功‘;
    }
    else {
        echo "注册失败,请重试" . $sql . ‘<br>‘ . $conn->error;
    }
}
else {
    echo "用户已存在";
}
$conn-close();
?>

 

用PHP&JS实现的ID&密码校验程序

标签:BMI   link   put   amp   ext   UI   页面   else   数据   

原文地址:https://www.cnblogs.com/liupy/p/9165215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!