PHP 杂

作者: 冰镇苏打水 | 来源:发表于2018-05-15 11:13 被阅读11次

    下载文件

    • 很多时候,需要下载文件,但是经常是在浏览器打开,不符合需求

        public function downQrImg()
        {
      
            $file_url = ''; // 文件地址
            $file_name = ''; // 文件名
            $file_format = ''; // 文件格式
            $out_filename = $file_name . '.' . $file_format;
            $file = @fopen($file_url, "r");
            if ($file) {
                $content = "";
                while (!feof($file)) {//测试文件指针是否到了文件结束的位置
                    $data = fread($file, 1024);
                    $content .= $data;
                }
                fclose($file);
                $filesize = strlen($content);
                header('Accept-Ranges: bytes');
                header('Accept-Length: ' . $filesize);
                header('Content-Transfer-Encoding: binary');
                header('Content-type: application/octet-stream');
                header('Content-Disposition: attachment; filename=' . $out_filename);
                header('Content-Type: application/octet-stream; name=' . $out_filename);
                echo $content;
                die();
            } else {
                echo "文件不存在";
            }
        }
      

    身份证验证

    • 验证身份证真实性

        <?php  
            function checkIdCard($idcard){  
          
            // 只能是18位  
            if(strlen($idcard)!=18){  
                return false;  
            }  
          
            // 取出本体码  
            $idcard_base = substr($idcard, 0, 17);  
          
            // 取出校验码  
            $verify_code = substr($idcard, 17, 1);  
          
            // 加权因子  
            $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);  
          
            // 校验码对应值  
            $verify_code_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');  
          
            // 根据前17位计算校验码  
            $total = 0;  
            for($i=0; $i<17; $i++){  
                $total += substr($idcard_base, $i, 1)*$factor[$i];  
            }  
          
            // 取模  
            $mod = $total % 11;  
          
            // 比较校验码  
            if($verify_code == $verify_code_list[$mod]){  
                return true;  
            }else{  
                return false;  
            }  
          
        }  
          
          
        $idcard = '1424311994****3030';  
        var_dump(checkIdCard($idcard));  
        ?>      
      

    is_null / empty / isset

    is_null

    • 判断变量是否为 NULL

    empty

    • 判断变量是否为空或为0

    isset

    • 判断变量是否已存在
    变量 is_null empty isset
    $a = '' false true true
    $a = null true true false
    var $a true true false
    $a = array() false true true
    $a = false false true true
    $a = 15 false false true
    $a = 1 false false true
    $a = 0 false true true
    $a = '0' false true true
    $a = 'true' false false true
    $a = 'false' false false true
    • 使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE
    • 等于 NULL,所以被认为是未置值的

    PHP防止重复提交

        <?php
        session_start();
        header("Content-Type:text/html;charset=utf-8");
        function set_token()
        {
            $_SESSION['token'] = md5(microtime(true));
        }
        
        function valid_token()
        {
            $return = $_REQUEST['token'] === $_SESSION['token'] ? true : false;
            set_token();
            return $return;
        }
        
        if (!isset($_SESSION['token'])||$_SESSION['token'] === '') {
            set_token();
        }
        
        if (isset($_POST['web'])) {
            if (!valid_token()) {
                echo "token error, 请不要重复提交";
            }else{
                echo "成功提交, Value:".$_POST['web'];
            }
        }else{ ?>
        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>PHP防止重复提交表单</title>
        <meta name="keywords" content="PHP" />
        <meta name="description" content="PHP防止重复提交表单" />
        </head>
        <body>
        <div id="main">
          <div class="demo">
            <form method="post" action="">
              <input type="hidden" name="token" value="<?= $_SESSION['token']?>">
              <input type="text" class="input" name="web" value="重复测试">
              <input type="submit" class="btn" value="提交" />
            </form>
          </div>
        </div>
        </body>
        </html>
        <?php } ?>

    相关文章

      网友评论

        本文标题:PHP 杂

        本文链接:https://www.haomeiwen.com/subject/ewaddftx.html