摘要:操作方式方式去除跨域腳本攻擊中文轉碼判斷參數是否為空判斷是否為數組用來處理不是數組的情況用來處理數組用來處理數組返回微信獲取授權第一步取得第二步根據全局和查詢用戶信息給微信用戶發送文字消息導出表格兌換列表導出表格是否下載表格第一列
1.curl操作
POST方式
public static function httpPost($url, $param = "") { $ch1 = curl_init(); if (version_compare(PHP_VERSION, "5.5.0", ">")) { curl_setopt($ch1, CURLOPT_SAFE_UPLOAD, false); } curl_setopt($ch1, CURLOPT_URL, $url); curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false); if (!empty($param)) { curl_setopt($ch1, CURLOPT_POST, 1); curl_setopt($ch1, CURLOPT_POSTFIELDS, $param); } curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch1); curl_close($ch1); return $result; }
GET方式
public static function httpGet($url) { $oCurl = curl_init(); if (stripos($url, "https://") !== false) { curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if (intval($aStatus["http_code"]) == 200) { return $sContent; } else { return false; } }
2.去除跨域腳本攻擊
public static function removeXSS($val) { $val = preg_replace("/([x00-x08,x0b-x0c,x0e-x19])/", "", $val); $search = "abcdefghijklmnopqrstuvwxyz"; $search .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $search .= "1234567890!@#$%^&*()"; $search .= "~`";:?+/={}[]-_|""; for ($i = 0; $i < strlen($search); $i++) { $val = preg_replace("/([xX]0{0,8}" . dechex(ord($search[$i])) . ";?)/i", $search[$i], $val); $val = preg_replace("/({0,8}" . ord($search[$i]) . ";?)/", $search[$i], $val); } $ra1 = array("javascript", "vbscript", "expression", "applet", "meta", "xml", "blink", "link", "style", "script", "embed", "object", "iframe", "frame", "frameset", "ilayer", "layer", "bgsound", "title", "base"); $ra2 = array("onabort", "onactivate", "onafterprint", "onafterupdate", "onbeforeactivate", "onbeforecopy", "onbeforecut", "onbeforedeactivate", "onbeforeeditfocus", "onbeforepaste", "onbeforeprint", "onbeforeunload", "onbeforeupdate", "onblur", "onbounce", "oncellchange", "onchange", "onclick", "oncontextmenu", "oncontrolselect", "oncopy", "oncut", "ondataavailable", "ondatasetchanged", "ondatasetcomplete", "ondblclick", "ondeactivate", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "onerror", "onerrorupdate", "onfilterchange", "onfinish", "onfocus", "onfocusin", "onfocusout", "onhelp", "onkeydown", "onkeypress", "onkeyup", "onlayoutcomplete", "onload", "onlosecapture", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onmove", "onmoveend", "onmovestart", "onpaste", "onpropertychange", "onreadystatechange", "onreset", "onresize", "onresizeend", "onresizestart", "onrowenter", "onrowexit", "onrowsdelete", "onrowsinserted", "onscroll", "onselect", "onselectionchange", "onselectstart", "onstart", "onstop", "onsubmit", "onunload"); $ra = array_merge($ra1, $ra2); $found = true; while ($found == true) { $val_before = $val; for ($i = 0; $i < sizeof($ra); $i++) { $pattern = "/"; for ($j = 0; $j < strlen($ra[$i]); $j++) { if ($j > 0) { $pattern .= "("; $pattern .= "([xX]0{0,8}([9ab]);)"; $pattern .= "|"; $pattern .= "|({0,8}([9|10|13]);)"; $pattern .= ")*"; } $pattern .= $ra[$i][$j]; } $pattern .= "/i"; $replacement = substr($ra[$i], 0, 2) . "" . substr($ra[$i], 2); $val = preg_replace($pattern, $replacement, $val); if ($val_before == $val) { $found = false; } } } return $val; }
3.JSON中文轉碼
public static function cn_arr_json($arr) { $_var = self::var_urlencode($arr); $_str = json_encode($_var); return urldecode($_str); } public static function var_urlencode($var) { if (empty($var)) {return false;} //判斷參數是否為空 if (is_array($var)) { //判斷是否為數組 foreach ($var as $k => $v) { if (is_scalar($v)) { //if用來處理不是數組的情況 $var[$k] = urlencode($v); } else { $var[$k] = self::var_urlencode($v); //else用來處理數組 } } } else { $var = urlencode($var); //用來處理數組 } return $var; }
4.ajax返回
public function ajaxJson($state = "success",$msg = "",$params = ""){ $json = array(); if($msg){$json["msg"] = $msg;} if($params){ if(is_array($params)){ foreach($params as $key => $val){ $json[$key] = $val; } }else{ $json["p"] = $params; } } $json["state"] = $state; echo json_encode($json);exit; }
5.微信獲取授權
function _diy_wxauth($authurl, $scope = "snsapi_base")
{
$appid=XUETANG_APPID; $secret = XUETANG_SCRECT; $code = isset($_GET["code"])?$_GET["code"]:""; if($code){
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $oauth2 = getJson($oauth2Url);
//第二步:根據全局access_token和openid查詢用戶信息
$access_token = $oauth2["access_token"]; $openid = $oauth2["openid"]; $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; return getJson($get_user_info_url); }else{ $redirect_uri = urlencode( WEBSITEURL.$authurl); $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=123#wechat_redirect"; header("Location:".$url); }
}
//給微信用戶發送文字消息
function replyhreftxt($openid, $txt)
{
$access_token = getDxToken(); $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token; if ($openid && $txt) { $datajson = "{"touser":"" . $openid . "","msgtype":"text","text":{"content":"" . $txt . ""}}"; $res = http_post($url, $datajson); if ($res) { return true; } else { return false; } } else { return false; }
}
6.PHPExcel導出Excel表格
/* 兌換列表導出Excel表格
$isDown 是否下載
$title 表格第一列
$data 需要導出的數據
$filename 文件名
*/
public function actionExceldata() { $isDown=true; $fileName = "兌換列表"; $title = [0=>"用戶名",1=>"手機號",2=>"添加時間",3=>"狀態","標題"]; $getPar = $this->params->get(); $this->exchangeModels->setParams($getPar); $data = $this->exchangeModels->getAll(); foreach ($data as $k => $v){ if($v["state"] == 1){ $data[$k]["state"] = "未發放"; }elseif($v["state"] == 2){ $data[$k]["state"] = "待發送"; }else{ $data[$k]["state"] = "已發放"; } } require($_SERVER["DOCUMENT_ROOT"]."/../../vendor/PHPExcel-1.8/Classes/PHPExcel.php"); require($_SERVER["DOCUMENT_ROOT"]."/../../vendor/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php"); $obj = new PHPExcel(); //橫向單元格標識 $cellName = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ"); $obj->getActiveSheet(0)->setTitle("sheet名稱"); //設置sheet名稱 $_row = 1; //設置縱向單元格標識 if($title){ $_cnt = count($title); $obj->getActiveSheet(0)->mergeCells("A".$_row.":".$cellName[$_cnt-1].$_row); //合并單元格 $obj->setActiveSheetIndex(0)->setCellValue("A".$_row, "數據導出:".date("Y-m-d H:i:s")); //設置合并后的單元格內容 $_row++; $i = 0; foreach($title AS $v){ //設置列標題 $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v); $i++; } $_row++; } //填寫數據 if($data){ $i = 0; foreach($data AS $_v){ $j = 0; foreach($_v AS $_cell){ $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell); $j++; } $i++; } } //文件名處理 if(!$fileName){ $fileName = uniqid(time(),true); } $objWrite = PHPExcel_IOFactory::createWriter($obj, "Excel2007"); if($isDown){ //網頁下載 header("pragma:public"); header("Content-Disposition:attachment;filename=$fileName.xls"); $objWrite->save("php://output");exit; } /*$_fileName = iconv("utf-8", "gb2312", $fileName); //轉碼 $_savePath = $savePath.$_fileName.".xlsx"; $objWrite->save($_savePath);exit;*/ //return $savePath.$fileName.".xlsx"; }
//導出數據,但是不是Excel表格,優點:導出的數據量比較大
*@param $datas array 設置表格數據
*@param $titlename string 設置head
*@param $title string 設置表頭
*/
public function excelData($datas,$titlename,$title,$filename){
$str = "rn
{$v} | ";
{ $this->GiftdetailModels->setParams($this->params->get()); $list = $this->GiftdetailModels->getAllList(); $str = "用戶名稱,禮品,禮品碼,禮品對應密碼,使用積分,兌換時間 "; $str = iconv("utf-8","gb2312",$str); foreach($list as $row){ $nickname = iconv("UTF-8", "GB2312//IGNORE",$row["nickname"]); $giftname = iconv("UTF-8", "GB2312//IGNORE",$row["giftname"]); $gnumber = iconv("UTF-8", "GB2312//IGNORE",$row["gnumber"]); $gmima = iconv("UTF-8", "GB2312//IGNORE",$row["gmima"]); $usescore = iconv("UTF-8", "GB2312//IGNORE",$row["usescore"]); $addtime = iconv("UTF-8", "GB2312//IGNORE", date("Y-m-d H:i:s",$row["addtime"])); $str .= $nickname.",".$giftname.",".$gnumber.",".$gmima.",".$usescore.",".$addtime." "; } $filename = "兌換管理_".date("YmdHis").".csv"; //設置文件名 export_csv($filename,$str); //導出 }
public static function export_csv($filename,$data) {
header("Content-type:text/csv"); header("Content-Disposition:attachment;filename=".$filename); header("Cache-Control:must-revalidate,post-check=0,pre-check=0"); header("Expires:0"); header("Pragma:public"); echo $data; }
7.生成隨機驗證碼
function getCode()
{
$str = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm"; return substr(str_shuffle($str),26,10);
}
8.判斷手機號歸屬地
function phoneCity($account)
{
$url = "http://202.102.221.75:18085/wxservice/hd.jsp"; $serverSign = MD5($account."$"."TTMM%S%XXYY"); $param = array( "account" => $account, "sign" => $serverSign, ); $res = CurlPost($url, $param); $newArr = json_decode($res,true); return $newArr["ContractRoot"]["SvcCont"]["SOO"][0]["PHONE_LATN_TYPE_RES"][0]["LATN_ID"];
}
9.成功或失敗返回ajax
/** * 將錯誤信息返回給Ajax請求 * @param string $msg 錯誤信息描述 * @param integer $code 錯誤代碼,默認1 */ public function ajaxError($msg = "", $code = 1) { $this->ajaxEcho([ "error" => $code, "msg" => $msg, ]); } /** * 將成功信息返回給Ajax請求 * @param mix $data 返回數據 */ public function ajaxSuccess($data = "") { $this->ajaxEcho([ "error" => 0, "data" => $data, ]); } /** * 一般情況下直接使用ajaxSuccess和ajaxError可以滿足絕大部分需求 * 考慮靈活性,本函數設置為公開,以滿足特殊需求 */ public function ajaxEcho($arr) { header("Content-type:application/json;charset=utf-8"); echo json_encode($arr); exit; }
10.正則表達式
function regex($value,$rule) { $validate = array( "require" => "/S+/", "email" => "/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/", "url" => "/^http(s?)://(?:[A-za-z0-9-]+.)+[A-za-z]{2,4}(?:[/?#][/=?%-&~`@[]":+!.#w]*)?$/", "currency" => "/^d+(.d+)?$/", "number" => "/^d+$/", "qq" => "/^d*$/", "telephone" => "/^0([1-9]{3})([0-9]{7,8})$/", "zip" => "/^d{6}$/", "integer" => "/^[-+]?d+$/", "double" => "/^[-+]?d+(.d+)?$/", "english" => "/^[A-Za-z]+$/", "mobile" => "/^1[3-9]{1}[0-9]{1}[0-9]{8}$/", ); // 檢查是否有內置的正則表達式 if(isset($validate[strtolower($rule)])) $rule = $validate[strtolower($rule)]; return preg_match($rule,$value)===1; }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/29789.html
摘要:將返回通過回調真值測試的第一個項的鍵。方法將集合分割為多個給定大小的較小集合。它可用于在任何位置的調試和查找集合內的內容。方法用于遍歷整個集合。這對集合同樣有效。它將導致由第二個參數的值作為鍵的集合。它接受回調并傳遞并將集合傳遞給它。 showImg(https://segmentfault.com/img/remote/1460000018924972); 文章轉自:https://...
摘要:將數組或者集合中的全部或者一部數據取出來,用迭代器比較方便迭代器能陸續遍歷幾個迭代器按順序迭代訪問幾個不同的迭代器。 一、SPL簡介 ?????什么是SPL PHP的標準庫SPL:Standard PHP Library ?????SPL: 用于解決常見普遍問題的一組接口與類的集合 ?????Common Problem: 數學建模/數據結構 解決數據怎么存儲的問題 元素遍歷 ...
摘要:一概述集合是引入的新的內置對象類型,其特點同數學意義的集合,即集合內所有元素不重復元素唯一。數組集合對比數組和集合,數組可以加入重復數據,而集合的所有元素是唯一的不允許重復。因此,適合臨時存放一組對象,以及存放跟對象綁定的信息。 本文同步帶你入門 帶你入門 JavaScript ES6 (五) 集合,轉載請注明出處。 前面我們學習了: for of 變量和擴展語法 塊作用域變量和解構...
摘要:前言集合通過進行實例,的內核大部分的參數傳遞都用到了集合,但這并不代表集合就是好的。當你需要處理一組數組時,你可能就需要它幫助你快捷的解決實際問題。例如我們需要通過一個水平線切分數組,將其分為個及以上的數組個數。謝謝,還不抓緊去練習下集合 showImg(https://segmentfault.com/img/bVbhjvY?w=600&h=296); 前言 集合通過 Illumin...
摘要:以實現自己熟悉的東西為導向比如我們做后端開發,首先是常用的循環迭代條件判斷增刪改成。它是由實現的,不保證元素的順序,也就是說所說元素插入的順序與輸出的順序不一致。 下面是我直播的文字版,直播地址:https://segmentfault.com/l/15...代碼:https://github.com/zhoumengka...整個項目我們我又細分了6個版本來演進,希望更加便于大家對比...
閱讀 1181·2021-09-22 15:24
閱讀 2285·2019-08-30 15:44
閱讀 2615·2019-08-30 10:55
閱讀 3355·2019-08-29 13:25
閱讀 1639·2019-08-29 13:09
閱讀 1391·2019-08-26 14:05
閱讀 1379·2019-08-26 13:58
閱讀 1985·2019-08-26 11:57