用PHP编写登陆界面
2021-07-16 20:06
标签:images username etc 数据库访问 set session http start sub 网页的编写用PHP最方便。用php做了最简单的用户登录、创建的程序。 一、 MySQL的设计 MySQL设计了两个表:members和sex。两张表的创建语句分别是: 后有增加了几个字段: 第二张表的创建语句: 两张表的结构如下: 二、php页面的编写 1 index.html Index.html是用户的登录页面主要是html的编写: 具体的页面如下: 2 用户登录login.php 用户填写了用户名和密码后的登录和认证程序如下: 登录后的页面如下: 3 mysql的连接程序 在2中有一段conn.php,是与mysql的连接程序。具体内容如下: 4 用户页面my.php 用户登录后,可以显示自己的个人主页,其具体内容如下: 页面显示如下内容: 5 用户注册页面 用户的注册页面和登录页面类似,是html的,具体内容如下: 页面显示如下: 6 用户注册程序 用户注册程序内容如下: 具体的页面显示如下: 三、总结 用php+mysql实现用户认证的程序相当比较简单。但程序中需要主要session的控制。在需要保持session或unset session时,都需要session_start()。 总共6个程序组成了这个登录的逻辑: 用PHP编写登陆界面 标签:images username etc 数据库访问 set session http start sub 原文地址:http://www.cnblogs.com/hengwei/p/7067929.htmlcreate table members (id int(4) NOT NULL auto_increment,
username varchar(65) NOT NULL default ‘‘,
password varchar(65) NOT NULL default ‘‘,
PRIMARY KEY (id));
alter table members add firstname varchar(65);
alter table members add lastname varchar(65);
alter table members add email varchar(65);
alter table members add age varchar(65);
alter table members add sex_id int;
alter table members modify sex_id int(4);
alter table members modify age int(4);
create table sex (sex_id int primary key not null, sex varchar(20));
mysql> desc members;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| username | varchar(65) | NO | | | |
| password | varchar(65) | NO | | | |
| firstname | varchar(65) | YES | | NULL | |
| lastname | varchar(65) | YES | | NULL | |
| email | varchar(65) | YES | | NULL | |
| age | int(4) | YES | | NULL | |
| sex_id | int(4) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> desc sex;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sex_id | int(11) | NO | PRI | NULL | |
| sex | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
form name="form1" method="post" action="login.php">
用户登录br>
Username:
input name="username" type="text" id="username">br>
Password:
input name="password" type="password" id="password">br>
input name="submit" type="submit" value="Login">
form>
form name="form2" method="post" action="reg.html">
用户注册br>
input name="reg" type="submit" value="Reg">
form>
php
include(‘conn.php‘);
$username = $_POST[‘username‘];
$password = $_POST[‘password‘];
$sql = "select * from members where username = ‘$username‘ and password = MD5(‘$password‘)";
$user_query = mysql_query($sql,$conn) or die(‘mysql query error‘);
$rows=mysql_num_rows($user_query);
//print_r(mysql_fetch_row($user_query));
if($_GET[‘action‘] == "logout"){
session_start();
unset($_SESSION[‘username‘]);
//session_unset(‘username‘);
//session_destroy(‘username‘); echo ‘注销登录成功!‘;
echo $_SESSION[‘username‘],$username;
exit;
}
if ($rows == 1){
session_start();
$_SESSION[‘username‘] = $username;
echo $username,‘ 欢迎你!进入 用户中心
‘;
echo ‘点击此处 注销 登>录!
‘;
exit;
} else {
echo ‘用户名密码错误,点击此处 login 登录!
‘;
}
?>php
$mysrv="localhost"; //数据库服务器名称
$myuser="root"; // 连接数据库用户名
$mypwd=""; // 连接数据库密码
$mydb=‘hengwei‘;
$conn = mysql_connect($mysrv,$myuser,$mypwd) or die("数据库链接错
误".mysql_error());
mysql_select_db($mydb,$conn) or die("数据库访问错误".mysql_error());
?>
php
session_start();
//检测是否登录,若没登录则转向登录界面
if(!isset($_SESSION[‘username‘])){
echo ‘您还没有登录,请登录‘,‘
‘,‘login 登录
‘;
exit();
}
$username = $_SESSION[‘username‘];
include(‘conn.php‘);
$regsql = "select * from members join sex on username=‘$username‘ and members.sex_id=sex.sex_id";
$user_query = mysql_query($regsql,$conn) or die(‘mysql query error‘);
while($row = mysql_fetch_array($user_query)){
echo ‘用户名:‘,$row[username],‘
‘;
echo ‘名:‘,$row[firstname],‘
‘;
echo ‘姓:‘,$row[lastname],‘
‘;
echo ‘年龄:‘,$row[age],‘
‘;
echo ‘Email:‘,$row[email],‘
‘;
echo ‘性别:‘,$row[sex],‘
‘;
}
echo ‘注销 登录
‘;
?>form name = "reg1" method = "post" action = "reg.php">
用户注册br>
Username:
input name="reguser" type="text" id="reguser">br>
Password:
input name="regpwd" type="password" id="regpwd">br>
First Name:
input name="regfn" type="text" id="regfn">br>
Last Name:
input name="regln" type="text" id="regln">br>
Email:
input name="regemail" type="text" id="regemail">br>
Age:
input name="regage" type="text" id="regage">br>
Sex
input name="regsex" type="text" id="regsex">br>
input name="OK" type="submit" value="OK">
form>
php
include(‘conn.php‘);
$reguser = $_POST[‘reguser‘];
$regpwd = $_POST[‘regpwd‘];
$regfn = $_POST[‘regfn‘];
$regln = $_POST[‘regln‘];
$regemail = $_POST[‘regemail‘];
$regage = $_POST[‘regage‘];
$regsex = $_POST[‘regsex‘];
$regsql = "insert into members (username,password,firstname,lastname,email,age,sex_id) values (‘$reguser‘,MD5(‘$regpwd‘),‘$regfn‘,‘$regln‘,‘$regemail‘,‘$regage‘,(select sex_id from sex where sex=‘$regsex‘))";
$user_query = mysql_query($regsql,$conn) or die(‘mysql query error‘);
echo $reguser,"注册成功","
";
echo ‘返回登录页面login‘;
mysql_close($conn);
?>[root@hwwaf02 au]# ll
total 24
-rw-r--r--. 1 root root 323 Jun 23 00:17 conn.php
-rw-r--r--. 1 root root 385 Jun 20 14:09 index.html
-rw-r--r--. 1 root root 963 Jun 23 00:13 login.php
-rw-r--r--. 1 root root 857 Jun 22 23:38 my.php
-rw-r--r--. 1 root root 543 Jun 22 23:16 reg.html
-rw-r--r--. 1 root root 630 Jun 22 23:42 reg.php