<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用戶登陸</title>
</head>
<!-- 定義輸入表單樣式 -->
<style type="text/css">
html{font-size: 12px;}
fieldset{width:200px;margin:0 auto;}
legend{font-weight: bold; font-size: 14px;}
label{float:left; width:70px; margin-left:10px;}
.left{margin-left:80px;}
.input{width:150px;}
span{color: #666666;}
</style>
<!-- 驗證表單是否輸入 -->
<script type="text/javascript">
function InputCheck(LoginForm){
if(LoginForm.username.value==''){
alert('請輸入用戶名!');
LoginForm.username.focus();
return (false);
}
if(LoginForm.password.value==''){
alert('請輸入密碼!');
LoginForm.password.focus();
return (false);
}
}
</script>
<body>
<div>
<fieldset>
<legend>用戶登陸</legend>
<form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
<p>
<lable for="username" class="lable">用戶名:</lable>
<input id="username" name="username" type="text" class="input" />
</p>
<p>
<lable for="password" class="lable">密 碼:</lable>
<input id="password" name="password" type="text" class="input" />
</p>
<p>
<input name="submit" value="提交" type="submit" />
<a href="reg.html">點此注冊</a>
</p>
</form>
</fieldset>
</div>
</body>
</html>
2、然后寫登陸源碼。
<?php
session_start();
//注銷登錄
function logout(){
if($_GET['action']='logout'){
unset($_SESSION['user_id']);
unset($_SESSION['username']);
echo '注銷登陸成功!點擊此處<a href="javascript:history.back(-1);">登陸</a>';
exit;
}
}
//登陸
if(!isset($_POST['submit'])){
exit('非法訪問!');
}
$username = htmlspecialchars($_POST['username']);
$password = md5($_POST['password']);
//引入數據庫文件
include('mysql.php');
//檢測用戶名密碼是否正確
$where = 'username="'.$username.'"';
if($rs=mysql_fetch_assoc(select('user',$where))){
//登陸成功
$_SESSION['name'] = $username;
$_SESSION['userid'] = $rs['user_id'];
echo $username,' 歡迎你!進入 <a href="my.php">用戶中心</a><br />';
echo '點擊此處 <a href="login.php?action=logout">注銷</a> 登錄!<br />';
exit;
}else {
exit('登錄失敗!點擊此處 <a href="javascript:history.back(-1);">返回</a> 重試');
}
3、注冊頁面表單
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用戶登陸</title>
</head>
<!-- 定義輸入表單樣式 -->
<style type="text/css">
html{font-size: 12px;}
fieldset{width:200px;margin:0 auto;}
legend{font-weight: bold; font-size: 14px;}
label{float:left; width:70px; margin-left:10px;}
.left{margin-left:80px;}
.input{width:150px;}
span{color: #666666;}
</style>
<!-- 驗證表單是否輸入 -->
<script type="text/javascript">
function InputCheck(LoginForm){
if(LoginForm.username.value==''){
alert('請輸入用戶名!');
LoginForm.username.focus();
return (false);
}
if(LoginForm.password.value==''){
alert('請輸入密碼!');
LoginForm.password.focus();
return (false);
}
if(LoginForm.email.value==''){
alert('請輸入郵箱');
LoginForm.email.focus();
return (false);
}
}
</script>
<body>
<div>
<fieldset>
<legend>用戶登陸</legend>
<form name="LoginForm" method="post" action="reg.php" onSubmit="return InputCheck(this)">
<p>
<lable for="username" class="lable">用戶名:</lable>
<input id="username" name="username" type="text" class="input" />
</p>
<p>
<lable for="password" class="lable">密 碼:</lable>
<input id="password" name="password" type="text" class="input" />
</p>
<p>
<lable for="password" class="lable">郵 箱:</lable>
<input id="email" name="email" type="text" class="input" />
</p>
<p>
<input name="submit" value="注冊" type="submit" />
</p>
</form>
</fieldset>
</div>
</body>
</html>
4、接收注冊表單。
<?php
if(!isset($_POST['submit'])){
exit('非法操作!');
}
$username = strtolower($_POST['username']);
$password = strtolower($_POST['password']);
$email = strtolower($_POST['email']);
//注冊信息判斷
// if(!preg_match('/^[\w\x80-\xff]{3-15}$/',$username)){
// exit('錯誤:用戶名不符合規定。<a href="javascript:history.back(-1);">返回</a>');
// }
if(strlen($password)<6){
exit('錯誤:密碼長度太短。<a href="javascript:history.back(-1);">返回</a>');
}
// if(!preg_match('/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/',$email)){
// exit('錯誤:電子郵件格式有誤。<a href="javascript:history.back(-1);">返回</a>');
// }
//引入數據庫操作文件
include('mysql.php');
//檢測用戶名是否存在
$where = 'username="'.$username.'"';
if(mysql_fetch_assoc(select('user',$where))){
echo '錯誤:用戶名',$usename,'已存在.<a href="javascript:history.back(-1);">返回</a>';
exit();
}
//插入數據
$password = md5($password);
$data['username'] = $username;
$data['password'] = $password;
$data['email'] = $_POST['email'];
$re = insert('user',$data);
if($re){
exit('用戶注冊成功!點此登錄<a href=" login.html">登陸</a>');
}else{
echo ' 添加數據失敗:',mysql_error(),'<br>';
echo '點擊此處<a href="javascript:history.back(-1);">返回</a>重試';
}
5、驗證頁面是否已經登錄
<?php
session_start();
//檢測是否登錄,若沒登錄則轉向登錄界面
if(!isset($_SESSION['userid'])){
header("Location:login.html");
exit();
}
//包含數據庫連接文件
include('conn.php');
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];
$user_query = mysql_query("select * from user where uid=$userid limit 1");
$row = mysql_fetch_array($user_query);
echo '用戶信息:<br />';
echo '用戶ID:',$userid,'<br />';
echo '用戶名:',$username,'<br />';
echo '郵箱:',$row['email'],'<br />';
echo '注冊日期:',date("Y-m-d", $row['regdate']),'<br />';
echo '<a href="login.php?action=logout">注銷</a> 登錄<br />';
6、操作數據庫函數。
<?php
$db_host = "localhost:3306";
// database name
$db_name = "test";
// database username
$db_user = "root";
// database password
$db_pass = "";
$con = mysql_connect($db_host,$db_user,$db_pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $con);
// some code
#mysql_close($con);
function select($table,$where='',$order='',$limits='',$field=''){
$field=($field==null)?'*':$field;
$sele.="from ".$table;
$where=($where==null)?'':' where '.$where;
$order=($order==null)?'':' order by '.$order;
$limits=($limits==null)?'':' limit '.$limits;
$sele="select ".$field." from ".$table.$where.$order.$limits;
//var_dump($sele);
return mysql_query($sele);
mysql_close($con);
}
//插入,二個參數都是必須,后為數組
function insert($table,$columns)
{
foreach($columns as $key=>$values)
{
$addkey.='`'.$key.'`,';
if(is_numeric($values))
{
$addvalues.=$values.',';
}
else
{
$values=mysql_real_escape_string($values);
$addvalues.='\''.$values.'\',';
}
}
$addkey=rtrim($addkey,',');
$addvalues=rtrim($addvalues,',');
$inse.='INSERT INTO '.$table.'('.$addkey.') VALUES ('.$addvalues.');';
return mysql_query($inse);
}
//更新,三個參數都是必須的
function update($table,$data,$where)
{
foreach($data as $k=>$v)
{
if(is_numeric($v))
{
$edit_data.='`'.$k.'`'.'='.$v.',';
}
else
{
$edit_data.='`'.$k.'`'.'='."'".mysql_real_escape_string($v)."',";
}
}
$edit_data=rtrim($edit_data,',');
$upda="UPDATE ".$table." SET ".$edit_data." WHERE ".$where;
return mysql_query($upda);
}
//刪除
function delete($table,$where)
{ $dele="DELETE FROM ".$table." WHERE ".$where;
return mysql_query($dele);
}
?>