您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用PHP怎么實現(xiàn)一個注冊登錄系統(tǒng),內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
目錄結(jié)構(gòu)如下,其中function文件夾下包含兩個函數(shù)文件,uploads文件夾用于存放上傳的文件。
登錄頁面
index.html
<form action="login_process.php" method="POST"> 登錄 <input type="text" name="userName" size="20" maxlength="15" placeholder="請?zhí)顚懹脩裘坝蛎?quot;> <br> 注冊 <input type="password" name="password" size="20" maxlength="15"> <br> <input type="submit" value="登錄"> <input type="button" onclick="window.location.href='register.html'" value="注冊"> </form>
注冊頁面
register.html
<h3>用戶注冊登錄系統(tǒng)</h3> <hr> <form action="register.php" method="POST" enctype="multipart/form-data"> 用戶名: <input type="text" name="userName" size="20" maxlength="15" placeholder="必須填寫用戶名"> @ <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> 確認密碼: <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> 個人愛好: <input name="interests[]" type="checkbox" value="music">音樂 <input name="interests[]" type="checkbox" value="game">游戲 <input name="interests[]" type="checkbox" value="film">電影 <br> 個人相片 <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="請?zhí)顚憘渥⑿畔?quot;></textarea> <br> <input type="submit" name="submit" value="注冊"> <input type="reset" name="cancel" value="重填"> </form>
實現(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(); ?>
實現(xiàn)注冊功能
register.php
<?php include_once("function/fileSystem.php"); include_once("function/database.php"); if (empty($_POST)) { exit("您提交的表單數(shù)據(jù)超過post_max_size! <br>"); } // 判斷輸入密碼與確認密碼是否相同 $password = $_POST['password']; $confirmPassword = $_POST['confirmPassword']; if ($password != $confirmPassword) { exit("輸入的密碼與確認密碼不相等!"); } $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("用戶名已被占用,請更換其他用戶名"); } $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>"; } else { exit($message); } $userSQL = "select * from users where user_id = '$userID'"; $userResult = mysql_query($userSQL); if ($user = mysql_fetch_array($userResult)) { echo "您的注冊用戶名為:" . $user['userName']; } else { exit("用戶注冊失??!"); } closeConnect();
實現(xiàn)數(shù)據(jù)庫連接與關(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()); } } ?>
實現(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 "上傳超過upload_max_filesize"; case 2: return "上傳文件超過form的MAX_FILE_SIZE"; case 3: return "附件部分上傳"; case 4: return "沒有上傳"; } } ?>
php的框架:1、Laravel,Laravel是一款免費并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運行速度最快的一個PHP框架。3、Symfony,Symfony是一款為Web項目準備的PHP框架。4、Yii,Yii是一款快速、安全和專業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強大的PHP框架。
關(guān)于使用PHP怎么實現(xiàn)一個注冊登錄系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。