Login function (3)
The previous preparations and the login front-end page have been completed. Now we will introduce the login function code.
First create a login.php file to write our login php code.
The first step is to connect to the database first, otherwise the information in the database will not be obtained. The code is as follows :
<?php $link = mysqli_connect("localhost","root","root","joke");//注意后面的這幾個(gè)參數(shù),服務(wù)器名,數(shù)據(jù)庫(kù)的用戶名,密碼,數(shù)據(jù)庫(kù)名。密碼沒(méi)有可以不填 if (!$link) { die("連接失敗: " . mysqli_connect_error()); } ?>
The second step: Whether it is a user name or a password, we pass the value to the php program through the front-end form, and then verify it, so the next step is to first Only by getting the value passed by the front-end form can we verify whether it is right or wrong. The code to get the value is as follows:
<?php $username = $_POST['username']; $password = $_POST['password']; ?>
Why do we post instead of get? This requires taking a look at our front-end form and the information in the form tag:
<form action="login.php" method="post"> ... </form>
action refers to where the information is submitted and how the method is passed. Here we are the post method
Step 3: After we obtain the value, we need to verify whether the value is the same as the value in the database. They must be the same before you can log in. The verification code is as follows:
<?php $sql="select * from login where username = '{$username}' and password = '{$password}'";//先從數(shù)據(jù)庫(kù)中查詢戶名和密碼 $rs=mysqli_query($link,$sql); //執(zhí)行sql查詢 $row=mysqli_fetch_array($rs);//將查詢的結(jié)果放入變量$row中 ?>
Step 4: Query the result After that, we will start to verify:
<?php if($row) { if ($username == $row['username'] && $password == $row['password']) //判斷表單獲取的用戶名,密碼和數(shù)據(jù)庫(kù)中的是否一致 { echo "登陸成功,正在為你跳轉(zhuǎn)至后臺(tái)頁(yè)面"; header("location:index.html");//如果一致會(huì)跳轉(zhuǎn)到后臺(tái)的首頁(yè) } }else{ echo "賬號(hào)或密碼錯(cuò)誤" . "<br/>"; echo "<a href='login.html'>返回登陸頁(yè)面</a>";//如果不一致,將重新跳轉(zhuǎn)至登錄頁(yè)面重新登錄 } ?>
It seems like something is missing?
...
...
The verification code seems like Didn’t say...
Finally let’s talk about the verification code.
We need a program to generate verification codes. First, create a PHP file called passcode.php to write and generate verification codes.
Below I will write the detailed way of writing the verification code. Each key and difficult point will be commented, so that you can understand, pay attention, and read the code:
<?php //設(shè)置開(kāi)啟session session_start(); $image = imagecreatetruecolor(100, 30); //設(shè)置驗(yàn)證碼圖片大小的函數(shù) //設(shè)置驗(yàn)證碼顏色,用法:imagecolorallocate(int im, int red, int green, int blue); $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff //區(qū)域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區(qū)域著色,col 表示欲涂上的顏色 imagefill($image, 0, 0, $bgcolor); //設(shè)置變量 $captcha_code = ""; //生成隨機(jī)數(shù)字 for($i=0;$i<4;$i++){ //設(shè)置字體大小 $fontsize = 10; //設(shè)置字體顏色,隨機(jī)顏色 $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色 //設(shè)置數(shù)字 $fontcontent = rand(0,9); //10>.=連續(xù)定義變量 $captcha_code .= $fontcontent; //設(shè)置坐標(biāo) $x = ($i*100/4)+rand(5,10); $y = rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } //存到session $_SESSION['authcode'] = $captcha_code; //增加干擾元素,設(shè)置雪花點(diǎn) for($i=0;$i<200;$i++){ //設(shè)置點(diǎn)的顏色,50-200顏色比數(shù)字淺,不干擾閱讀 $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200)); //imagesetpixel — 畫(huà)一個(gè)單一像素 imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor); } //增加干擾元素,設(shè)置橫線 for($i=0;$i<4;$i++){ //設(shè)置線的顏色 $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設(shè)置線,兩點(diǎn)一線 imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor); } //設(shè)置頭部,image/png header('Content-Type: image/png'); //imagepng() 建立png圖形函數(shù) imagepng($image); //imagedestroy() 結(jié)束圖形函數(shù) 銷毀$image imagedestroy($image); ?>
If you want to understand each step in detail. To understand the function of a line of code, you can talk about the code changes (try to be very different from before so that it is obvious), and then see how the changes are different from before. In this way, you will know the function of this line of code.
The next step is to verify the verification code. Look at the code:
<?php //判斷驗(yàn)證碼是否填寫(xiě)并且是否正確 if(!$_POST['code']){ //首先判斷前端頁(yè)面是否有驗(yàn)證碼的值傳過(guò)來(lái),沒(méi)有就提示驗(yàn)證碼不能為空 echo('驗(yàn)證碼不能為空'); return; }else if($_POST['code']!=$_SESSION['authcode']){ //如果有,判斷是否正確 echo('驗(yàn)證碼不正確'); return; } ?>
Okay, this is our complete login function.