採用php與Mysql資料庫完成使用者註冊登入等操作
阿新 • • 發佈:2019-01-31
1、採用PHP與Mysql資料庫,完成使用者註冊程式,登錄檔單項可自由指定,儘量包含所有表單型別,註冊成功後在新頁面顯示註冊資訊,註冊中需要上傳的照片的操作功能使用自定義函式呼叫,並在註冊成功後顯示照片。
2、採用PHP與Mysql資料庫,完成使用者登入功能。
步驟如下:
(1)資料庫設計:
users.sql:
set default_storage_engine=InnoDB; set character_set_client = gbk ; set character_set_connection = gbk ; set character_set_database = gbk ; set character_set_results = gbk ; set character_set_server = gbk ; create database register; use register; create table users( user_id int primary key auto_increment, userName char(20) not null unique, password char(10) not null, sex char(10) not null, interests char(100), my_picture char(200), remark text );
(2)註冊介面:
register.htnl:
<!DOCTYPE html> <h2>使用者註冊系統</h2> <hr/> <form action="register.php" method="post" enctype="multipart/form-data"> 用 戶 名: <input type="text" name="userName" size="20" maxlength="15" value="必須填寫使用者名稱" /> @ <select name="domain"> <option value="@163.com" selected>163.com</option> <option value="@126.com">126.com</option> </select> <br/> <br/> 登入密碼: <input type="password" name="password" size="20" maxlength="15" /> <br/> <br/> 確認密碼: <input type="password" name="confirmPassword" size="20" maxlength="15" /> <br/> <br/> 選擇性別: <input name="sex" type="radio" value="男" checked />男 <input name="sex" type="radio" value="女" />女 <br/> <br/> 個人愛好: <input name="interests[]" type="checkbox" value="音樂" checked />音樂 <input name="interests[]" type="checkbox" value="遊戲" checked />遊戲 <input name="interests[]" type="checkbox" value="電影" />電影 <input name="interests[]" type="checkbox" value="籃球" />籃球 <br/> <br/> 個人相片: <input type="file" name="myPicture" size="25" maxlength="100" /> <br/> <br/> 備註資訊 : <textarea name="remark" cols="30" rows="4">請填寫備註資訊</textarea> <br/> <br/> <input type="submit" name="submit" value="註冊按鈕" /> <input type="reset" name="cancel" value="重新填寫" /> </form>
註冊操作程式:
register.php:
<?php include_once("functions/fileSystem.php"); include_once("functions/database.php"); if(empty($_POST)){ exit("您提交的表單資料超過post_max_size的配置!<br/>"); } $password = $_POST['password']; $confirmPassword = $_POST['confirmPassword']; if($password!=$confirmPassword){ exit("輸入的密碼和確認密碼不相等!"); } $userName = $_POST['userName']; $domain = $_POST['domain']; $userName = $userName.$domain; //判斷使用者名稱是否佔用 $userNameSQL = "select * from users where userName='$userName'"; getConnection(); $resultSet = mysql_query($userNameSQL); if(mysql_num_rows($resultSet)>0){ closeConnection(); exit("使用者名稱已經被佔用,請更換其他使用者名稱!"); } //收集使用者其他資訊 $sex = $_POST['sex']; if(empty($_POST['interests'])){ $interests = ""; }else{ $interests = implode(";",$_POST['interests']); } $remark = $_POST['remark']; $myPictureName = $_FILES['myPicture']['name']; //只有“檔案上傳成功”或“沒有上傳附件”時,才進行註冊 $registerSQL = "insert into users values(null,'$userName','$password','$sex', '$interests','$myPictureName','$remark')"; $message = upload($_FILES['myPicture'],"uploads"); if($message=="檔案上傳成功!"||$message=="沒有選擇上傳附件!"){ mysql_query($registerSQL); $userID = mysql_insert_id(); echo "使用者資訊成功註冊!<br/><br/><br/>"; }else{ exit($message); } //從資料庫中提取使用者註冊資訊 $userSQL = "select * from users where user_id=$userID"; $userResult = mysql_query($userSQL); if($user = mysql_fetch_array($userResult)){ echo "您註冊的使用者名稱為:".$user["userName"]."<br/><br/>"; echo "您填寫的登入密碼為:".$user["password"]."<br/><br/>"; echo "性別:".$user["sex"]."<br/><br/>"; echo "愛好:".$user["interests"]."<br/><br/>"; $pictureAdrees="uploads/".$myPictureName; echo "上傳的照片:"; echo '<img src="'.$pictureAdrees.'" width="200px">'; echo "<br/><br/>"; echo "備註資訊:".$user['remark']; }else{ exit("使用者資訊註冊失敗!"); } closeConnection(); ?>
fileSystem.php:
<?php
function upload($file,$filePath){
$error = $file['error'];
switch ($error){
case 0:
$fileName = $file['name'];
$fileTemp = $file['tmp_name'];
$destination = $filePath."/".$fileName;
move_uploaded_file($fileTemp,$destination);
return "檔案上傳成功!";
case 1:
return "上傳附件超過了php.ini中upload_max_filesize選項限制的值!";
case 2:
return "上傳附件的大小超過了form表單MAX_FILE_SIZE選項指定的值!";
case 3:
return "附件只有部分被上傳!";
case 4:
return "沒有選擇上傳附件!";
}
}
?>
database.php:
<?php
$databaseConnection = null;
function getConnection(){
$hostname = "localhost"; //資料庫伺服器主機名,可以用IP代替
$database = "register"; //資料庫名
$userName = "root"; //資料庫伺服器使用者名稱
$password = ""; //資料庫伺服器密碼
global $databaseConnection;
$databaseConnection = @mysql_connect($hostname, $userName, $password) or die (mysql_error()); //連線資料庫伺服器
mysql_query("set names 'gbk'"); //設定字符集
@mysql_select_db($database, $databaseConnection) or die(mysql_error());
}
function closeConnection(){
global $databaseConnection;
if($databaseConnection){
mysql_close($databaseConnection) or die(mysql_error());
}
}
?>
(3)登入介面:
login.html:
<h2>登入介面</h2>
<hr><br/><br/>
<form action="login_process.php" method="post">
用 戶 名:
<input type="text" name="userName" size="20" maxlength="15" value="請填寫使用者名稱及域名" />
<br/> <br/>
登入密碼:
<input type="password" name="password" size="20" maxlength="15" />
<br/><br/>
<input type="submit" value="登入" />
<input type="reset" value="重填" />
</form>
登入操作程式:
login.php:
<?php
include_once("functions/database.php");
//收集表單提交資料
$userName = addslashes($_POST['userName']);
$password = addslashes($_POST['password']);
//連線資料庫伺服器
getConnection();
//判斷使用者名稱和密碼是否輸入正確
$sql = "select * from users where userName='$userName' and password='$password'";
$resultSet = mysql_query($sql);
if(mysql_num_rows($resultSet)>0){
echo "使用者名稱和密碼輸入正確!登入成功!";
}else{
echo "使用者名稱和密碼輸入錯誤!登入失敗!";
}
closeConnection();
?>