本文實(shí)例為大家分享了PHP實(shí)現(xiàn)簡(jiǎn)單注冊(cè)登錄系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
目錄結(jié)構(gòu)如下,其中function文件夾下包含兩個(gè)函數(shù)文件,uploads文件夾用于存放上傳的文件。
注:博主使用的是php5,使用php7的小伙伴運(yùn)行報(bào)錯(cuò)的話有一部分原因是新的語(yǔ)法造成的,修改成新語(yǔ)法就可以了
html頁(yè)面
登錄頁(yè)面
index.html
form action="login_process.php" method="POST">
登錄
input type="text" name="userName" size="20" maxlength="15" placeholder="請(qǐng)?zhí)顚?xiě)用戶名及域名">
br>
注冊(cè)
input type="password" name="password" size="20" maxlength="15">
br>
input type="submit" value="登錄">
input type="button" onclick="window.location.href='register.html'" value="注冊(cè)">
/form>
注冊(cè)頁(yè)面
register.html
h2>用戶注冊(cè)登錄系統(tǒng)/h2>
hr>
form action="register.php" method="POST" enctype="multipart/form-data">
用戶名:
input type="text" name="userName" size="20" maxlength="15" placeholder="必須填寫(xiě)用戶名">
@
select name="domain" id="">
option value="@163.com" selected>163.com/option>
option value="@126.com">126.com/option>
/select>
br>
登錄密碼:
input type="password" name="password" size="20" maxlength="15">
br>
確認(rèn)密碼:
input type="password" name="confirmPassword" size="20" maxlength="15">
br>
選擇性別:
input type="radio" name="sex" value="male" checked>男
input type="radio" name="sex" value="female">女
br>
個(gè)人愛(ài)好:
input name="interests[]" type="checkbox" value="music">音樂(lè)
input name="interests[]" type="checkbox" value="game">游戲
input name="interests[]" type="checkbox" value="film">電影
br>
個(gè)人相片
input type="hidden" name="MAX_FILE_SIZE" value="1024">
input type="file" name="myPicture" size="25" maxlength="100">
br>
備注信息:
textarea name="remark" cols="30" rows="4" placeholder="請(qǐng)?zhí)顚?xiě)備注信息">/textarea>
br>
input type="submit" name="submit" value="注冊(cè)">
input type="reset" name="cancel" value="重填">
/form>
功能實(shí)現(xiàn)文件
實(shí)現(xiàn)登錄功能
login_process.php
?php
include_once("function/database.php");
// $userName = $_POST['userName'];
// $password = $_POST['password'];
$userName = addslashes($_POST['userName']);
$password = addslashes($_POST['password']);
getConnect();
$loginSQL = "select * from users where userName='$userName' and password='$password'";
echo $loginSQL;
$resultLogin = mysql_query($loginSQL);
if (mysql_num_rows($resultLogin) > 0) {
echo "登錄成功";
} else {
echo "登錄失敗";
}
closeConnect();
?>
實(shí)現(xiàn)注冊(cè)功能
register.php
?php
include_once("function/fileSystem.php");
include_once("function/database.php");
if (empty($_POST)) {
exit("您提交的表單數(shù)據(jù)超過(guò)post_max_size! br>");
}
// 判斷輸入密碼與確認(rèn)密碼是否相同
$password = $_POST['password'];
$confirmPassword = $_POST['confirmPassword'];
if ($password != $confirmPassword) {
exit("輸入的密碼與確認(rèn)密碼不相等!");
}
$userName = $_POST['userName'];
$domain = $_POST['domain'];
$userName = $userName . $domain;
// 判斷用戶名是否重復(fù)
$userNameSQL = "select * from users where userName = '$userName'";
getConnect();
$resultSet = mysql_query($userNameSQL);
if (mysql_num_rows($resultSet) > 0) {
exit("用戶名已被占用,請(qǐng)更換其他用戶名");
}
$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 == "沒(méi)有上傳") {
mysql_query($registerSQL);
$userID = mysql_insert_id();
echo "注冊(cè)成功br>";
} else {
exit($message);
}
$userSQL = "select * from users where user_id = '$userID'";
$userResult = mysql_query($userSQL);
if ($user = mysql_fetch_array($userResult)) {
echo "您的注冊(cè)用戶名為:" . $user['userName'];
} else {
exit("用戶注冊(cè)失??!");
}
closeConnect();
函數(shù)文件(function文件夾)
實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接與關(guān)閉的函數(shù)
database.php
?php
$databaseConnection = null;
function getConnect() {
$hosthome = "localhost";
$database = "register";
$userName = "root";
$password = "123456";
global $databaseConnection;
$databaseConnection = @mysql_connect($hosthome, $userName, $password) or die (mysql_error());
mysql_query("set names gbk");
@mysql_select_db($database, $databaseConnection) or die (mysql_error());
}
function closeConnect() {
global $databaseConnection;
if ($databaseConnection) {
@mysql_close($databaseConnection) or die (mysql_error());
}
}
?>
實(shí)現(xiàn)文件上傳的函數(shù)
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 "上傳超過(guò)upload_max_filesize";
case 2:
return "上傳文件超過(guò)form的MAX_FILE_SIZE";
case 3:
return "附件部分上傳";
case 4:
return "沒(méi)有上傳";
}
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 基于PHP的登錄和注冊(cè)的功能的實(shí)現(xiàn)
- 基于PHP實(shí)現(xiàn)用戶登錄注冊(cè)功能的詳細(xì)教程
- PHP實(shí)現(xiàn)的注冊(cè),登錄及查詢用戶資料功能API接口示例
- PHP實(shí)現(xiàn)的登錄,注冊(cè)及密碼修改功能分析
- php注冊(cè)和登錄界面的實(shí)現(xiàn)案例(推薦)
- PHP實(shí)現(xiàn)簡(jiǎn)單注冊(cè)登錄詳細(xì)代碼