PHP 基础编程 2

时间函数

时区:中国 东8区
php.ini 时区设置为:date.timezone = Asia/Shanghai
分号为注释
H = 小时 i = 分钟 s = 秒钟
Ymd = 年月日

date

<?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

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

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"注册成功";

实现修改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 则退出代码执行

    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"用户不存在";

}

相关推荐

  1. C++基础-编程练习题2

    2024-01-05 11:42:06       37 阅读
  2. 网络安全练气篇——PHP编程语言基础

    2024-01-05 11:42:06       30 阅读
  3. 突破编程_C++_面试(基础知识(2))

    2024-01-05 11:42:06       57 阅读
  4. <span style='color:red;'>PHP</span><span style='color:red;'>基础</span>

    PHP基础

    2024-01-05 11:42:06      35 阅读
  5. PHP基础

    2024-01-05 11:42:06       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 11:42:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 11:42:06       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 11:42:06       82 阅读
  4. Python语言-面向对象

    2024-01-05 11:42:06       91 阅读

热门阅读

  1. 编程笔记 html5&css&js 024 HTML表单元素

    2024-01-05 11:42:06       54 阅读
  2. HTML概念与W3C标准 及其规范

    2024-01-05 11:42:06       56 阅读
  3. html websocket的基本使用

    2024-01-05 11:42:06       58 阅读
  4. 数据结构和算法:二叉树解题思维模式

    2024-01-05 11:42:06       58 阅读
  5. LeetCode每日一题.08(162.寻找峰值)

    2024-01-05 11:42:06       50 阅读
  6. 力扣:209. 长度最小的子数组(Python3)

    2024-01-05 11:42:06       54 阅读
  7. 力扣122. 买卖股票的最佳时机 II

    2024-01-05 11:42:06       58 阅读
  8. Leetcode12-统一一致字符串的数目(1684)

    2024-01-05 11:42:06       57 阅读
  9. 力扣第一百道题,记录一下——x 的平方根

    2024-01-05 11:42:06       53 阅读
  10. 爬虫案列 --抖音视频批量爬取

    2024-01-05 11:42:06       38 阅读
  11. CentOS快速安装Mysql5.7(Alibaba Cloud Linux兼容)

    2024-01-05 11:42:06       61 阅读
  12. 观察黄绿豆两则

    2024-01-05 11:42:06       58 阅读
  13. C++ 学习系列 -- using关键字

    2024-01-05 11:42:06       56 阅读
  14. Linux搭建MQTT服务器(mosquitto)并使用

    2024-01-05 11:42:06       59 阅读
  15. 测试在软件开发中存在的意义

    2024-01-05 11:42:06       58 阅读
  16. 2023新年总结与展望

    2024-01-05 11:42:06       48 阅读