Login function ...LOGIN

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");//注意后面的这几个参数,服务器名,数据库的用户名,密码,数据库名。密码没有可以不填
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}'";//先从数据库中查询户名和密码
$rs=mysqli_query($link,$sql); //执行sql查询
$row=mysqli_fetch_array($rs);//将查询的结果放入变量$row中
?>

Step 4: Query the result After that, we will start to verify:

<?php
if($row) { 
     if ($username == $row['username'] && $password == $row['password']) //判断表单获取的用户名,密码和数据库中的是否一致
     { 
         echo "登陆成功,正在为你跳转至后台页面";
         header("location:index.html");//如果一致会跳转到后台的首页
     }
 }else{
     echo "账号或密码错误" . "<br/>";
     echo "<a href='login.html'>返回登陆页面</a>";//如果不一致,将重新跳转至登录页面重新登录
 }
?>

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
//设置开启session
session_start();
$image = imagecreatetruecolor(100, 30);    //设置验证码图片大小的函数
//设置验证码颜色,用法:imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色
imagefill($image, 0, 0, $bgcolor);
//设置变量
$captcha_code = "";
//生成随机数字
for($i=0;$i<4;$i++){
    //设置字体大小
    $fontsize = 10;
    //设置字体颜色,随机颜色
    $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));      //0-120深颜色
    //设置数字
    $fontcontent = rand(0,9);
    //10>.=连续定义变量
    $captcha_code .= $fontcontent;
    //设置坐标
    $x = ($i*100/4)+rand(5,10);
    $y = rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//存到session
$_SESSION['authcode'] = $captcha_code;
//增加干扰元素,设置雪花点
for($i=0;$i<200;$i++){
    //设置点的颜色,50-200颜色比数字浅,不干扰阅读
    $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
    //imagesetpixel — 画一个单一像素
    imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
}
//增加干扰元素,设置横线
for($i=0;$i<4;$i++){
    //设置线的颜色
    $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));
    //设置线,两点一线
    imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
}
//设置头部,image/png
header('Content-Type: image/png');
//imagepng() 建立png图形函数
imagepng($image);
//imagedestroy() 结束图形函数 销毁$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
//判断验证码是否填写并且是否正确
 if(!$_POST['code']){            //首先判断前端页面是否有验证码的值传过来,没有就提示验证码不能为空
     echo('验证码不能为空');
     return;
 }else if($_POST['code']!=$_SESSION['authcode']){   //如果有,判断是否正确
     echo('验证码不正确');
     return;
 }
?>

Okay, this is our complete login function.

Next Section
<?php session_start(); header("content-type:text/html;charset=utf-8"); //连接数据库 $link = mysqli_connect("localhost","root","root","joke"); if (!$link) { die("连接失败: " . mysqli_connect_error()); } $username =$_POST['username']; $password = $_POST['password']; //判断验证码是否填写并且是否正确 if(!$_POST['code']){ echo('验证码不能为空'); return; }else if($_POST['code']!=$_SESSION['authcode']){ echo('验证码不正确'); return; } $sql="select * from login where username = '{$username}' and password = '{$password}'"; $rs=mysqli_query($link,$sql); //执行sql查询 $row=mysqli_fetch_array($rs); if($row) { // 用户存在; if ($username == $row['username'] && $password == $row['password']) { //对密码进行判断。 echo "登陆成功,正在为你跳转至后台页面"; header("location:index.html"); } }else{ echo "账号或密码错误" . "<br/>"; echo "<a href='login.html'>返回登陆页面</a>"; } // 我这里没有数据库,所以右边会显示连接失败。 ?>
submitReset Code
ChapterCourseware