怎么新建MySQL数据库

发布时间:2022-08-02 作者:admin
阅读:317
在这篇文章中,我们来学习一下“PHP方法封装常见的有几种,怎么做”的相关知识,下文有详细的讲解,易于大家学习和理解,有需要的朋友可以借鉴参考,下面就请大家跟着小编的思路一起来学习一下吧。

1.get请求
function get_curl($url, $timeout = 5) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    $result = curl_exec( $ch );
    curl_close($ch);
    return $result;
}

2.post请求

function post_curl($url, $data, $timeout = 5) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result = curl_exec( $ch );
    curl_close($ch);
    return $result;
}

3.随机字符串

function random_str($len=10, $type=1){
    switch($type){
        case 2:
            $chars='0123456789';
            break;
        case 3:
            $chars='abcdefghijklmnopqrstuvwxyz';
            break;
        case 4:
            $chars='ABDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 5:
            $chars='abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 6:
            $chars='abcdefghijklmnopqrstuvwxyz0123456789';
            break;
        default:
            $chars='abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            break;
    }
 
    $str = '';
    for ( $i = 0; $i <$len; $i++ ){
        $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $str;
}

4.返回json数据

function json_back($data, $is_exit = true) {
    $callback = $_GET['callback'] ?? '';
    if ($callback) {
        echo '' . $callback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
    } else {
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }
    if ($is_exit) exit();
}

5.数据过滤

function check_data($data){
    if(is_array($data)){
        foreach($data as $key => $v){
            $data[$key] = $this->check_data($v);
        }
    }else{
        $data = trim($data);
        $data = strip_tags($data);
        $data = htmlspecialchars($data);
        $data = addslashes($data);
    }
    return $data;
}

6.重定向

function redirect($url = 'https://blog.csdn.net/weixin_41635750'){
    if (!headers_sent()) {
        header("Location: {$url}");
        exit();
    }else{
        $str = "<meta http-equiv='Refresh' content='0;URL={$url}'>";
        exit($str);
    }
}

7.获取客户端ip

function get_real_ip() {
    if(@$_SERVER["HTTP_ALI_CDN_REAL_IP"]){
        $ip = $_SERVER["HTTP_ALI_CDN_REAL_IP"];
    }
    elseif (@$_SERVER["HTTP_X_FORWARDED_FOR"] ?: false) {
        $ip  = $_SERVER["HTTP_X_FORWARDED_FOR"];
        $ips = explode(',', $ip);
        $ip  = $ips[0];
    } elseif (@$_SERVER["HTTP_CDN_SRC_IP"] ?: false) {
        $ip = $_SERVER["HTTP_CDN_SRC_IP"];
    } elseif (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    } elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');
    } elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $ip = str_replace(array('::ffff:', '[', ']'), array('', '', ''), $ip);
 
    return $ip;
}

现在大家对于PHP方法封装常见的有几种,怎么做的内容应该都有一定的认识了吧,希望这篇能对大家有所帮助。最后,想要了解更多,欢迎关注群英网络,群英网络将为大家推送更多相关的文章。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

二维码-群英

长按识别二维码并关注微信

更方便到期提醒、手机管理

7*24 全天候服务

售前 400-678-4567

售后 0668-2555666

售后 400 678 4567

信息安全 0668-2555 118

域名空间 3004329145