首先创建一个数据库和表,代码如下:

CREATE DATA  `dbtest` ;
CREATE TABLE `dbtest`.`users` (
`user_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 35 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
UNIQUE (`email`)
) ENGINE = MYISAM ;

你可以复制上面的代码到phpmyqdmin来创建该数据库和表。

 

用户注册代码:

Dbconnect.php  包含本地主机连接,数据库选择的代码。

<?php
if(!mysql_connect(\"localhost\",\"root\",\"\"))
{
     die(\'oops connection problem ! --> \'.mysql_error());
}
if(!mysql_select_db(\"dbtest\"))
{
     die(\'oops data  selection problem ! --> \'.mysql_error());
}
?>

 

Register.php 包含了简单的HTML表单和PHP代码

<?php
session_start();
if(isset($_SESSION[\'user\'])!=\"\")
{
 header(\"Location: home.php\");
}
include_once \'dbconnect.php\';

if(isset($_POST[\'btn-signup\']))
{
 $uname = mysql_real_escape_string($_POST[\'uname\']);
 $email = mysql_real_escape_string($_POST[\'email\']);
 $upass = md5(mysql_real_escape_string($_POST[\'pass\']));
 
 if(mysql_query(\"INSERT INTO users(username,email,password) VALUES(\'$uname\',\'$email\',\'$upass\')\"))
 {
  ?>
        < >alert(\'successfully registered \');</ >
        <?php
 }
 else
 {
  ?>
        < >alert(\'error while registering you...\');</ >
        <?php
 }
}
?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html  ns=\"http://www.w3.org/1999/xhtml\">
<head>
<  http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
< >Login & Registration System</ >
<  rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />

</head>
<body>
<center>
<div id=\"login-form\">
<form method=\"post\">
<table align=\"center\" width=\"30%\" border=\"0\">
<tr>
<td><input type=\"text\" name=\"uname\" placeholder=\"User Name\" required /></td>
</tr>
<tr>
<td><input type=\"email\" name=\"email\" placeholder=\"Your Email\" required /></td>
</tr>
<tr>
<td><input type=\"password\" name=\"pass\" placeholder=\"Your Password\" required /></td>
</tr>
<tr>
<td><button type=\"submit\" name=\"btn-signup\">Sign Me Up</button></td>
</tr>
<tr>
<td><a href=\"index.php\">Sign In Here</a></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

注意:我们应该在所有的页面开启session会话(session_start)。

现在,成功创建注册页面,接下来创建用户登录的页面。

<##ads_in_article_manong##>

用户登录代码:

Index.php 登录页面

<?php
session_start();
include_once \'dbconnect.php\';

if(isset($_SESSION[\'user\'])!=\"\")
{
 header(\"Location: home.php\");
}
if(isset($_POST[\'btn-login\']))
{
 $email = mysql_real_escape_string($_POST[\'email\']);
 $upass = mysql_real_escape_string($_POST[\'pass\']);
 $res=mysql_query(\"SELECT * FROM users WHERE email=\'$email\'\");
 $row=mysql_fetch_array($res);
 if($row[\'password\']==md5($upass))
 {
  $_SESSION[\'user\'] = $row[\'user_id\'];
  header(\"Location: home.php\");
 }
 else
 {
  ?>
        < >alert(\'wrong details\');</ >
        <?php
 }
 
}
?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html  ns=\"http://www.w3.org/1999/xhtml\">
<head>
<  http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
< >cleartuts - Login & Registration System</ >
<  rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />
</head>
<body>
<center>
<div id=\"login-form\">
<form method=\"post\">
<table align=\"center\" width=\"30%\" border=\"0\">
<tr>
<td><input type=\"text\" name=\"email\" placeholder=\"Your Email\" required /></td>
</tr>
<tr>
<td><input type=\"password\" name=\"pass\" placeholder=\"Your Password\" required /></td>
</tr>
<tr>
<td><button type=\"submit\" name=\"btn-login\">Sign In</button></td>
</tr>
<tr>
<td><a href=\"register.php\">Sign Up Here</a></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

该php文件包含了一个HTML表单,表单用来提交用户登录的信息,还包含了处理登录信息的PHP脚本,php脚本介绍表单的用户名和密码数据,然后在数据库中查询是否存在用户名和密码,如果存在,登录成功,否则提示用户名和密码错误。

 

home.php 登录成功后跳转到的页面

<?php
session_start();
include_once \'dbconnect.php\';

if(!isset($_SESSION[\'user\']))
{
 header(\"Location: index.php\");
}
$res=mysql_query(\"SELECT * FROM users WHERE user_id=\".$_SESSION[\'user\']);
$userRow=mysql_fetch_array($res);
?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html  ns=\"http://www.w3.org/1999/xhtml\">
<head>
<  http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
< >Welcome - <?php echo $userRow[\'email\']; ?></ >
<  rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />
</head>
<body>
<div id=\"header\">
 <div id=\"left\">
    <label>cleartuts</label>
    </div>
    <div id=\"right\">
     <div id=\"content\">
         hi\' <?php echo $userRow[\'username\']; ?>&nbsp;<a href=\"logout.php?logout\">Sign Out</a>
        </div>
    </div>
</div>
</body>
</html>

 

logout.php 用户注销(退出登录)页面:

<?php
session_start();

if(!isset($_SESSION[\'user\']))
{
 header(\"Location: index.php\");
}
else if(isset($_SESSION[\'user\'])!=\"\")
{
 header(\"Location: home.php\");
}

if(isset($_GET[\'logout\']))
{
 session_destroy();
 unset($_SESSION[\'user\']);
 header(\"Location: index.php\");
}
?>

我希望这篇文章帮助您了解用户登录和使用PHP和MySQL注册。

收藏 打印