php+mysql+html+css實現簡單的登錄注冊功能


本文目錄


前言:
暑假作業留到現在,emmm,最后那段時間是懶了很多。開學事情比較煩,各種麻煩。這個作業進行總結一下。

准備工作

利用phpstudy集成環境進行實現,看下效果
m0re
m0re
將CSS寫在一個文件夾中,進行調用比較方便。
另外我之前收藏過這樣的一個404界面的源碼,覺得還不錯,就加上了。我將它替換那個忘記密碼的功能,因為界面簡單,(我也沒去學習這個模塊,復雜,懶的去看了)所以直接替換了。
m0re

正題

注冊功能的頁面,注冊時,需要進行的是連接數據庫,然后執行SQL語句,插入數據到指定的表中。

<?php
header('content-type:text/html;charset=utf-8');

$conn = mysqli_connect('localhost', 'root', 'root', "m0re");      

if (mysqli_connect_errno($conn)) {
    echo mysqli_connect_error($conn);
}
if(isset($_POST['submit'])){
	$sql = "insert into m0re(username,password,register_time) values('{$_POST['username']}',md5('{$_POST['password']}'),now())";
	$query=$conn->query($sql);
	if($query){
	//echo "success";
}
else{
	echo $conn->error;
}
}
mysqli_close($conn);
?>

我寫的時候,那個限制功能都在HTML中,所以PHP沒有太多的設置。不過還是寫一下PHP的代碼(我的代碼里面是沒有這些的,因為寫進去會出錯,因為我的HTML代碼的原因,自己寫的簡單的登錄狂就可以使用)

<?php 
if(empty($_POST['username'])){
	skip('register.php', '', '用戶名不得為空!');
}
if(mb_strlen($_POST['username'])>32){
	skip('register.php', '', '用戶名長度不要超過32個字符!');
}
if(mb_strlen($_POST['password'])<6){
	skip('register.php', '','密碼不得少於6位!');
}
if($_POST['password']!=$_POST['confirm_pw']){
	skip('register.php', '','兩次密碼輸入不一致!');
}
if(strtolower($_POST['vcode'])!=strtolower($_SESSION['vcode'])){
	skip('register.php', '','驗證碼輸入錯誤!');
}
$_POST=escape($link,$_POST);
$query="select * from member where username='{$_POST['username']}'";
$result=execute($link, $query);
if(mysqli_num_rows($result)){
	skip('register.php', '', '這個用戶名已經注冊過了,請直接登錄');
}
?>

驗證碼:我沒加上去,因為操作不好,但是我寫一個驗證碼的代碼,可以自行添加

#驗證碼--m0re--
<?php
header('Content-type:image/jpeg');
$width=120;
$height=40;
$string='';
$element=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
for ($i=0; $i <5 ; $i++) { 
	$string.=$element[rand(0,count($element)-1)];
}
$img=imagecreatetruecolor($width, $height);
$color_bg=imagecolorallocate($img, rand(200,255), rand(200,255), rand(200,255));
$color_border=imagecolorallocate($img, rand(200,255), rand(200,255), rand(200,255));
$color_string=imagecolorallocate($img, rand(10,100), rand(10,100), rand(10,100));
imagefill($img, 0, 0, $color_bg);
imagerectangle($img, 0, 0, $width-1, $height-1, $color_border);
for ($i=0; $i <100 ; $i++) { 
	imagesetpixel($img, rand(0,$width-1), rand(0,$height-1), imagecolorallocate($img, rand(10,100), rand(10,100), rand(10,100)));
}
for ($i=0; $i <3 ; $i++) { 
	imageline($img, rand(0,$width/2), rand(0,$height), rand($width/2,$width), rand(0,$height), imagecolorallocate($img, rand(10,100), rand(10,100), rand(10,100)));
}
imagettftext($img, 25, rand(-5,5), rand(5,15), rand(30,35), $color_string, 'font/HARLOWSI.TTF', $string);
imagejpeg($img);
imagedestroy($img);
?>

注冊輸入內容后點擊注冊(submit),在phpmyadmin中可以看到插入的數據
m0re
還有注冊時間。

登錄
基於cookie的登錄,登錄將生成一個cookie,

<?php
header('Content-type:text/html;charset=utf-8');
if (isset($_POST['submit'])){
    if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username']===$username && $_POST['password']==='123456'){
        if (setcookie('username',$_POST['username'],time()+3600)){
            header('Location:404.html');//跳轉到指定網頁
        }
        else{
            echo 'cookie設置失敗!';
        }
    }else{
        echo '對不起,登錄失敗,請檢查您的用戶名或者密碼是否正確,注意密碼安全。';
    }
}
?>

一些部分不是很完善,之前還想着加上過濾,預防SQL注入和xss之類的簡單注入。但是,沒有扔掉這個HTML,所以就比較繁瑣了,還是自己寫的HTML可以,但是太簡陋了,最終沒有換。不過需要用到的知識差不多也都實現了。至於登錄成功后,就可以加一些自己的東西了。

總結

驗證碼是之前就寫過的,這都是之前讓學習PHP的時候前60節中的內容,所以總體來說不是很難,就是對我們這些小白,可能出現最多的就是連接出錯。無法插入等問題。多嘗試幾次就可以了,報錯就一個一個查。
最后貼一下我同學——奇點寫的博客地址,我們有較多討論,可以對比一下。我們寫的格式不一樣,有些細節我可能沒寫到。
https://blog.csdn.net/qq_45869039/article/details/108380902


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM