zoukankan      html  css  js  c++  java
  • php 重要函数归集

    1、json_encode 与 json_decode

    json_encode 把数组转化成字符串 json_encode(array)

    json_decode 把目标字符串转成数组json_decode(string,true),如果省略第二个参数,那么生成的是个对象

    <?php
    header('Content-type:text/html;charset=utf8');
    $arr = [
        'a' => 'first',
        'b' => 'second',
        'c' => 'third',
        'd' => 'fourth',
        'f' => 'fifth',
        'e' => 'sixth'
    ];
    $str = json_encode($arr);
    var_dump($str);
    //输出 string(75) "{"a":"first","b":"second","c":"third","d":"fourth","f":"fifth","e":"sixth"}"
    var_dump(json_decode($str,true));
    //输出 array(6) { ["a"]=> string(5) "first" ["b"]=> string(6) "second" ["c"]=> string(5) "third" ["d"]=> string(6) "fourth" ["f"]=> string(5) "fifth" ["e"]=> string(5) "sixth" }
    //注意如果省略第二个参数,那么输出的是一个对象
    ?>

     在处理ajax请求的时候,采用这个函数进行数据的返回文件名test.php

    <?php
    header('Content-type: application/json');
    ini_set('display_errors', true);
    
    class testData
    {
        public function getData() {
            return json_encode([
                'name' => 'aaa',
                'age' => 30
            ]);
        }
    }
    $data = new testData();
    echo $data->getData();
    ?>

    前端代码

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>test</title>
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script>
            $(() => {
                $.post('./test.php', {'name': 'bbbb'}, function (data) {
                    console.log(data);
                });
            })
        </script>
    </head>
    <body>
    <div>abc</div>
    </body>
    </html>

    2、password_hash、password_verify 与 password_get_info

    password_hash(string,int $algo) algo的选项有PASSWORD_DEFAULT ,PASSWORD_BCRYPT ,PASSWORD_ARGON2I

    password_verify(string,hash) 判断string与hash是否一致

    password_get_info(hash)获取hash的相关信息

    <?php
    header('Content-type:text/html;charset=utf8');
    $str = 'abcd123456';
    $build = password_hash($str, PASSWORD_DEFAULT);
    echo $build;
    //输出 $2y$10$XUhYPpXM2OxI6VtTGwbv2OKSnEMJAsx7fRVGJypyYcgmd61fIrIse
    var_dump(password_get_info($build));
    //输出 array(3) { ["algo"]=> int(1) ["algoName"]=> string(6) "bcrypt" ["options"]=> array(1) { ["cost"]=> int(10) } }
    var_dump(password_verify($str, $build));
    //输出 bool(true)
    ?>

     3、ini_set,int_get

    ini_set(varname,value)是设置php.ini里的环境变量的值。varname表示属性,value表示值

    ini_get(varname)是获取php.ini里的环境变量的值。

    ini_get_all()是表示获取全部的php.ini里的环境变量的值。

    <?php
    //设置编码
    header("Content-type:text/html;charset=utf-8");
    //设置时区
    ini_set('date.timezone', 'Asia/Shanghai');
    //设置是否显示错误
    ini_set('display_errors', 1);
    var_dump(ini_get('display_errors'));
    //输出 string(1) "1"

     4、spl_autoload_register

    spl_autoload_register(array)表示自动加载模块,接收一个参数array里面有两个子项,第一个子项表示主体对象,第二个接收一个函数,表示找不到模块时运行的函数,并且函数默认一个参数,该参数表示路径,第一个参数调用的$this表示调用本类里面的相对应的方法

    //框架运行的方法
    public function run()
    {
         spl_autoload_register([$this, 'load']);
         $url = $_SERVER['REQUEST_URI'];
        //其他代码。。。
    }
    
    /**解析地址
      * @param $className
      */
    private function load($className)
    {
        if (strpos($className, 'app') !== false) {
            $path = str_replace('app\', APP_PATH . 'app/', $className) . '.php';
        } else if (strpos($className, 'yf') !== false) {
            $path = MAIN . $className . '.php';
        }else {
            $path = APP_PATH . sprintf('App/controllers/%s.php', $className);
        }
        if (file_exists($path)) {
            require $path;
        }
    }

    6、set_error_handler与set_exception_handler

    set_error_handler([class,method])表示对错误信息的收集,class表示调用的类,method表示相对应的类里的方法

    set_exception_handler([class,method])表示对异常信息的收集,class表示调用的类,method表示相对应的类里的方法

    注意:这两个方法都不能用protected与private来修饰,只能用public,并且要注意set_exception_handler的错误信息的获取,祥见例子

    //框架运行的方法
    public function run()
    {
        spl_autoload_register([$this, 'load']);
        //收集错误信息
        set_error_handler([$this, 'AppError']);
        //收集异常信息
        set_exception_handler([$this, 'AppException']);
        //其他代码块
    }
    /**错误信息收集的方法
     * @param $errNum
     * @param $errStr
     * @param $errPos
     * @param $errLine
     */
    public static function AppError($errNum, $errStr, $errPos, $errLine)
    {
        $file = fopen('./App/log/error.txt', 'a');
        $err = "%s,错误信息:%s,错误位置:%s,第%s行,请求地址:%s,请求时间:%s
    ";
        fprintf($file, $err, $errNum, $errStr, $errPos, $errLine, $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']));
        exit;
    }
    
    /**异常信息的收集方法
     * @param $obj
     */
    public static function AppException($obj)
    {
        $file = fopen('./App/log/error.txt', 'a');
        $exception = "%s,错误信息:%s,错误位置:%s,第%s行,请求地址:%s,请求时间:%s
    ";
        fprintf($file, $exception, $obj->getCode(), $obj->getMessage(), $obj->getFile(), $obj->getLine(), $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']));
        exit;
    }
  • 相关阅读:
    云如何解决安全问题 狼人:
    存储安全 系统的最后一道防线 狼人:
    云安全仍是企业决策者最大担心 狼人:
    骇客宣称已入侵多家认证机构 波及微软、谷歌 狼人:
    盘点云计算服务中的隐患 狼人:
    云服务安全吗?美国政府用实际行动告诉你 狼人:
    微软高层称移动设备越多 对信息安全需更多考量 狼人:
    云计算需要让安全优先 狼人:
    金山网络两月被黑4次 入侵黑客留名挑衅 狼人:
    惠普推出全新企业级安全软件 狼人:
  • 原文地址:https://www.cnblogs.com/rickyctbu/p/9763913.html
Copyright © 2011-2022 走看看