php實現(xiàn)登錄注冊的時候,數(shù)據(jù)庫部分不會弄,請大神幫忙解決問題,謝謝!已經(jīng)創(chuàng)建了test表。下面我貼上代碼:第一部分:(login.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>用戶登錄</title>
<style type="text/css">
button,input{padding:0px;}
html{font-size:12px;}
fieldset{width:300px; margin: 0 auto;}
legend{font-weight:bold; font-size:14px;}
.label{float:left; width:70px; margin-left:10px;}
.left{margin-left:80px; width: 65px;}
.input{width:150px;}
span{color: #666666;}
.right a{
text-decoration: none;
color:#000;
display: block;
width:100%;
height: 100%;
position: relative;
z-index: 999;
}
.right{
width: 65px;
margin-left:20px;
}
</style>
<script language=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>
</head>
<body>
<div>
<fieldset>
<legend>用戶登錄</legend>
<form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
<p>
<label for="username" class="label">用戶名:</label>
<input id="username" name="username" type="text" class="input" />
<p/>
<p>
<label for="password" class="label">密 碼:</label>
<input id="password" name="password" type="password" class="input" />
<p/>
<p>
<input type="submit" name="submit" value=" 確 定 " class="left" />
<button name="button" value=" 注冊 " class="right" /><a href = "reg.html">注冊</a></button>
</p>
</form>
</fieldset>
</div>
</body>
</html>
第二部分:(login.php):
<?php
session_start();
//注銷登錄
if($_GET['action'] == "logout"){
unset($_SESSION['userid']);
unset($_SESSION['username']);
echo '注銷登錄成功!點擊此處 <a href="login.html">登錄</a>';
exit;
}
//登錄
if(!isset($_POST['submit'])){
exit('非法訪問!');
}
$username = htmlspecialchars($_POST['username']);
$password = MD5($_POST['password']);
//包含數(shù)據(jù)庫連接文件
include('conn.php');
//檢測用戶名及密碼是否正確
$check_query = mysql_query("select uid from user where username='$username' and password='$password' limit 1");
if($result = mysql_fetch_array($check_query)){
//登錄成功
$_SESSION['username'] = $username;
$_SESSION['userid'] = $result['uid'];
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> 重試');
}
?>
第三部分:(reg.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>用戶注冊</title>
<style type="text/css">
button,input{padding:0px;}
html{font-size:12px;}
fieldset{width:520px; margin: 0 auto;}
legend{font-weight:bold; font-size:14px;}
label{float:left; width:70px; margin-left:10px;}
.left{margin-left:80px; width:90px;}
.input{width:150px;}
span{color: #666666;}
.right a{
text-decoration: none;
color:#000;
display: block;
width:100%;
height: 100%;
position: relative;
z-index:999;
}
.right{
width:90px;
margin-left:20px;
}
</style>
<script language=JavaScript>
<!--
function InputCheck(RegForm)
{
if (RegForm.username.value == "")
{
alert("用戶名不可為空!");
RegForm.username.focus();
return (false);
}
if (RegForm.password.value == "")
{
alert("必須設定登陸密碼!");
RegForm.password.focus();
return (false);
}
if (RegForm.repass.value != RegForm.password.value)
{
alert("兩次密碼不一致!");
RegForm.repass.focus();
return (false);
}
if (RegForm.email.value == "")
{
alert("電子郵箱不可為空!");
RegForm.email.focus();
return (false);
}
}
//-->
</script>
</head>
<div>
<fieldset>
<legend>用戶注冊</legend>
<form name="RegForm" method="post" action="reg.php" onSubmit="return InputCheck(this)">
<p>
<label for="username" class="label">用戶名:</label>
<input id="username" name="username" type="text" class="input" />
<span>(必填,3-15字符長度,支持漢字、字母、數(shù)字及_)</span>
<p/>
<p>
<label for="password" class="label">密 碼:</label>
<input id="password" name="password" type="password" class="input" />
<span>(必填,不得少于6位)</span>
<p/>
<p>
<label for="repass" class="label">重復密碼:</label>
<input id="repass" name="repass" type="password" class="input" />
<p/>
<p>
<label for="email" class="label">電子郵箱:</label>
<input id="email" name="email" type="text" class="input" />
<span>(必填)</span>
<p/>
<p>
<input type="submit" name="submit" value=" 提交注冊 " class="left" />
<button name = "button" class = "right"><a href="login.html">返回登錄</a></button>
</p>
</form>
</fieldset>
</div>
<body>
</body>
</html>
第四部分:(reg.php):
<?php
if(!isset($_POST['submit'])){
exit('非法訪問!');
}
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
//注冊信息判斷
if(!preg_match('/^[\w\x80-\xff]{3,15}$/', $username)){
exit('錯誤:用戶名不符合規(guī)定。<a href="javascript:history.back(-1);">返回</a>');
}
if(strlen($password) < 6){
exit('錯誤:密碼長度不符合規(guī)定。<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>');
}
//包含數(shù)據(jù)庫連接文件
include('conn.php');
//檢測用戶名是否已經(jīng)存在
$check_query = mysql_query("select uid from user where username='$username' limit 1");
if(mysql_fetch_array($check_query)){
echo '錯誤:用戶名 ',$username,' 已存在。<a href="javascript:history.back(-1);">返回</a>';
exit;
}
//寫入數(shù)據(jù)
$password = MD5($password);
$regdate = time();
$sql = "INSERT INTO user(username,password,email,regdate)VALUES('$username','$password','$email',$regdate)";
if(mysql_query($sql,$conn)){
exit('用戶注冊成功!點擊此處 <a href="login.html">登錄</a>');
} else {
echo '抱歉!添加數(shù)據(jù)失?。?,mysql_error(),'<br />';
echo '點擊此處 <a href="javascript:history.back(-1);">返回</a> 重試';
}
?>
第五部分:(my.php):
<?php
session_start();
//檢測是否登錄,若沒登錄則轉向登錄界面
if(!isset($_SESSION['userid'])){
header("Location:login.html");
exit();
}
//包含數(shù)據(jù)庫連接文件
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 />';
?>
最關鍵的一部分 :(conn.php):
<?php
/*****************************
*數(shù)據(jù)庫連接
*****************************/
$conn = @mysql_connect("112.74.164.248","root","root");
if (!$conn){
die("連接數(shù)據(jù)庫失?。? . mysql_error());
}
mysql_select_db("test", $conn);
//字符轉換,讀庫
mysql_query("set character set 'gbk'");
//寫庫
mysql_query("set names 'gbk'");
?>
添加回答
舉報
0/150
提交
取消