目录:
- 浏览器友好的变量输出
- 写日志
- 根据年月获取日期
- 返回浏览器发送Referer请求头
- 获取随机唯一id串数字
- 动态加密解密
- 凯撒加密解密
- 数组序列化成get参数
- 用户id加密解密
- 日期友好输出
- 获取网站根路径
- 高效生成随机密码
function dump($var, $echo=true, $label=null, $strict=true) {
$label = ($label === null) ? '' : rtrim($label) . ' ';
if (!$strict) {
if (ini_get('html_errors')) {
$output = print_r($var, true);
$output = "" . $label . htmlspecialchars($output, ENT_QUOTES) . "
";
} else {
$output = $label . " : " . print_r($var, true);
}
} else {
ob_start();
var_dump($var);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
$output = '' . $label . htmlspecialchars($output, ENT_QUOTES) . '
';
}
}
if ($echo) {
echo($output);
return null;
}else
return $output;
}
写日志
private function write_log($log){
header("Content-type: text/html; charset=utf-8");
/********************
1、写入内容到文件,追加内容到文件
2、打开并读取文件内容
********************/
$file = './logs/wx.log';//要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个
$content =date('Y-m-d H:i:s',time())."\n";
$content .= $log."\n\n";
// 这个函数支持版本(PHP 5)
$f = file_put_contents($file, $content,FILE_APPEND);
/*if($data = file_get_contents($file)){; // 这个函数支持版本(PHP 4 >= 4.3.0, PHP 5)
echo "写入文件的内容是:$data";
}*/
}
根据年月获取日期
/** * 根据年月获取日期 * */
public function getDayAction()
{
$year = $this->getRequest()->getGet('year');
$month = $this->getRequest()->getGet('month');
if(in_array($month, array(1,3,5,7,8,12)))
{
$max = 31;
}
elseif(in_array($month, array(4,6,9,10,11)))
{
$max = 30;
}
else
{
if(($year%400==0)||(($year%4==0)&&($year%100!=0)))
{
$max = 29;
}
else
{
$max = 28;
}
}
echo $max;exit;
}
返回浏览器发送Referer请求头
/**
* 返回浏览器发送Referer请求头
*
* 可以让服务器了解和追踪发出本次请求的起源URL地址
*
* @return string
*/
public function getUrlReferer() {
return $this->getServer('HTTP_REFERER');
}
# 获得主机信息,包含协议信息,主机名,访问端口信息
/** * 获得主机信息,包含协议信息,主机名,访问端口信息 * *
Example: * 请求: http://www.phpwind.net/example/index.php?a=test * 返回: http://www.phpwind.net/ ** @throws WindException 获取主机信息失败的时候抛出异常 */ private function _initHostInfo() { http= http = this->isSecure() ? ‘https’ : ‘http’; if (( httpHost= httpHost = this->getServer(‘HTTP_HOST’)) != null) this−>hostInfo= this->_hostInfo = http . ‘://’ . httpHost;elseif(( httpHost; elseif ((httpHost = this->getServer(‘SERVER_NAME’)) != null) {this->getServer(‘SERVER_NAME’)) != null) { this->_hostInfo = http.‘://′. http . ‘://’ . httpHost; if (( port= port = this->getServerPort()) != null) this−>hostInfo.=′:′. this->_hostInfo .= ':' . port; } else throw new WindException( CLASS . ’ determine the entry script URL failed!!’); }
# 获取随机唯一id串字母
//获取随机唯一id串
function keyGen($flag = false) {
$charid = md5(uniqid(mt_rand(), true));
$hyphen = chr(45); // "-"
$uuid = chr(123) // "{"
. substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12) .
chr(125); // "}"
if ($flag) {
return str_replace('-', '', $uuid);
}
return str_replace('-', '', substr($uuid, 1, -1));
}
获取随机唯一id串数字
function execOrder(){
return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 11);
}
动态加密解密
6.
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
// 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
$ckey_length = 4;
// 密匙
$key = md5 ( $key ? $key : 'yongge' );
// 密匙a会参与加解密
$keya = md5 ( substr ( $key, 0, 16 ) );
// 密匙b会用来做数据完整性验证
$keyb = md5 ( substr ( $key, 16, 16 ) );
// 密匙c用于变化生成的密文
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
// 参与运算的密匙
$cryptkey = $keya . md5 ( $keya . $keyc );
$key_length = strlen ( $cryptkey );
// 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),解密时会通过这个密匙验证数据完整性
// 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确
$string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
$string_length = strlen ( $string );
$result = '';
$box = range ( 0, 255 );
$rndkey = array ();
// 产生密匙簿
for($i = 0; $i 0 验证数据有效性
// substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) 验证数据完整性
// 验证数据有效性,请看未加密明文的格式
if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
return substr ( $result, 26 );
} else {
return '';
}
} else {
// 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
// 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码
return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
}
}
凯撒加密解密
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?