PHP Experiment 3 PHP Object-Oriented Programming: Implementing user registration and user login

1. Experimental hours

2 hours

2. Experimental purpose

1. Master the method of customizing PHP functions;

2. Master the data input method of PHP program;

3. Master the redirection method in PHP Web;

4. Proficient in using PHP selection and loop structures;

5. Master object-oriented programming syntax

3. Experimental equipment

    PC computer, equipped with Win10 operating system, Word2019, PHPStudy+eclipse for php

4.Experimental content and procedures

( 1 ) Experimental steps

①New Project File—new—Local PHP Project

②Create a new php file, test-new-PHP File

③Enter the code and save it

④Verify the correctness of the code in the browser and output the running results

( 2 )Experimental content

[1] Implement user registration and user login functions (written as two functions to implement)

Front-end static page source code: index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="login.php" method="post">
			<table align="center">
				<tr>
					<td>学号:</td>
					<td><input type="text" name="num"></td>
				</tr>
				<tr>
					<td>姓名:</td>
					<td><input type="text" name="name"></td>
				</tr>
				<tr>
					<td>性别:</td>
					<td>
						<input type="radio" name="sex" value="男" checked>男
						<input type="radio" name="sex" value="女">女
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" name="zhuce" id="" value="注册">
						<input type="submit" name="ok" id="" value="登录">
						<input type="reset" name="" id="" value="重置">
					</td>
				</tr>
			</table>
		</form>
</body>
</html>

Form handler source code: login.php. The program contains two methods, register() and login()

Among them, the student number entered in the register() method uses a regular expression to determine whether it is 8 digits and the format is correct. The registration information is displayed, but the format is incorrect; the output is "Registration failed, the student number format is incorrect!"

<?php
require_once 'student.class.php';
function login(){
	if (isset($_POST['ok'])){
		$xh = $_POST['num'];
		$xm = $_POST['name'];
		$xb = $_POST['sex'];
		$stu=new student();
		$stu->set($xh,$xm,$xb);
		if ($stu->login()=='1'){
			echo '登陆成功!';
		}
		else {
			echo '登陆失败!';
		}
	}
}
function register(){
	if (isset($_POST['zhuce'])){
		$xh = $_POST['num'];
		$xm = $_POST['name'];
		$xb = $_POST['sex'];
		$checkxh=preg_match('/^\d{8}$/',$xh);//检查学号是否是8位数字
		$stu=new student();
		$stu->set($xh,$xm,$xb);
		if($checkxh){
			$stu->show($xh, $xm, $xb);
		echo '</br>';
		}
		else 
			echo "注册失败,学号格式不对!";
	}
}
login();
register();
?>

student.class.php for object-oriented programming

<?php
	class student{
		private $num;
		private $name;
		private $sex;
		public function show($xh,$xm,$xb){
			$this->num=$xh;
			$this->name=$xm;
			$this->sex=$xb;
			echo '学号:'.$this->num.'</br>';
			echo '姓名:'.$this->name.'</br>';
			echo '性别:'.$this->sex.'</br>';
		}
		public function set($xh,$xm,$xb){
			$this->num=$xh;
			$this->name=$xm;
			$this->sex=$xb;
		}
		public function login(){
			if ($_POST['name']=='admin'){
				return '1';
			}
		}
	}
?>

Run the resulting test:

  • Front-end page

  • When registering, when the entered student number is not 8 digits

  • When registering, the student number entered is 8 digits

  • When logging in, the name is admin, that is, the information entered is correct.

  • When logging in, inputting incorrect information

[2] The page outputs an 8-digit random string, and the randomly displayed content ranges from uppercase and lowercase characters plus numbers from 0 to 9.

<?php
	function randstring($n){
		$a=range('a', 'z');
		$b=range('A', 'Z');
		$c=range('0', '9');
		$d = array_merge($a,$b,$c);
		$str='';
		for ($i=1;$i<=$n;$i++){
			$str.=$d[rand(0,count($d)-1)];
		}
		return $str;
	}
	$str=randstring(8);
	echo $str;
?>

Guess you like

Origin blog.csdn.net/pzcxl/article/details/127311211