PHP註冊登入:包括資料存入資料庫,生成隨機驗證碼
自己學習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的程式碼:
<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" onclick="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裡面
<script>
function refresh() {
document.getElementById("codeP").src = "picture.php?tm="+Math.random();
}
</script>
如何生成隨機驗證碼呢?看看這個PHP程式碼,檔案picture.php
<?php session_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,上面的不小於200 for ($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
<?php
session_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>";
}
接下來是登入頁面
<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
<?php
session_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"]。因為有同學問我這樣一個問題,讓我對登入註冊功能很不解,就是“就好像你登入你的QQ可以檢視修改自己QQ空間的內容,這個是怎麼做到的”,我感覺我做的這個登入註冊功能僅僅是做了個路障而已,沒有實際性的內容,希望懂的人可以給點提示。