Home > Article > Backend Development > How to develop an online learning platform using PHP and CGI
How to use PHP and CGI to develop an online learning platform
Introduction:
In recent years, with the rapid development of the Internet, online learning platforms have become the first choice for many people to acquire knowledge. When developing an efficient and stable online learning platform, PHP and CGI technologies are widely used. This article will introduce how to use PHP and CGI to develop an online learning platform, and give corresponding code examples.
1. Preparation
Before you start, you need to prepare the following tools:
2. Use PHP to create a user login system
CREATE DATABASE user;
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL
);
<!DOCTYPE html> <html> <head> <title>用户注册</title> <style> label, input { display: block; margin-bottom: 10px; } </style> </head> <body> <form action="register.php" method="POST"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <input type="submit" value="注册"> </form> </body> </html>
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "user"; // 创建数据库连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取表单提交的用户名和密码 $username = $_POST['username']; $password = $_POST['password']; // 将用户名和加密后的密码插入到users表中 $sql = "INSERT INTO users (username, password) VALUES ('$username', md5('$password'))"; if ($conn->query($sql) === TRUE) { echo "注册成功!"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } // 关闭数据库连接 $conn->close(); ?>
<!DOCTYPE html> <html> <head> <title>用户登录</title> <style> label, input { display: block; margin-bottom: 10px; } </style> </head> <body> <form action="login.php" method="POST"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <input type="submit" value="登录"> </form> </body> </html>
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "user"; // 创建数据库连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取表单提交的用户名和密码 $username = $_POST['username']; $password = $_POST['password']; // 从users表中查询匹配的用户名和密码数据 $sql = "SELECT * FROM users WHERE username='$username' AND password=md5('$password')"; $result = $conn->query($sql); // 判断用户名和密码是否匹配 if ($result->num_rows > 0) { echo "登录成功!"; } else { echo "登录失败!"; } // 关闭数据库连接 $conn->close(); ?>
3. Use CGI to create a course management system
CREATE TABLE courses (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(30) NOT NULL,
teacher VARCHAR(30) NOT NULL
);
#!/usr/bin/perl use strict; use warnings; use CGI; # 创建CGI对象 my $cgi = CGI->new; # 创建数据库连接 my $servername = "localhost"; my $username = "root"; my $password = ""; my $dbname = "user"; # 检查连接是否成功 my $dbh = DBI->connect("dbi:mysql:$dbname", $username, $password) or die "无法连接到数据库: $DBI::errstr"; # 查询courses表中的数据 my $sth = $dbh->prepare("SELECT * FROM courses"); $sth->execute(); # 生成课程列表 my $html = ""; while (my @row = $sth->fetchrow_array) { $html .= "<li>$row[1] - $row[2]</li>"; } # 输出HTML页面 print $cgi->header(-charset=>'UTF-8'); print <<EOF; <!DOCTYPE html> <html> <head> <title>课程列表</title> </head> <body> <h1>课程列表</h1> <ul> $html </ul> </body> </html> EOF # 关闭数据库连接 $dbh->disconnect;
<Directory "/usr/local/apache2/cgi-bin"> AllowOverride None Options +ExecCGI AddHandler cgi-script .cgi .pl Require all granted </Directory>
IV. Summary
This article introduces how to use PHP and CGI An online learning platform is developed and corresponding code examples are given. By creating a user login system and course management system, we can build a fully functional online learning platform. I hope this article can help you better understand the application of PHP and CGI, and provide a reference for your project development.
Note: The above code examples are for demonstration purposes only, and more security and error handling measures are required in actual applications.
The above is the detailed content of How to develop an online learning platform using PHP and CGI. For more information, please follow other related articles on the PHP Chinese website!