emmm...
这个学期有一门php的课程。感觉思想和java差不多...
既然是和java差不多,理论上也做一个登录与注册的程序看下...
按照自己的思路完成了这个简陋的PHP登录注册程序...
不可否认,BUG也挺多的..走的弯路也挺多但过程也是挺美好的。
使用了easyui框架和bootstrap
登录后台有简单权限管理
最后还是java好玩!
源码下载:https://github.com/jiomer/regist
效果图:
以下是代码
数据库表:
1 2 3 4 5 6 7 8 9 10 |
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `uid` int(8) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL, `password` varchar(40) NOT NULL, `email` varchar(30) NOT NULL, `isadmin` enum('1','0') NOT NULL DEFAULT '0', `reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`uid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
conn.php
1 2 3 4 5 6 7 8 9 10 |
<?php //连接数据库 $conn = mysqli_connect('localhost','easy','root','easy'); if($conn){ // echo "连接成功!"; }else { exit("mysqli_connect 失败!!".mysqli_connect_errno()); } ?> |
img.php 验证码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php session_start(); //生成四位随机数 $pattern = '123456789ABCDEFGHIJKLOMNPQRSTUVWXYZ'; for($i = 0; $i < 4; $i++) { $code .= $pattern{mt_rand(0, 35)}; } $_SESSION['image'] = $code;//将随机数保存到session $im = imagecreatetruecolor(60,30);//创建一张宽60高30像素的图片 $bg = imagecolorallocate($im,222,222,222);//设置图片的背景颜色 imagefill($im,0,0,$bg);//载入背景颜色 $ft = imagecolorallocate($im,23,122,234);//设置字体颜色 $xian = imagecolorallocate($im,83,186,103);//线条的颜色 $dian = imagecolorallocate($im,205,229,92);//噪点的颜色 //imagefill($im,0,0,$dian); imageline($im,10,5,50,rand(0,20),$xian);//绘制一根线条 for($i = 0;$i < 100;$i++)//使用for循环来绘制多个噪点 { imagesetpixel($im,rand(10,50),rand(5,40),$dian); } imagestring($im,6,10,0,$code,$ft); //输出图片 header("Content-type:image/jpeg"); ob_clean(); imagejpeg($im); ?> |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
<?php //开启session session_start(); header("Content-Type: text/html; charset=utf-8"); //判断cookie是否含有username if(isset($_COOKIE['username'])){ $_SESSION['username'] = $_COOKIE['username']; } //判断session是否含有islogin if( isset($_SESSION['islogin']) ){ $admin = $_SESSION['admin']; $username = $_SESSION['username']; $userid = $_SESSION['userid']; ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Demo</title> <!-- easyui库导入开始 --> <link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css"> <link rel="stylesheet" type="text/css" href="easyui/demo/demo.css"> <link type="image/vnd.microsoft.icon" href="./img/favicon.png" rel="shortcut icon"> <script type="text/javascript" src="easyui/jquery.min.js"></script> <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script> <!-- easyui库导入结束 --> <link rel="stylesheet" type="text/css" href="css/myicon.css"> <style> .logo { width: 180px; height: 25px; line-height: 70px; text-align: center; font-weight: bold; font-size: 20px; float: left; } .logo a{ text-decoration: none; color: #fff; } .logout{ float: right; margin-top: 30px; margin-right: 10px; } </style> </head> <body class="easyui-layout"> <!-- 头部 --> <div data-options="region:'north'" style="height:70px;padding-left:10px;background: #000;"> <div class="logo"><a href="#">后台管理</a></div> <!--<div class="logout">你好,admin | <a href="#">退出</a></div>--> <div class="logout" style="padding:5px 0;"> <?php if($admin){ ?> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-myuser'">用户管理</a> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-mymenu'">菜单管理</a> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-myman'">角色管理</a> <?php } ?> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-myedit'">信息发布</a> <a href="loginout.php" class="easyui-linkbutton" data-options="iconCls:'icon-loginout'">登出</a> </div> </div> <!-- 底部--> <div data-options="region:'south',split:false" style="height:60px;"> <p style="height:20px;line-height:30px; text-align:center"> Theme by<a href="#" title="test"> Guoliangjun</a> Design. </p> </div> <!-- 左边--> <div data-options="region:'west',split:false" title="菜单导航" style="width:200px;padding:10px;"> <div class="easyui-panel" style="padding:6px;width: 178px;"> <ul class="easyui-tree"> <li> <span>系统菜单</span> <ul> <?php if($admin){ ?> <li data-options="state:'closed'"> <span>权限管理</span> <ul> <li> <span>用户管理</span> </li> <li> <span>菜单管理</span> </li> <li> <span>角色管理</span> </li> </ul> </li> <?php } ?> <li data-options="state:'closed'"> <span>新闻公告</span> <ul> <li>企业新闻</li> </ul> </li> <li data-options="state:'closed'"> <span>信息发布</span> <ul> <li>信息发布</li> </ul> </li> <li>个人信息</li> </ul> </li> </ul> </div> <!-- <input class="easyui-tagbox" value="搜索" style="width:100%" data-options="--> <!-- buttonText: '<?php echo "搜索";?>',--> <!-- onClickButton: function(){--> <!-- alert('click button');--> <!-- }--> <!--">--> <input class="easyui-searchbox" data-options="prompt:'<?php echo "搜索";?>',searcher:doSearch" style="width:178px"></input> <script> function doSearch(value){ alert('You input: ' + value); } </script> </div> <!-- 右边--> <div data-options="region:'center'"> <div class="easyui-tabs" style="width:100%;"> <div title="Title1" style="padding:10px"> <?php echo "<h1>".$username.",欢迎登录Demo后台!</h1>"; echo '用户ID:'.$userid.'<br />'; echo "是否为管理员:".$admin."<br />"; ?> 当前时间:<?php echo date("Y-m-d H:i");?> </div> <div title="Title2" id="Title2" style="padding:10px"> <?php echo "Title2"; include('selectuser.php'); ?> </div> <div title="Title3" style="padding:10px"> <?php echo "Title3"; ?> </div> </div> </div> </body> </html> <?php }else{ echo "<h1>非法登录!</h1>点击此处<a href=\"login.html\">登录</a>"; // header('refresh:10;url=login.html'); } ?> |
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php //设置编码格式 header("Content-Type: text/html; charset=utf-8"); //开启session session_start(); //REQUEST_METHOD是否为post if($_SERVER['REQUEST_METHOD']=='POST'){ if(!isset($_POST['login'])){ //exit('<h1>非法登录!</h1>"); echo "<h1>login.php非法登录!</h1>"; // header('refresh:2;url=login.html'); } $image = strtoupper($_POST['image']);//取得用户输入的图片验证码并转换为大写 $image2 = $_SESSION['image'];//取得图片验证码中的四个随机数 if($image == $image2){ //获取username和password $username = htmlspecialchars(trim($_POST['username'])); $password = md5(trim($_POST['password'])); //链接数据库 include('conn.php'); $check_query = mysqli_query($conn,"select uid,isadmin from user where username ='$username' and password='$password' limit 1"); if($result = mysqli_fetch_array($check_query)){ $_SESSION['username'] = $username; $_SESSION['userid'] = $result['uid']; //管理员权限 $_SESSION['admin']= $result['isadmin']; //验证成功,将username存到session $_SESSION['islogin'] = 1; if($_POST['remenber']=="ture"){ setcookie("username",$username,time()+60*60*24); // setcookie("code",md5($username.md5($password)),time()+60*60*24); }else{ setcookie("username",'',time()-1); //setcookie("code",'',time()-1); } header("location:index.php"); }else{ echo("用户名或密码错误,请重新登录!<a href=\"javascript:history.back(-1);\">返回</a>"); // header('refresh :0;url=404.html'); } }else{ echo "<script> alert('验证码错误!'); javascript:history.back(-1); </script>"; } }else{ echo "<h1>post非法登录!</h1><a href=\"javascript:history.back(-1);\">返回</a>"; } ?> |
loginout.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php //设置编码格式 header("Content-Type: text/html; charset=utf-8"); //开启session session_start(); //判断session是否含有username if(isset($_SESSION['username'])){ //清除session $username = $_SESSION['username']; // unset($_SESSION['userid']); // unset($_SESSION['username']); // unset($_SESSION['islogin']); // unset($_SESSION['isadmin']); $_SESSION = array(); session_destroy(); //清除cookie setcookie("username",'',time()-1); echo "<h1>".$username."注销成功!</h1>点击此处 <a href=\"login.html\">登录</a>"; exit(); } ?> |
reg.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php session_start(); header("Content-Type: text/html; charset=utf-8"); if(!isset($_POST['submit'])){ exit("<h1>非法登录!</h1>"); } $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $regimage = strtoupper($_POST['regimage']);//取得用户输入的图片验证码并转换为大写 $regimage2 = $_SESSION['image'];//取得图片验证码中的四个随机数 include('conn.php'); $check_query = mysqli_query($conn,"select uid from user where username ='$username' limit 1"); if($regimage == $regimage2){ if(mysqli_fetch_array($check_query)){ echo '错误:用户名 '.$username.' 已存在。<a href="javascript:history.back(-1);">返回</a>'; exit; } //写入 $password = md5($password); $sql = "insert into user (username,password,email) values ('$username','$password','$email');"; if(mysqli_query($conn,$sql)){ exit("用户注册成功,点击此处 <a href=\"login.html\">登录</a>"); }else { // echo '抱歉!添加数据失败:'.mysqli_error().'<br />'; echo '点击此处 <a href="javascript:history.back(-1);">返回</a> 重试'; } }else { echo "<script> alert('注册验证码错误!'); javascript:history.back(-1); </script>"; } ?> |
selectuser.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php //导入数据库 include('conn.php'); //查询 $sql='select uid,username,email,reg_date from user'; $result=mysqli_query($conn, $sql); if(mysqli_num_rows($result)>0) { echo ' <table border="1px" cellspacing="0" cellpadding="0" width=300>'; echo ' <tr><th>uid</th><th>姓名</th><th>email</th><th>regTime</th></tr>'; while($row=mysqli_fetch_assoc($result)) { echo "<tr><th>".$row['uid']."</th><th>".$row['username']."</th><th>".$row['email']."</th><th>".$row['reg_date']."</th></tr>"; } echo ' </table>'; } ?> |
register.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
<!DOCTYPE html> <head> <title>Rigister One</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link type="image/vnd.microsoft.icon" href="./img/favicon.png" rel="shortcut icon"> <link href="css/bootstrap/font-awesome.min.css" rel="stylesheet" type="text/css"> <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" type="text/css"> <link href="css/bootstrap/bootstrap-theme.min.css" rel="stylesheet" type="text/css"> <link href="css/bootstrap/templatemo_style.css" rel="stylesheet" type="text/css"> <script language = "javascript"> function checkform()//使用JS来验证用户输入是否符合规范 { if(myform.username.value == "")//昵称不能为空 { alert("昵称不能为空!"); myform.username.focus(); return false; } if(!myform.username.value.replace(/[^\a-\z\A-\Z]/g,''))//使用正则表达式来判断昵称 { alert("昵称不符合规范!"); myform.username.focus(); return false; } if(myform.username.value.length < 4 || myform.username.value.length > 8)//当用户输入的昵称小于4或者大于8时 { alert("昵称不符合规范!"); myform.username.focus(); return false; } if(myform.email.value == "")//邮箱不能为空 { alert("邮箱必须填写!"); myform.email.focus(); return false; } if(myform.email.value.length < 12)//邮箱不能少于12个字符,否则不符合规范 { alert("邮箱不符合规范!"); myform.email.focus(); return false; } if(myform.password.value == "")//密码不能为空 { alert("密码不能为空!"); myform.password.focus(); return false; } if(myform.password2.value == "")//密码不能为空 { alert("密码不能为空!"); myform.password2.focus(); return false; } if(!myform.password2.value.replace(/[^\a-\z\A-\Z]/g,''))//使用正则表达式来判断密码 { alert("密码不符合规范!"); myform.password2.focus(); return false; } if(myform.password2.value.length < 6)//如果密码小于6位 { alert("密码不能少于6位!"); myform.password2.focus(); return false; } if(myform.password.value != myform.password2.value)//判断两次输入的密码是否一致 { alert("两次输入的密码不一致!"); myform.password2.focus(); return false; }if(myform.regimage.value == "")//昵称不能为空 { alert("验证码不能为空!"); myform.regimage.focus(); return false; } } </script> </head> <body class="templatemo-bg-gray"> <div class="container"> <div class="col-md-12"> <h1 class="margin-bottom-15">注册系统</h1> <form class="form-horizontal templatemo-container templatemo-login-form-1 margin-bottom-30" role="form" name="myform" action="reg.php" onsubmit = "return checkform();" method="post"> <div class="form-group"> <div class="col-xs-12"> <div class="control-wrapper"> <label for="username" class="control-label fa-label"><i class="fa fa-user fa-medium"></i></label> <input type="text" class="form-control" id="username" name="username" placeholder="用户名"> </div> </div> </div> <div class="form-group"> <div class="col-md-12"> <div class="control-wrapper"> <label for="password" class="control-label fa-label"><i class="fa fa-lock fa-medium"></i></label> <input type="password" class="form-control" id="password" name="password" placeholder="密码"> </div> </div> </div> <div class="form-group"> <div class="col-md-12"> <div class="control-wrapper"> <label for="password2" class="control-label fa-label"><i class="fa fa-lock fa-medium"></i></label> <input type="password" class="form-control" id="password2" name="password2" placeholder="再次输入密码"> </div> </div> </div> <div class="form-group"> <div class="col-md-12"> <div class="control-wrapper"> <label for="email" class="control-label fa-label"><i class="fa glyphicon-envelope fa-medium"></i></label> <input type="email" class="form-control" id="email" name="email" placeholder="邮箱"> </div> </div> </div> <div class="form-group"> <div class="col-md-6"> <div class="control-wrapper"> <label for="regimage" class=" control-label fa-label"><i class="fa glyphicon-asterisk fa-medium"></i></label> <input type="text" class="form-control" id="regimage" name="regimage" placeholder="验证码"><img src = 'img.php' /> </div> </div> </div> <div class="form-group"> <div class="col-md-5"> <div class="control-wrapper"> <input type="submit" name="submit" value="注册" class="btn btn-info"> </div> </div> </div> <hr> </form> <div class="text-center"> </div> </div> </body> </html> |
login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<!DOCTYPE html> <head> <title>Login One</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link type="image/vnd.microsoft.icon" href="./img/favicon.png" rel="shortcut icon"> <link href="css/bootstrap/font-awesome.min.css" rel="stylesheet" type="text/css"> <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" type="text/css"> <link href="css/bootstrap/bootstrap-theme.min.css" rel="stylesheet" type="text/css"> <link href="css/bootstrap/templatemo_style.css" rel="stylesheet" type="text/css"> </head> <body class="templatemo-bg-gray"> <div class="container"> <div class="col-md-12"> <h1 class="margin-bottom-15">登录系统</h1> <form class="form-horizontal templatemo-container templatemo-login-form-1 margin-bottom-30" role="form" action="login.php" method="post"> <div class="form-group"> <div class="col-xs-12"> <div class="control-wrapper"> <label for="username" class="control-label fa-label"><i class="fa fa-user fa-medium"></i></label> <input type="text" class="form-control" id="username" name="username" placeholder="用户名"> </div> </div> </div> <div class="form-group"> <div class="col-md-12"> <div class="control-wrapper"> <label for="password" class="control-label fa-label"><i class="fa fa-lock fa-medium"></i></label> <input type="password" class="form-control" id="password" name="password" placeholder="密码"> </div> </div> </div> <div class="form-group"> <div class="col-md-6"> <div class="control-wrapper"> <label for="image" class=" control-label fa-label"><i class="fa glyphicon-asterisk fa-medium"></i></label> <input type="text" class="form-control" id="image" name="image" placeholder="验证码"><img src = 'img.php' /> </div> </div> </div> <div class="form-group"> <div class="col-md-12"> <div class="checkbox control-wrapper"> <label> <input type="checkbox" name="remenber" value="ture"> 记住我 </label> </div> </div> </div> <div class="form-group"> <div class="col-md-12"> <div class="control-wrapper"> <input type="submit" name="login" value="登录" class="btn btn-info"> <a href="#" class="text-right pull-right">管理员测试账号密码:admin;用户:test</a> </div> </div> </div> <hr> </form> <div class="text-center"> </div> </div> </body> </html> |
连个验证码都没和输入框在同一行,差评
@灰狼 估计html没学好吧~
@大雄 等下就改啊呜~
大神……!