php 个人函数库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 个人函数库相关的知识,希望对你有一定的参考价值。
crypt.php
<?php function pkcs5_pad ($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } function myCrypt($input,$key) { $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $input =pkcs5_pad($input, $size); $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_ECB, ‘‘); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $data = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); $data = base64_encode($data); return $data; } function myDecrypt($str,$key) { $decrypted= mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, base64_decode($str), MCRYPT_MODE_ECB ); $dec_s = strlen($decrypted); $padding = ord($decrypted[$dec_s-1]); $decrypted = substr($decrypted, 0, -$padding); return $decrypted; } ?>
Common.php
<?php //DES加密类库 include ‘crypt.php‘; //Lee便携式类库 include ‘Lee.php‘; function POST($get_key) { $str=$_POST[$get_key]; return Validata($str); } function GET($get_key) { $str=$_GET[$get_key]; return Validata($str); } function Validata($str) { //如果值为空,那么返回空字符串 if(!isset($str)) return ""; //过滤html标记 $farr = array("/<(\/?)(script|i?frame|style|html|body|title|link|meta|\?|\%)([^>]*?)>/isU"); //过滤类似 <script> <style> <object> <meta> <iframe> 等 $str = preg_replace($farr,"",$str); //对单引号、双引号等预定义字符 前面加上反斜杠 如‘变成\‘ $str=addslashes($str); //过滤敏感词汇 $str=str_replace(explode(",", UNSAFE_WORD),"***",$str); //返回结果 return trim($str); } function IP() { if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $cip = $_SERVER["HTTP_CLIENT_IP"]; } elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ $cip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } elseif(!empty($_SERVER["REMOTE_ADDR"])){ $cip = $_SERVER["REMOTE_ADDR"]; } else{ $cip = ""; } return $cip; } function set_cache($key,$value,$expireTime) { $m = new Memcache(); $m->connect(CACHE_IP,CACHE_PORT); $m->set($key,$value,0,$expireTime); } function get_cache($key) { $m = new Memcache(); $m->connect(CACHE_IP,CACHE_PORT); return $m->get($key); } function load_model($modelName) { return new Model($modelName); } function load_lib($folderName,$touchName) { require_once ‘Library/‘.$folderName.‘/‘.$touchName; } ?>
Lee.php
<?php function echo_para($split,$a,$b=0,$c=0,$d=0,$e=0,$f=0,$g=0,$h=0,$i=0,$j=0,$k=0,$l=0,$m=0,$n=0) { //获取参数总数 $n = func_num_args(); $str = ""; for($i = 1;$i< $n;$i++) { $str .= func_get_arg($i) . $split; } return $str; } function AJAX($Msg,$Result,$Status) { $arr = array("Msg" => $Msg,"Result" => $Result,"Status" => $Status); exit(json_encode($arr)); } function WriteLog($msg,$module = null,$logLevel = "DEBUG") { $filepath = "Log/"; if(!is_dir($filepath)) mkdir($filepath,‘0777‘); $MyLogFile = @fopen($filepath.date("Y-m-d").".txt",‘a+‘); $time = date("Y-m-d H:i:s"); if(isset($module)){$module = sprintf("\r\n归属模块:".$module."\r\n");} $logLine = "\r\n------------------------------- $time -------------------------------\r\n"; $logLine .= $module; $logLine .= "\r\n异常信息:$msg\r\n"; $logLine .= "\r\n错误等级:$logLevel\r\n"; fwrite($MyLogFile,$logLine); } //警告 function alert($str) { echo "<script type=‘text/javascript‘>alert(‘".$str."‘)</script>"; } //页面跳转 function href($url) { echo "<script type=‘text/javascript‘>window.location.href(‘".$url."‘)</script>"; } function Sub_截取字符串如果超出某位就省略号($str,$num) { if(mb_strlen($str) > $num) { $str = substr($str,0,$num)."..."; } return $str; } function mkFolder($path) { if(!is_readable($path)) { is_file($path) or mkdir($path); } } ?>
以上是关于php 个人函数库的主要内容,如果未能解决你的问题,请参考以下文章