自己学习html、CSS和PHP,在给自己的网站写注册和登录的时候遇到了不小的麻烦,而且现在也没弄清楚,暂时可能不再研究这方面了,所以先把我学会的部分记录下来。
首先是数据连接快捷键"ctrl+R"打开运行,输入services.msc回车,打开服务,找到MySQL57,检查数据库是否在运行,如果在运行,就不用管它,如果没运行,就右键“启动”它。
启动数据库以后,手动建立一个叫“tz”的数据库,然后建立一个叫user的表,里面有username、password和email的table,下面就是这个步骤的具体过程:
我数据库在 C:\Program Files\MySQL\MySQL Server 5.7
在控制台下进入 C:\Program Files\MySQL\MySQL Server 5.7\bin
接下来输入 mysql -h localhost -u root -p
然后创建数据库 create database tz;
使用这个数据库 use tz;
创建表 create table user(username char(30),password char(100),email char(30));
数据的准备工作就已经完成了。
接下来是表单提交和PHP文件把内容存给数据库,上代码,这是html的代码:
1 | < h1 >注册(Registered)</ h1 >< form name = "form1" action = "submit_info.php" method = "post" > 账户 < input type = "text" name = "username" placeholder = "请输入您的用户名!" > < br />密码 < input type = "password" name ="password" size = 30 onKeyUp = pwStrength (this.value) onBlur = pwStrength (this.value) placeholder = "请输入您的用户密码!" > < span id = "passw" ></ span > < br />邮箱 < input size = 30 id = "email" name = "email" onBlur = "checkemail()" type = "text" placeholder = "请输入您的邮箱!" /> < span id = "mail" ></ span >< br >< img id = "codeP" src = "picture.php" ο nclick = "refresh()" />< input type = "Captcha" id = "Captcha" placeholder = "请输入验证码!" name = "captcha_u" style = "width:130px;" > < span class = "" >点击图片刷新验证码</ span >< br />< button type = "submit" value = "submit" name = "submit" >注册</ button > |
上面代码里面有一个函数在JS里面
1 | <script> function refresh() { document.getElementById( "codeP" ).src = "picture.php?tm=" +Math.random(); } </script> |
如何生成随机验证码呢?看看这个PHP代码,文件picture.php
1 | <?phpsession_start();header( "Cache-Control: no-cache, must-revalidate" ); // 声明图像大小$img_height=70;$img_width=35;$authnum='';// 验证码内容$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";$list=explode(",",$ychar);for($i=0;$i<4;$i++){ $randnum=rand(0,35); $authnum.=$list[$randnum];}$_SESSION["captcha"]=$authnum;// 生成一个基本大小图像$aimg = imagecreate($img_height,$img_width);// 图像填充颜色imagecolorallocate($aimg, 255,255,255);$black = imagecolorallocate($aimg, 0,0,0); for ($i=1; $i<=100; $i++) { imagestring($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"@",imagecolorallocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));} //为了区别于背景,这里的颜色不超过200,上面的不小于200for ($i=0;$i<strlen($authnum);$i++){ imagestring($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(2,7),mt_rand(1,$img_width/2-2), $authnum[$i],imagecolorallocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));}imagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//画一个矩形Header("Content-type: image/PNG");ImagePNG($aimg); //生成png格式ImageDestroy($aimg); |
这是PHP代码,文件submit_info.php
1 | <?phpsession_start(); //启动session$capt= $_SESSION["captcha"];//获得验证码if(isset($_POST["submit"])) {//判断是否点击提交 $user = $_POST["username"]; $psw =md5( $_POST["password"]); $email = $_POST["email"]; if ($user == "" || $psw == "" || $email == "") {//用户名、密码、邮箱一旦有为空的,就刷新页面 echo "<script>alert('please again!');history.go(-1);</script>"; } else { if($_POST["captcha_u"]==$capt){ $link=mysqli_connect('localhost','root','1234','tz');//连接数据库 mysqli_select_db($link,"tz");//选择tz数据库 mysqli_query($link,'set name gb2312'); $sql="select username from user where username='$_POST[username]'";//在username当中寻找,判断是否已存在用户名 $result=mysqli_query($link,$sql);//执行sql语句 $num=mysqli_num_rows($result);//将执行完的结果用1或0表示 if($num)//如果已经存在该用户$num为1 { echo"<script>alert('username already exist');history.go(-1);</script>"; } else{ $sql_email="select email from user where email='$_POST[email]'"; $result=mysqli_query($link,$sql_email); $num=mysqli_num_rows($result); if($num){ echo"<script>alert('email already exist');history.go(-1);</script>"; }else{ $sql_insert="insert into user (username,password,email) values ('$_POST[username]','$psw','$_POST[email]')"; $res_insert=mysqli_query($link,$sql_insert);//执行sql语句将该用户的用户名、密码、邮箱存入数据库 echo"<script>alert('OK');</script>";//弹出OK对话框 echo "<script language='javascript'>window.location='login.html'</script>";//页面跳转 } } } else{ echo"<script>alert('captcha error!');history.go(-1);</script>"; } }}else{ echo"<script>alert('error!');history.go(-1);</script>";} |
接下来是登录页面
1 | < h1 >登录(Login)</ h1 > < form action = "login.php" method = "post" > < input type = "text" name = "username" class = "username" placeholder = "请输入您的用户名!" > < input type = "password" name = "password" class = "password" placeholder = "请输入您的用户密码!" > < img src = "picture.php" width = "" height = "" alt = "" style = "float:left; padding-top:27px;" /> < input type = "Captcha" class = "Captcha" name = "captcha" placeholder = "请输入验证码!" > < button type = "submit" class = "submit_button" value = "submit" name = "submit" >登录</ button > </ form > |
登录的PHP代码,login.php
1 | <?phpsession_start(); $capt = $_SESSION [ "captcha" ]; //if(isset($_POST["submit"]))//{ $user = $_POST["username"]; $psw = md5($_POST["password"]); if($user==""||$psw=="") { echo"<script>alert('please again!');history.go(-1);</script>"; } else { $link=mysqli_connect('localhost','root','0100','tz');//连接数据库 mysqli_select_db($link,"tz"); $sql="select username from user where username='$_POST[username]' and password='$psw'"; $result=mysqli_query($link,$sql); $num=mysqli_num_rows($result); if($num)//如果已经存在该用户 { $row = mysqli_fetch_array($result); $_SESSION["username"]= $row[0]; echo "<script language='javascript'>window.location='userinfo.php'</script>"; } else//不存在当前注册用户名称 { echo "<script>alert('error!2');history.go(-1);</script>"; } }/*}else{ echo"<script>alert('error!3');history.go(-1);</script>";}*/ |
登录功能也仅仅是匹配好数据的账号密码,然后输出“欢迎XXX”,没有实质性的内容。
但这仅仅是把用户的账号和密码存在数据库,我不懂登录注册的具体原理,在存入数据库之后,唯一可以显示的就是用户的名称,保存在session["username"]。因为有同学问我这样一个问题,让我对登录注册功能有一点认识。我感觉我做的这个登录注册功能仅仅是做了个路障而已,没有实际性的内容,希望懂的人可以给点提示
共同学习,写下你的评论
评论加载中...
作者其他优质文章