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空間的內容,這個是怎麼做到的”,我感覺我做的這個登入註冊功能僅僅是做了個路障而已,沒有實際性的內容,希望懂的人可以給點提示。
相關文章
- Django登入(含隨機生成圖片驗證碼)註冊例項Django隨機
- 10.註冊和登入功能實現(3)—— 註冊資料寫入資料庫資料庫
- 登入介面:從資料庫中獲取資訊驗證登入(與註冊介面相聯絡)資料庫
- js登入與註冊驗證JS
- javaweb專案(1)連線資料庫,登入註冊JavaWeb資料庫
- vue登入註冊,帶token驗證Vue
- 不存資料庫生成驗證碼(totp演算法)資料庫演算法
- 隨機生成四個驗證碼隨機
- vuelidate 結合 Laravel 後端資料註冊驗證VueLaravel後端
- Laravel 6.x 為註冊或登入元件新增驗證碼Laravel元件
- swing 實現使用者登入註冊介面(不使用資料庫)資料庫
- php實現mysql資料庫隨機重排例子PHPMySql資料庫隨機
- python生成隨機圖形驗證碼Python隨機
- 隨機生成六位數驗證碼隨機
- python 隨機生成字串資料Python隨機字串
- 使用Oracle外部身份認證登入資料庫Oracle資料庫
- 登入驗證判斷,獲取後臺資料
- oracle 資料庫密碼中包括@Oracle資料庫密碼
- Python + Tkinter簡單實現註冊登入(連線本地MySQL資料庫)PythonMySql資料庫
- PHP資料寫入MySql資料庫PHPMySql資料庫
- Oracle資料庫密碼延遲驗證Oracle資料庫密碼
- 隨機驗證碼隨機
- Python PIL模組隨機生成中文驗證碼Python隨機
- javaWeb 簡單註冊登入(含資料庫連線) -- (四) 使用者登入請求與響應JavaWeb資料庫
- delphi 把圖片存入資料庫資料庫
- 建立catalog並註冊資料庫資料庫
- 【sqlserver】更改資料庫註冊的計算機名稱SQLServer資料庫計算機
- 簡單的登入註冊(前端+後端+MySQL資料庫 DRuid連線池 DBUtils)前端後端MySql資料庫UI
- Python隨機生成驗證碼的兩種方法Python隨機
- 關於板橋的使用者註冊系統資料庫的生成資料庫
- MySQL 隨機查詢資料與隨機更新資料實現程式碼MySql隨機
- 機器學習演算法的隨機資料生成機器學習演算法隨機
- 支援 Laravel/Lumen 下控制器註釋生成 API 文件,生成資料庫字典, 註冊路由呈現LaravelAPI資料庫路由
- php生成唯一隨機碼PHP隨機
- 資料庫的身份驗證方式資料庫
- TP5.1excel匯入資料庫的程式碼?php excel如何匯入資料庫?Excel資料庫PHP
- 9.Django之登陸註冊驗證登出Django
- Oracle新建資料庫並登入Oracle資料庫