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

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











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.

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.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.
