时区:中国 东8区
php.ini 时区设置为:date.timezone = Asia/Shanghai
分号为注释
H = 小时 i = 分钟 s = 秒钟
Ymd = 年月日
<?php
//date 格式化一个本地时间/日期
//getdate getdate 是一个数组,取数组中的值
//time 返回当前的unix时间戳
header("Content-Type: text/html; charset=utf-8");
$a = date ("H:i:s");
$b = date ("Ymd");
echo $b;
echo "<br>";
echo $a;
getdate 是一个数组
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) }
取数组的时间戳 [0]=> int(1704339437) ,有了时间戳就可以将时间数组读取出来
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'];
有了时间戳就可以随意输出你想要的时间,具体代码如下
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()函数返回当前的uinx时间戳
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();
数组函数的用途:1、增加一个元素 2、修改元素 3、删除元素
<?php
header("Content-Type: text/html; charset=utf-8");
$a = array('鼠','牛','虎','龙','蛇');
var_dump($a);
?>
<?php
header("Content-Type: text/html; charset=utf-8");
$user=array('admin'=>'123456','test'=>'123','root'=>'789456');//关联数组
var_dump( $user );
$user['administrator'] = 'admin';//往数组里增加一个元素
echo "<br>";
var_dump( $user );
?>
<?php
header("Content-Type: text/html; charset=utf-8");
$user=array('admin'=>'123456','test'=>'123','root'=>'789456');
var_dump( $user );
$user['administrator'] = 'admin';//往数组里增加一个元素
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';//往数组里增加一个元素
echo "<br>";
var_dump( $user );
$user['administrator'] = 'admin123456';//修改administrator 元素
echo "<br>";
var_dump( $user );
unset($user["administrator"]);//删除administrator 元素
echo "<br>";
var_dump( $user );
数组不是字符串想写到文件里面需要序列化
序列化之后会返回一个字符串
serialize() //序列化函数
header("Content-Type: text/html; charset=utf-8");
$user=array('admin'=>'123456','test'=>'123','root'=>'789456');//数组
$a=serialize($user);//序列化数组$user
file_put_contents('userpassword.txt',$a);//将$a的数据写到userpassword.txt
$a=file_get_contents("userpassword.txt");//读序列化后的文件,内容为字符串
$b=unserialize($a);//反序列化,将字符串转为数组的过程
var_dump($b);
前端代码
<!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';//往数组里增加一个元素
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');//数组
$a=serialize($user);//序列化数组$user,将变量转换为字符串
file_put_contents('userpassword.txt',$a);//将$a的数据写到userpassword.txt
*/
$i=1;
$a=file_get_contents("userpassword.txt");//读序列化后的文件,内容为字符串
$b=unserialize($a);//反序列化,将字符串转为数组的过程
//var_dump($b);
foreach($b as $key=>$value){ //循坏遍历关联数组$b
if($u == $key && $p == $value){ //$u = $key $p = $value
$i=0; //匹配成功将$i 设置为0 输出 "登录成功"
echo "登录成功";
break;
}
}
if($i == 1){
echo "登录失败";
} //未匹配成功$i=1 输出 登录失败
前端页面
<!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">
确认密码:<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 则退出代码执行
echo "两次密码不一致";
exit;
}
$a=file_get_contents("userpassword.txt");//读序列化后的文件,内容为字符串
$b=unserialize($a);//反序列化,将字符串转为数组的过程
//var_dump($b);
foreach($b as $key=>$value){ //循坏遍历关联数组$b
if($u == $key ){ //$u = $key
echo "用户已存在"; //查找用户是否存在
exit; //退出,代码不再执行
}
}
$b[$u]=$p; //添加用户元素
$c=serialize($b); //序列化
file_put_contents("userpassword.txt",$c);//写入文件
echo"注册成功";
前端代码
<!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 则退出代码执行
echo "两次密码不一致";
exit;
}
$i=1;
$a = file_get_contents("..\userpassword.txt");
$b = unserialize($a);
foreach($b as $key=>$value){ //循坏遍历关联数组$b
if($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"用户不存在";
}