/*
*功能:对字符串进行加密处理
*参数一:需要加密的内容
*参数二:密钥
*/
function passport_encrypt($str,$key){ //加密函数
srand((double)microtime() * 1000000);
$encrypt_key=md5(rand(0, 32000));
$ctr=0;
$tmp='';
for($i=0;$i<strlen($str);$i++){
$ctr=$ctr==strlen($encrypt_key)?0:$ctr;
$tmp.=$encrypt_key[$ctr].($str[$i] ^ $encrypt_key[$ctr++]);
}
return base64_encode(passport_key($tmp,$key));
}
/*
*功能:对字符串进行解密处理
*参数一:需要解密的密文
*参数二:密钥
*/
function passport_decrypt($str,$key){ //解密函数
$str=passport_key(base64_decode($str),$key);
$tmp='';
for($i=0;$i<strlen($str);$i++){
$md5=$str[$i];
$tmp.=$str[++$i] ^ $md5;
}
return $tmp;
}
/*
*辅助函数
*/
function passport_key($str,$encrypt_key){
$encrypt_key=md5($encrypt_key);
$ctr=0;
$tmp='';
for($i=0;$i<strlen($str);$i++){
$ctr=$ctr==strlen($encrypt_key)?0:$ctr;
$tmp.=$str[$i] ^ $encrypt_key[$ctr++];
}
return $tmp;
}
//纯文本的utf8编码
header("Content-Type: text/plain;charset=utf8");
//html的utf8编码
header("Content-Type: text/html;charset=utf8");
//纯文本的gb2312编码
header("Content-Type: text/plain;charset=gb2312");
//html的gb2312编码
header("Content-Type: text/html;charset=gb2312");
时常会遇到根据用户的IP获取用户所在的城市,如“欢迎来自北京的张三”。所以有了下面的程序。
<?php
/*
* 获取用户的IP地址
*/
header("Content-Type: text/plain;charset=gb2312");
function get_online_ip($format = 0) {
if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
$onlineip = getenv ( 'HTTP_CLIENT_IP' );
} elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
$onlineip = getenv ( 'HTTP_X_FORWARDED_FOR' );
} elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
$onlineip = getenv ( 'REMOTE_ADDR' );
} elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
$onlineip = $_SERVER ['REMOTE_ADDR'];
}
preg_match ( "/[\d\.]{7,15}/", $onlineip, $onlineipmatches );
$onlineip = $onlineipmatches [0] ? $onlineipmatches [0] : 'unknown';
if ($format) {
$ips = explode ( '.', $onlineip );
for($i = 0; $i < 3; $i ++) {
$ips [$i] = intval ( $ips [$i] );
}
return sprintf ( 'ddd', $ips [0], $ips [1], $ips [2] );
} else {
return $onlineip;
}
}
/**
*用php的curl函数从网路上获取详细IP对应的地理未知
*/
function lazdf($ip){
$url = "http://www.ip138.com/ips138.asp?ip=".$ip;
$ipdz = http_request($url);
preg_match("/<ul class=\"ul1\"><li>(.*?)<\/li>/i",$ipdz['body'],$jgarray);
preg_match('/本站主数据:(.*)/', $jgarray[1], $place);
echo "欢迎来自".$place[1]."的朋友";
}
/**
* CURL封装
*/
function http_request($url,$post_data = array(),$header = array()) {
$post_data = http_build_query($post_data);
$header = $header ? $header : array ("Content-type: application/x-www-form-urlencoded; charset=gb2312");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$response = curl_exec($ch);
if(curl_errno($ch))
{
$response = "ERROR:".curl_error($ch);
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('body'=>$response,'status'=>$status) ;
}
lazdf(get_online_ip());
水平居中
#layout {text-align: center;}
#center { margin-left: auto; margin-right: auto; }
垂直居中
一、单行内容的居中
只考虑单行是最简单的,无论是否给容器固定高度,只要给容器设置 line-height 和 height,并使两值相等,再加上 over-flow: hidden 就可以了
line-height:35px;height:35px;overflow:hidden;
优点:
1. 同时支持块级和内联极元素
2. 支持所有浏览器
缺点:
1. 只能显示一行
2. IE中不支持等的居中
要注意的是:
1. 使用相对高度定义你的 height 和 line-height
2. 不想毁了你的布局的话,overflow: hidden 一定要
二、多行内容居中,且容器高度可变也很简单,给出一致的 padding-bottom 和 padding-top 就行
#nav li{float:left;margin:0 10px;padding-top:10px;padding-bottom:10px;}
二、利用line-height和text-align:center结合
#nav li{float:left;margin:0 10px;line-height:30px; text-align:center;}