teach you to write MySQL database user authentication system in PHP

Source: Internet
Author: User
Tags mysql mysql database

These two days by a friend entrusted, want me to help him write a user authentication system using MySQL database. I certainly not to shirk, had to spend a night of rest time, wrote a very simple PHP program.

The principle of user authentication is very simple: first, users need to fill in the page with user name and password, of course, unregistered users need to register first. Then call the database search for a corresponding user. If there is confirmation, do not remind the user to register first. Using PHP to do all of this is simple, but it is important to note that if you want to be able to identify the user in a future page, using PHP3 I can only think of a way to use cookies. To use the session, you can only wait for the PHP4 official version of the release!

The first step is to do a login page, here is not much to say. I've only made a very simple one and we can do it beautifully.

The second step begins the design of the confirmation program after login.

  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 请进!
;
}
?>

The third step of course is to do the registration of the page, also do not say more.

The fourth step is to register the identity confirmation and input database.

  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 请登录!;}
?>

The next step is the use of cookies, I intended to use cookies to make each page identify the user, but because the other page is not ready, I do not know what information to use. So there's only one very simple use, and here's a reference to PHP:

if(!$usercookie)
{header("非法用户");
}
?>
welcome.php:
require("cookie.php"); /*调用cookie.php*/
?>
echo $usercookie;
?>

Here we have completed a very simple user authentication system, of course, if you want to use it to build a database. Here is the structure of my database table, the name of the library is userinfo.

create table usertbl
(
ID int auto_increment primary key,
Name varchar(30),
Password varchar(20),
Lastlogin varchar(20)
);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.