关于MySQL数据库的用户认证系统分析
用户认证的原理很简单:首先需要用户在页面上填入用户名和密码,当然没注册的用户需要先注册。然后调用数据库搜索是否有相应的用户。 如果有就确认,没有则提醒用户先注册。使用PHP来完成这一切很简单,但需要注意的是如果想在以后的页面中都能确认用户身份
用户认证的原理很简单:首先需要用户在页面上填入用户名和密码,当然没注册的用户需要先注册。然后调用数据库搜索是否有相应的用户。
如果有就确认,没有则提醒用户先注册。使用PHP来完成这一切很简单,但需要注意的是如果想在以后的页面中都能确认用户身份,使用PHP3我只能想出使用cookie的方法。要想使用session,就只能等待PHP4正式版的发布了!
第一步是做一个登录的页面,这儿就不多讲了。我只做了个极简单的,大家自己做得漂亮点。
第二步开始登录后的确认程序的设计。
login.php: mysql_connect("localhost","user","password") /*连接数据库,用户名和密码自行修改*/ or die("无法连接数据库,请重试"); mysql_select_db("userinfo") or die("无法选择数据库,请重试"); $today=date("Y-m-d H:i:s"); $query=" select id from usertbl where name=$name and password=$password /*从数据库中搜索和登录用户相应的资料*/ "; $result=mysql_query($query); $numrows=mysql_num_rows($result); if($numrows==0){ /*验证是否能找出相同资料的用户,不能则未注册*/ echo 非法用户 ; echo 请注册先 ; echo 重试 ; } else{ $row=mysql_fetch_array($result); $id=$row[0]; $query=" update usertbl set lastlogin=$today where id=$id"; $result=mysql_query($query); SetCookie("usercookie", "欢迎你,$name"); /*这里使用了cookie,以方便之后的页面认证。 但我未开发完这一块。希望有兴趣的朋友指正*/ echo 登录成功 ; echo 请进! ; } ?> Copy after login |
第三步当然是做好注册的页面,也不多讲了。
第四步是注册后的身份确认和输入数据库。
register.php: mysql_connect("localhost","user","password") /*请修改用户名和密码*/ or die("无法连接数据库,请重试"); mysql_select_db("userinfo") or die("无法选择数据库,请重试"); $query="select id from usertbl where name=$name\"; /*从数据库中搜索相同名字的用户资料*/ $result=mysql_query($query); $numrows=mysql_num_rows($result); if($numrows!=0) /*找到了当然就是有人先注册了相同的名字*/ {echo 已有人注册此名,请重新选择名字!;} else {$query="insert into usertbl values(0,$name,$password,\)"; /*找不到相同的就输入新的用户资料*/ mysql_query($query); echo 注册成功; echo 请登录!;} ?> Copy after login |
下一步是cookie的使用,我原打算使用cookie来使每一页都能识别用户身份,但由于别的页面还没做好,不知道需要用到哪些资料。于是就只有一个很简单的使用,这里用到了php的引用:
if(!$usercookie) {header("非法用户"); } ?> welcome.php: require("cookie.php"); /*调用cookie.php*/ ?> echo $usercookie; ?> Copy after login |
到这儿便完成了一个很简单的用户认证系统,当然如果你要使用它还得建好数据库.下面是我的数据库表的结构,库的名字是userinfo.
create table usertbl ( ID int auto_increment primary key, Name varchar(30), Password varchar(20), Lastlogin varchar(20) ); Copy after login |
(责任编辑:铭铭 mingming_ky#126.com TEL:(010)-68476636)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.
