網(wǎng)站開發(fā)文檔范文今日國際新聞頭條15條
文章目錄
- 時(shí)間函數(shù)
- date
- getdate
- time
- 使用數(shù)組實(shí)現(xiàn)登錄注冊和修改密碼
- 簡單數(shù)組
- 增加元素方法
- 修改元素方法
- 刪除元素方法
- 具體實(shí)現(xiàn)方法
- 數(shù)組序列化
- 數(shù)組寫入文件
- 判斷元素是否在關(guān)聯(lián)數(shù)組中(登錄功能實(shí)現(xiàn))
- 實(shí)現(xiàn)注冊功能
- 實(shí)現(xiàn)修改admin用戶密碼功能
時(shí)間函數(shù)
時(shí)區(qū):中國 東8區(qū)
php.ini 時(shí)區(qū)設(shè)置為:date.timezone = Asia/Shanghai
分號為注釋
H = 小時(shí) i = 分鐘 s = 秒鐘
Ymd = 年月日
date
<?php//date 格式化一個(gè)本地時(shí)間/日期//getdate getdate 是一個(gè)數(shù)組,取數(shù)組中的值//time 返回當(dāng)前的unix時(shí)間戳header("Content-Type: text/html; charset=utf-8");$a = date ("H:i:s");$b = date ("Ymd");echo $b;echo "<br>";echo $a;
getdate
getdate 是一個(gè)數(shù)組
header("Content-Type: text/html; charset=utf-8");$a = date ("H:i:s");$b = date ("Ymd");$c = getdate ();echo $b;echo "<br>";echo $a;echo "<br>";var_dump($c);
array(11) { ["seconds"]=> int(17) ["minutes"]=> int(37) ["hours"]=> int(11) ["mday"]=> int(4) ["wday"]=> int(4) ["mon"]=> int(1) ["year"]=> int(2024) ["yday"]=> int(3) ["weekday"]=> string(8) "Thursday" ["month"]=> string(7) "January" [0]=> int(1704339437) }
取數(shù)組的時(shí)間戳 [0]=> int(1704339437) ,有了時(shí)間戳就可以將時(shí)間數(shù)組讀取出來
header("Content-Type: text/html; charset=utf-8");$a = date ("H:i:s");$b = date ("Ymd");$c = getdate ();echo $b;echo "<br>";echo $a;echo "<br>";var_dump($c);echo $c['0'];
有了時(shí)間戳就可以隨意輸出你想要的時(shí)間,具體代碼如下
header("Content-Type: text/html; charset=utf-8");$a = date ("Ymd H:i:s",1704349737);//$b = date ("Ymd",1704349737);$c = getdate (1704349737);echo $a;echo "<br>";echo $a;echo "<br>";var_dump($c);echo "<br>";echo $c['0'];echo '<br>';echo $c["year"].$c["mon"].$c["mday"]." ".$c["hours"].":".$c["minutes"].":".$c["seconds"];
time
time()函數(shù)返回當(dāng)前的uinx時(shí)間戳
header("Content-Type: text/html; charset=utf-8");$a = date ("Ymd H:i:s",1704349737);//$b = date ("Ymd",1704349737);$c = getdate (1704349737);echo $a;echo "<br>";echo $a;echo "<br>";var_dump($c);echo "<br>";echo $c['0'];echo '<br>';echo $c["year"].$c["mon"].$c["mday"]." ".$c["hours"].":".$c["minutes"].":".$c["seconds"];echo '<br>';echo time();
使用數(shù)組實(shí)現(xiàn)登錄注冊和修改密碼
數(shù)組函數(shù)的用途:1、增加一個(gè)元素 2、修改元素 3、刪除元素
簡單數(shù)組
<?phpheader("Content-Type: text/html; charset=utf-8");$a = array('鼠','牛','虎','龍','蛇'); var_dump($a);?>
增加元素方法
<?phpheader("Content-Type: text/html; charset=utf-8");$user=array('admin'=>'123456','test'=>'123','root'=>'789456');//關(guān)聯(lián)數(shù)組var_dump( $user );$user['administrator'] = 'admin';//往數(shù)組里增加一個(gè)元素echo "<br>";var_dump( $user );?>
修改元素方法
<?phpheader("Content-Type: text/html; charset=utf-8");$user=array('admin'=>'123456','test'=>'123','root'=>'789456');var_dump( $user );$user['administrator'] = 'admin';//往數(shù)組里增加一個(gè)元素echo "<br>";var_dump( $user );$user['administrator'] = 'admin123456';//修改元素echo "<br>";var_dump( $user );
?>
刪除元素方法
header("Content-Type: text/html; charset=utf-8");//$a = array('鼠','牛','虎','龍','蛇'); //var_dump($a);$user=array('admin'=>'123456','test'=>'123','root'=>'789456');var_dump( $user );$user['administrator'] = 'admin';//往數(shù)組里增加一個(gè)元素echo "<br>";var_dump( $user );$user['administrator'] = 'admin123456';//修改administrator 元素echo "<br>";var_dump( $user );unset($user["administrator"]);//刪除administrator 元素echo "<br>";var_dump( $user );
具體實(shí)現(xiàn)方法
數(shù)組序列化
數(shù)組不是字符串想寫到文件里面需要序列化
序列化之后會返回一個(gè)字符串
serialize() //序列化函數(shù)
header("Content-Type: text/html; charset=utf-8");$user=array('admin'=>'123456','test'=>'123','root'=>'789456');//數(shù)組$a=serialize($user);//序列化數(shù)組$userfile_put_contents('userpassword.txt',$a);//將$a的數(shù)據(jù)寫到userpassword.txt
數(shù)組寫入文件
$a=file_get_contents("userpassword.txt");//讀序列化后的文件,內(nèi)容為字符串$b=unserialize($a);//反序列化,將字符串轉(zhuǎn)為數(shù)組的過程var_dump($b);
判斷元素是否在關(guān)聯(lián)數(shù)組中(登錄功能實(shí)現(xiàn))
前端代碼
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>登錄頁面</title></head><body><form action="shuzu.php" method="get">用戶名:<input type="text" name="username">密碼:<input type="text" name="password"><input type="submit"></form></body></html>
后端代碼
//需求 1、登錄 2、注冊 3、修改密碼header("Content-Type: text/html; charset=utf-8");$u = $_GET["username"];$p = $_GET["password"];/*//$a = array('鼠','牛','虎','龍','蛇'); //var_dump($a);$user=array('admin'=>'123456','test'=>'123','root'=>'789456');var_dump( $user );$user['administrator'] = 'admin';//往數(shù)組里增加一個(gè)元素echo "<br>";var_dump( $user );$user['administrator'] = 'admin123456';//修改administrator 元素echo "<br>";var_dump( $user );unset($user["administrator"]);//刪除administrator 元素echo "<br>";var_dump( $user );*//*header("Content-Type: text/html; charset=utf-8");$user=array('admin'=>'123456','test'=>'123','root'=>'789456');//數(shù)組$a=serialize($user);//序列化數(shù)組$user,將變量轉(zhuǎn)換為字符串file_put_contents('userpassword.txt',$a);//將$a的數(shù)據(jù)寫到userpassword.txt*/$i=1;$a=file_get_contents("userpassword.txt");//讀序列化后的文件,內(nèi)容為字符串$b=unserialize($a);//反序列化,將字符串轉(zhuǎn)為數(shù)組的過程//var_dump($b);foreach($b as $key=>$value){ //循壞遍歷關(guān)聯(lián)數(shù)組$bif($u == $key && $p == $value){ //$u = $key $p = $value$i=0; //匹配成功將$i 設(shè)置為0 輸出 "登錄成功"echo "登錄成功";break;}}if($i == 1){echo "登錄失敗";} //未匹配成功$i=1 輸出 登錄失敗
實(shí)現(xiàn)注冊功能
前端頁面
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>注冊</title></head><body><form action="register.php" method="POST">用戶名:<input type="text" name="username">密碼: <input type="text" name="password">確認(rèn)密碼:<input type="text" name="password1"><input type="submit"></form></body></html>
后端頁面
//需求 1、登錄 2、注冊 3、修改密碼header("Content-Type: text/html; charset=utf-8");$u = $_POST["username"];$p = $_POST["password"];$p1 = $_POST ["password1"];if ($p != $p1) { //如果$p 不等于 $p1 則退出代碼執(zhí)行echo "兩次密碼不一致";exit;}$a=file_get_contents("userpassword.txt");//讀序列化后的文件,內(nèi)容為字符串$b=unserialize($a);//反序列化,將字符串轉(zhuǎn)為數(shù)組的過程//var_dump($b);foreach($b as $key=>$value){ //循壞遍歷關(guān)聯(lián)數(shù)組$bif($u == $key ){ //$u = $key echo "用戶已存在"; //查找用戶是否存在exit; //退出,代碼不再執(zhí)行}} $b[$u]=$p; //添加用戶元素$c=serialize($b); //序列化file_put_contents("userpassword.txt",$c);//寫入文件echo"注冊成功";
實(shí)現(xiàn)修改admin用戶密碼功能
前端代碼
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>改密碼</title></head><body><h1>admin 修改密碼</h1><form action="changepassword.php?username=admin" method="post">舊密碼:<input type="password" name="password">新密碼:<input type="password" name="password1"><input type="submit"></form></body></html>
后端代碼
//需求 1、登錄 2、注冊 3、修改密碼header("Content-Type: text/html; charset=utf-8");$u = $_GET["username"];$p = $_POST["password"];$p1 = $_POST["password1"];if ($p != $p1) { //如果$p 不等于 $p1 則退出代碼執(zhí)行echo "兩次密碼不一致";exit;}$i=1;$a = file_get_contents("..\userpassword.txt");$b = unserialize($a);foreach($b as $key=>$value){ //循壞遍歷關(guān)聯(lián)數(shù)組$bif($u == $key ){ //$u = $key $b[$u]=$p;$c=serialize($b); //序列化file_put_contents("..\userpassword.txt",$c);//寫入文件echo"密碼修改成功";break;}else{$i=0;}}if($i=0){echo"用戶不存在";}