龚哥哥 爱生活、做自己!
PHP支付宝支付接口pc+wap MD5加密方式
发表于 2015-8-31 | 浏览(1830) | PHP
<?php

/**
 * 支付宝支付驱动
 * @author  Devil
 * @version V_1.0.0
 */
class AlipayLibrary
{
	/**
	 * [__construct 构造方法]
	 */
	public function __construct(){}

	/**
	 * [SoonPay 立即支付]
	 * @param [array] $data [支付信息]
	 */
	public function SoonPay($data, $config)
	{
		if(empty($data) || empty($config)) return false;

                //这里判断是否是手机访问,自己写一个方法接口。根据访问终端类型进行区分pc或wap
		if(IsMobile())
		{
			$this->SoonPayMobile($data, $config);
		} else {
			$this->SoonPayWeb($data, $config);
		}
	}

	/**
	 * [SoonPayMobile wap支付]
	 * @param  [array] $data   [参数列表]
	 * @param  [array] $config [配置信息]	
	 */
	private function SoonPayMobile($data, $config)
	{
		$request_token = $this->GetRequestToken($data, $config);

		$req_data = '<auth_and_execute_req><request_token>'.$request_token.'</request_token></auth_and_execute_req>';
		$parameter = array(
			'service'				=>	'alipay.wap.auth.authAndExecute',
			'format'				=>	'xml',
			'v'						=>	'2.0',
			'partner'				=>	$config['id'],
			'sec_id'				=>	'MD5',
			'req_data'				=>	$req_data,
			'request_token'			=>	$request_token
		);

		$param = $this->GetParamSign($parameter, $config);
		header('location:http://wappaygw.alipay.com/service/rest.htm?'.$param['param']. '&sign='.md5($param['sign']));

	}

	/**
	 * [GetRequestToken 获取临时token]
	 * @param  [array] $data   [参数列表]
	 * @param  [array] $config [配置信息]
	 * @return [string]        [返回临时token]
	 */
	private function GetRequestToken($data, $config)
	{
		$parameter = array(
			'service'				=>	'alipay.wap.trade.create.direct',
			'format'				=>	'xml',
			'v'						=>	'2.0',
			'partner'				=>	$config['id'],
			'req_id'				=>	$data['order_sn'],
			'sec_id'				=>	'MD5',
			'req_data'				=>	$this->GetReqData($data, $config),
			'subject'				=>	$data['name'],
			'out_trade_no'			=>	$data['order_sn'],
			'total_fee'				=>	$data['total_price'],
			'seller_account_name'	=>	$config['name'],
			'call_back_url'			=>	$data['call_back_url'],
			'notify_url'			=>	$data['notify_url'],
			'out_user'				=>	$data['out_user'],
			'merchant_url'			=>	$data['merchant_url'],
		);
		
		$param = $this->GetParamSign($parameter, $config);
        $ret = urldecode(file_get_contents('http://wappaygw.alipay.com/service/rest.htm?'.$param['param'].'&sign='.md5($param['sign'])));

        $para_split = explode('&',$ret);
        //把切割后的字符串数组变成变量与数值组合的数组
        foreach ($para_split as $item) {
            //获得第一个=字符的位置
            $nPos = strpos($item,'=');
            //获得字符串长度
            $nLen = strlen($item);
            //获得变量名
            $key = substr($item,0,$nPos);
            //获得数值
            $value = substr($item,$nPos+1,$nLen-$nPos-1);
            //放入数组中
            $para_text[$key] = $value;
        }

        $req = Xml_Array($para_text['res_data']);
        if(empty($req['request_token']))
        {
        	exit(header('location:'.__ROOT__.'index.php?g=Info&c=Prompt&f=PromptInfo&state=error&content=支付宝异常错误&url='.__ROOT__));
        }

        return $req['request_token'];
	}

	private function GetReqData($data, $config)
	{
		return '<direct_trade_create_req>
					<subject>'.$data['name'].'</subject>
					<out_trade_no>'.$data['order_sn'].'</out_trade_no>
					<total_fee>'.$data['total_price'].'</total_fee>
					<seller_account_name>'.$config['name'].'</seller_account_name>
					<call_back_url>'.$data['call_back_url'].'</call_back_url>
					<notify_url>'.$data['notify_url'].'</notify_url>
					<out_user>'.$data['out_user'].'</out_user>
					<merchant_url>'.$data['merchant_url'].'</merchant_url>
					<pay_expire>3600</pay_expire>
					<agent_id>0</agent_id>
				</direct_trade_create_req>';
	}

	/**
	 * [SoonPayWeb web支付]
	 * @param [array] $data   [订单信息]
	 * @param [array] $config [配置信息]
	 */
	private function SoonPayWeb($data, $config)
	{
        $parameter = array(
            'service'           => 'create_direct_pay_by_user',
            'partner'           => $config['id'],
            '_input_charset'    => ML_CHARSET,
            'notify_url'        => $data['notify_url'],
            'return_url'        => $data['call_back_url'],

            /* 业务参数 */
            'subject'           => $data['name'],
            'out_trade_no'      => $data['order_sn'],
            'price'             => $data['total_price'],

            'quantity'          => 1,
            'payment_type'      => 1,

            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',

            /* 买卖双方信息 */
            'seller_email'      => $config['name']
        );

        $param = $this->GetParamSign($parameter, $config);
		header('location:https://mapi.alipay.com/gateway.do?'.$param['param']. '&sign='.md5($param['sign']).'&sign_type=MD5');
	}

	/**
	 * [GetParamSign 生成参数和签名]
	 * @param  [array] $data   [待生成的参数]
	 * @param  [array] $config [配置信息]
	 * @return [array]         [生成好的参数和签名]
	 */
	private function GetParamSign($data, $config)
	{
        $param = '';
        $sign  = '';
        ksort($data);

		foreach($data AS $key => $val)
        {
            $param .= "$key=" .urlencode($val). "&";
            $sign  .= "$key=$val&";
        }

        return array(
        	'param'	=>	substr($param, 0, -1),
        	'sign'	=>	substr($sign, 0, -1).$config['key']
        );
	}

	/**
	 * [Respond 异步处理]
	 * @param  [array] $config [配置信息]
	 * @return [array|string] [成功返回数据列表,失败返回no]
	 */
	public function Respond($config)
	{
		if(empty($config)) return 'no';

        $data = empty($_POST) ? $_GET :  array_merge($_GET, $_POST);
		ksort($data);

		$sign = '';
		$sec = isset($data['sec_id']) ? $data['sec_id'] : '';
		if($sec == 'MD5')
		{
            $data_xml = json_decode(json_encode((array) simplexml_load_string($data['notify_data'])), true);
            $data = array_merge($data, $data_xml);
            $sign = 'service='.$data['service'].'&v='.$data['v'].'&sec_id='.$data['sec_id'].'&notify_data='.$data['notify_data'];
		} else {
	        foreach($data AS $key=>$val)
	        {
	            if ($key != 'sign' && $key != 'sign_type' && $key != 'code')
	            {
	                $sign .= "$key=$val&";
	            }
	        }
	        $sign = substr($sign, 0, -1);
	    }
        if(!isset($data['sign']) || md5($sign.$config['key']) != $data['sign']) return 'no';

        /* 支付状态 */
        $state = isset($data['trade_status']) ? $data['trade_status'] : $data['result'];
        switch($state)
        {
            case 'TRADE_SUCCESS':
                return $data;
                break;

            case 'TRADE_FINISHED':
                return $data;
                break;

               case 'success':
                return $data;
                break;
        }
        return 'no';
	}

}
?>

阅读全文

PHP图片上传
发表于 2015-8-31 | 浏览(607) | PHP
<?php

/**
 * 图片上传驱动
 * @author 	Devil
 * @version 1.0.0
 */
class ImageLibrary
{
	private $i_path;
	private $i_type;
	private $i_is_new_name;
	private $i_quality;
	private $i_data;

	/**
	 * [__construct 构造方法]
	 * @param [array] $parameters [参数列表]
	 */
	private function __construct($parameters)
	{
		/* 检测是否支持gd */
		$this->IsGD();

		/* 图片类型 */
		$this->i_type = empty($parameters['type']) ? $this->GetImageType() : $parameters['type'];

		/* 图片名称是否使用新名称, 则使用原图片名称 */
		$this->i_is_new_name = (isset($parameters['is_new_name']) && is_bool($parameters['is_new_name'])) ? $parameters['is_new_name'] : true;

		/* jpg图片的质量值, 值越大质量越好 */
		$this->i_quality = max(75, isset($parameters['quality']) ? intval($parameters['quality']) : 75);
	}

	/**
	 * [Instance 静态方法实例化本类]
	 * @return [object] [实例对象]
	 */
	public static function Instance($parameters = array())
	{
		static $object = null;
		if(!is_object($object)) $object = new self($parameters);
		return $object;
	}

	/**
	 * [SetParameters 参数设置]
	 * @param [array] $parameters [参数列表]
	 */
	public function SetParameters($parameters = array())
	{
		if(empty($parameters)) return;
		$this->__construct($parameters);
	}

	/**
	 * [Is_GD 检查是否支持gd库]
	 */
	private function IsGD()
	{
		if(!isset(gd_info()['GD Version']) || empty(gd_info()['GD Version'])) Api_Return(L('code_412'), 412);
	}

	/**
	 * [GetImageType 获取图片后缀类型]
	 * @return [array] [图片后缀类型]
	 */
	private function GetImageType()
	{
		return array(
			'gif'	=> 'image/gif',
			'png'	=> 'image/png',
			'jpg'	=> 'image/jpeg',
			'bmp'	=> 'image/bmp'
		);
	}

	/**
	 * [Initialization 基本信息初始化]
	 * @param  [array]   $data [临时图片数据]
	 * @param  [string]  $path [图片保存路径]
	 * @return [boolean]	   [true或false]
	 */
	private function Initialization($data, $path)
	{
		if(empty($data)) return false;

		/* 图片保存地址 */
		$path .= (substr($path, -1) == '/') ? '' : '/';
		$this->i_path = empty($path) ? '/tmp/image' : $path;

		/* 设置存储路径是否存在 */
		if(!is_dir($this->i_path)) @mkdir($this->i_path, 0777, true);
		if(!@opendir($this->i_path)) exit('dir not access');

		/* 初始化返回数据 */
		$this->i_data = array();

		return true;
	}

	/**
	 * [GetNewFileName 获取随机名称]
	 * @return [string] [新的名称]
	 */
	private function GetNewFileName()
	{
		return date('YmdHis').str_shuffle(rand());
	}

	/**
	 * [SaveBaseImages base64图片存储]
	 * @param  [array] $data  [需要存储的base数据, 一维数组]
	 * @param  [string] $path [存储路径]
	 * @return [array] 		  [返回图片地址, 一维数组]
	 */
	public function SaveBaseImages($data, $path)
	{
		if(!$this->Initialization($data, $path)) return null;

		for($i=0; $i<count($data); $i++)
		{
			if(empty($data[$i])) continue;

			$temp_img = str_replace(array("\n", '\n', ' '), '', $data[$i]);
			$img_info = @getimagesize('data://application/octet-stream;base64,'.$temp_img);
			if(empty($img_info['mime'])) continue;

			$type = $this->Is_ImageType($img_info['mime']);
			if($type == 'no') continue;

			$file_name = $this->GetNewFileName().'.'.$type;
			if(file_put_contents($this->i_path.$file_name, base64_decode($temp_img)) !== false) $this->i_data[] = $file_name;
		}
		return $this->i_data;
	}

	/**
	 * [SaveBinaryImages 二进制图片保存]
	 * @param  [array]  $data [二进制图片数据, 一维数组]
	 * @param  [string] $path [存储路径]
	 * @return [array] 		  [返回图片地址, 一维数组]
	 */
	public function SaveBinaryImages($data, $path)
	{
		if(!$this->Initialization($data, $path)) return null;

		for($i=0; $i<count($data); $i++)
		{
			if(empty($data[$i])) continue;

			$img_info = getimagesizefromstring($data[$i]);
			if(empty($img_info['mime'])) continue;

			$type = $this->Is_ImageType($img_info['mime']);
			if($type == 'no') continue;

			$file_name = $this->GetNewFileName().'.'.$type;
			if(file_put_contents($this->i_path.$file_name, $data) !== false) $this->i_data[] = $file_name;
		}
		return $this->i_data;
	}

	/**
	 * [GetOriginal 获取临时图片文件的原图]
	 * @param  [array]  $data [临时图片数据]
	 * @param  [string] $path [存储路径]
	 * @return [string] 	  [返回图片地址]
	 */
	public function GetOriginal($data, $path)
	{
		if(!$this->Initialization($data, $path)) return '';

		if(empty($data['tmp_name'])) return '';

		$type = $this->Is_ImageType($data['type']);
		if($type == 'no') return '';

		$file_name = $this->GetNewFileName().'.'.$type;

		if(move_uploaded_file($data['tmp_name'], $this->i_path.$file_name)) return $file_name;
		return '';
	}

	/**
	 * [GetBinaryCompress 获取指定图片路径的压缩图]
	 * @param  [string] $file  [图片地址]
	 * @param  [string] $path  [存储路径]
	 * @param  [int] 	$width [设定图片宽度]
	 * @param  [int] 	$height[指定图片高度, 不指定则高度按照比例自动计算]
	 * @return [string] 	   [返回图片地址]
	 */
	public function GetBinaryCompress($file, $path, $width = 0, $height = 0)
	{
		if(!$this->Initialization($file, $path) || is_array($file) || !file_exists($file)) return '';

		$img_info = pathinfo($file);
		if(empty($img_info['basename'])) return '';

		$type = $this->Is_ImageType($img_info['extension']);
		if($type == 'no') return '';

		$file_name = ($this->i_is_new_name) ? $this->GetNewFileName().'.'.$type : $img_info['basename'];

		if($this->ImageCompress($width, $height, $file, $type, $this->i_path.$file_name)) return $file_name;
		
		return '';
	}

	/**
	 * [GetCompress 获取临时图片文件的压缩图]
	 * @param  [array]  $data  [临时图片数据]
	 * @param  [string] $path  [存储路径]
	 * @param  [int] 	$width [设定图片宽度]
	 * @param  [int] 	$height[指定图片高度, 不指定则高度按照比例自动计算]
	 * @return [atring|空字符串] [返回图片存储的名称]
	 */
	public function GetCompress($data, $path, $width = 0, $height = 0)
	{
		if(!$this->Initialization($data, $path)) return '';

		if(empty($data['tmp_name'])) return '';
		
		$type = $this->Is_ImageType($data['type']);
		if($type == 'no') return '';

		$file_name = $this->GetNewFileName().'.'.$type;

		if($this->ImageCompress($width, $height, $data['tmp_name'], $type, $this->i_path.$file_name)) return $file_name;
		return '';
	}

	/**
	 * [GetCompressCut 临时图像裁剪]
	 * @param [array]  	$data       [图像临时数据]
	 * @param [string] 	$path       [图像存储地址]
	 * @param [int] 	$width      [指定存储宽度]
	 * @param [ing] 	$height     [指定存储高度]
	 * @param [ing] 	$src_x      [裁剪x坐标]
	 * @param [ing] 	$src_y      [裁剪y坐标]
	 * @param [ing] 	$src_width  [裁剪区域宽度]
	 * @param [ing] 	$src_height [裁剪区域高度]
	 * @return [atring|空字符串] [返回图片存储的名称]
	 */
	function GetCompressCut($data, $path, $width = 0, $height = 0, $src_x = 0, $src_y = 0, $src_width = 0, $src_height = 0)
	{
		if(!$this->Initialization($data, $path)) return '';

		if(empty($data['tmp_name'])) return '';
		
		$type = $this->Is_ImageType($data['type']);
		if($type == 'no') return '';

		$file_name = $this->GetNewFileName().'.'.$type;

		if($this->ImageCompress($width, $height, $data['tmp_name'], $type, $this->i_path.$file_name, $src_x, $src_y, $src_width, $src_height)) return $file_name;
		return '';
	}

	/**
	 * [ImageCompress 图片压缩]
	 * @param  [int] 	$width [指定图片宽度]
	 * @param  [int] 	$height[指定图片高度]
	 * @param  [string] $file  [原图片地址]
	 * @param  [string] $type  [类型]
	 * @param  [string] $path  [新图片地址]
	 * @return [boolean] 	   [成功true, 失败false]
	 */
	private function ImageCompress($width, $height, $file, $type, $path, $src_x = 0, $src_y = 0, $src_width = 0, $src_height = 0)
	{
		/* 获取图片原本尺寸 */
		list($w, $h) = getimagesize($file);
		
		/* 尺寸计算 */
		$new_width = ($width > 0 && $w > $width) ? $width : $w;
		$new_height = ($width > 0 && $w > $width) ? (round($h/($w/$width))) : $h;
		if($width > 0 && $height > 0) $new_width = $width;
		if($height > 0) $new_height = $height;

		/* url创建一个新图象 */
		switch($type)
	    {
	        case 'gif':
	            $src_im = @imagecreatefromgif($file);
	            break;
	        case 'png':
	            $src_im = @imagecreatefrompng($file);
	            break;
	        default:
	            $src_im = @imagecreatefromjpeg($file);
	    }
	    if(!$src_im) return;

	    /* 新建一个真彩色图像 */
	    $dst_im = imagecreatetruecolor($new_width, $new_height);

	    /* 是否裁剪图片 */
	    if($src_width > 0 && $src_height > 0)
	    {
	    	/* 新建拷贝大小的真彩图像 */
	    	$cpd_im = imagecreatetruecolor($src_width, $src_height);

	    	/* 拷贝图片 */
		    imagecopy($cpd_im, $src_im, 0, 0, $src_x, $src_y, $src_width, $src_height);

		    /* 图片缩放 */
		    $s = imagecopyresampled($dst_im, $cpd_im, 0, 0, 0, 0, $new_width, $new_height, $src_width, $src_height);
		} else {
			/* 图片缩放 */
	    	$s = imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
		}
	    
	    if($s)
	    {
		    switch($type)
		    {
		        case 'png':
		            imagepng($dst_im, $path);
		            break;
		        case 'gif':
		            imagegif($dst_im, $path);
		            break;
		        default:
		            imagejpeg($dst_im, $path, $this->i_quality);
		    }
		}
		imagedestroy($dst_im);
		imagedestroy($src_im);

		return $s;
	}

	/**
	 * [Is_ImageType 验证后缀名是否合法]
	 * @param  [string] $image_type [图片后缀类型]
	 * @return [string] 			[后缀名或no]
	 */
	private function Is_ImageType($image_type)
	{
		if(empty($image_type)) return 'no';
		if(array_key_exists($image_type, $this->i_type)) return $image_type;

		foreach($this->i_type as $key=>$val)
		{
			if($val == $image_type) return $key;
		}
		return 'no';
	}
}
?>

阅读全文

PHP Memcached操作类
发表于 2015-8-31 | 浏览(670) | PHP
<?php

/**
 * 缓存驱动
 * @author 	Devil
 * @version 1.0.0
 */
class CacheLibrary
{
	private $c_obj;
	private $c_time;
	/**
	 * [__construct 构造方法]
	 */
	public function __construct($host, $port = 11211)
	{
		/* 实例化memcached */
		$this->c_obj = new Memcached();
		if(!$this->c_obj->addServer($host, $port)) Api_Return(L('code_413'), 413);

		/* 基础参数设置 */
		$this->c_time = empty(C('cache')['time']) ? 0 : C('cache')['time'];
	}

	/**
	 * [GetTime 获取缓存时间]
	 * @return [int] [缓存时间]
	 */
	private function GetTime($time = 0)
	{
		return (intval($time) > 0) ? intval($time) : $this->c_time;
	}

	/**
	 * [Add 缓存添加]
	 * @param  [string]  	$key  [索引]
	 * @param  [mixed]  	$val  [值]
	 * @param  [integer] 	$time [过期时间(单位秒), 0永久, 或时间戳]
	 * @return [boolean] 	[成功true, 失败false]
	 */
	public function Add($key, $val, $time = 0)
	{
		return $this->c_obj->add($key, $val, $this->GetTime($time));
	}

	/**
	 * [Set 缓存替换]
	 * @param  [string]  	$key  [索引]
	 * @param  [mixed]  	$val  [值]
	 * @param  [integer] 	$time [过期时间(单位秒), 0永久, 或时间戳]
	 * @return [boolean] 	[成功true, 失败false]
	 */
	public function Set($key, $val, $time = 0)
	{
		return $this->c_obj->set($key, $val, $this->GetTime($time));
	}

	/**
	 * [SetMulti 设置多个索引的缓存数据]
	 * @param [type] $key_all [索引数组]
	 * @param  [integer] 	  $time [过期时间(单位秒), 0永久, 或时间戳]
	 * @return [boolean] 	  [成功true, 失败false]
	 */
	public function SetMulti($key_all, $time = 0)
	{
		return $this->c_obj->setMulti($key_all, $time);
	}

	/**
	 * [Append 向已存在索引后面追加数据]
	 * @param [string] $key [索引]
	 * @param [string] $val [追加的数据(字符串)]
	 */
	public function Append($key, $val)
	{
		$this->setOption();
		return $this->c_obj->append($key, $val);
	}

	/**
	 * [Prepend 向已存在索引前面追加数据]
	 * @param [string] $key [索引]
	 * @param [string] $val [追加的数据(字符串)]
	 */
	public function Prepend($key, $val)
	{
		$this->setOption();
		return $this->c_obj->prepend($key, $val);
	}

	/**
	 * [Replace 替换已存在索引下的元素]
	 * @param  [string]  	$key  [索引]
	 * @param  [mixed]  	$val  [值]
	 * @param  [integer] 	$time [过期时间(单位秒), 0永久, 或时间戳]
	 * @return [boolean] 	[成功true, 失败false]
	 */
	public function Replace($key, $val, $time = 0)
	{
		$this->c_obj->replace($key, $val, $time);
	}

	/**
	 * [setOption 设置选项]
	 */
	private function setOption()
	{
		$this->c_obj->setOption(Memcached::OPT_COMPRESSION, false);
	}

	/**
	 * [Fetch 抓取下一个结果]
	 * @param [string]  $key_all [索引名]
	 * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
	 */
	public function Fetch($key_all, $cas = false)
	{
		$this->GetDelayed($key_all, $cas);
		return $this->c_obj->fetch();
	}

	/**
	 * [FetchAll 抓取所有剩余的结果]
	 * @param [string]  $key_all [索引名]
	 * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
	 */
	public function FetchAll($key_all, $cas = false)
	{
		$this->GetDelayed($key_all, $cas);
		return $this->c_obj->fetchAll();
	}

	/**
	 * [GetDelayed 请求多个索引]
	 * @param [string]  $key_all [索引名]
	 * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
	 */
	private function GetDelayed($key_all, $cas)
	{
		$this->c_obj->getDelayed($key_all, $cas);
	}

	/**
	 * [Get 缓存获取]
	 * @param  [string]  $key  [索引]
	 * @return [mixed]   [当前索引对应的数据]
	 */
	public function Get($key)
	{
		return $this->c_obj->get($key);
	}

	/**
	 * [GetMulti 获取多个索引的缓存数据]
	 * @param [type] $key_all [索引数组]
	 */
	public function GetMulti($key_all)
	{
		return $this->c_obj->getMulti($key_all);
	}

	/**
	 * [Delete 删除一个索引]
	 * @param  [string]  $key [索引]
	 * @param  [integer] $time [等待时间(单位秒), 0立即, 或时间戳]
	 * @return [boolean] [成功true, 失败false]
	 */
	public function Delete($key, $time = 0)
	{
		return $this->c_obj->delete($key, $time);
	}

	/**
	 * [DeleteMulti 删除多个索引]
	 * @param  [array]            $key_all     [索引数组]
	 * @param  [integer] 	      $time [过期时间(单位秒), 0立即, 或时间戳]
	 * @return [boolean或array] 	  [成功true, 失败false]
	 */
	public function DeleteMulti($key_all, $time = 0)
	{
		$s = $this->c_obj->deleteMulti($key_all, $time);
		if(empty($s)) return false;
		foreach($s as $key=>$val)
		{
			if($val !== true) if($this->Get($key) == false) $s[$key] = true;
		}
		return $s;
	}

	/**
	 * [Flush 作废所有元素]
	 * @param  [integer] $time [等待时间(单位秒), 0立即]
	 * @return [boolean] [成功true, 失败false]
	 */
	public function Flush($time = 0)
	{
		return $this->c_obj->flush($time);
	}
}
?>

阅读全文

PHP生成分页类 前端基于amazeui
发表于 2015-8-31 | 浏览(937) | PHP
<?php

/**
 * 分页驱动
 * @author  Devil
 * @version v_1.0.0
 */
class PageLibrary
{
	private $page;
	private $total;
	private $number;
	private $bt_number;
	private $where;
	private $page_total;
	private $url;
	private $html;

	/**
	 * [__construct description]
	 * @param [int]    $param['total'] 		[数据总数]
	 * @param [int]    $param['number'] 	[每页数据条数]
	 * @param [int]    $param['bt_number'] 	[分页显示按钮个数]
	 * @param [array]  $param['where'] 		[额外条件(键值对)]
	 * @param [string] $param['url'] 		[url地址]
	 */
	public function __construct($param = array())
	{
		$this->page = max(1, isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1);
		$this->total = max(1, isset($param['total']) ? intval($param['total']) : 1);
		$this->number = max(1, isset($param['number']) ? intval($param['number']) : 1);
		$this->bt_number = isset($param['bt_number']) ? intval($param['bt_number']) : 2;
		$this->where = (isset($param['where']) && is_array($param['where'])) ? $param['where'] : '';
		$this->url = isset($param['url']) ? $param['url'] : '';
		$this->page_total = 1;
		$this->html = '';

		/* 参数设置 */
		$this->SetParem();
	}

	/**
	 * [SetParem 参数设置]
	 */
	private function SetParem()
	{
		/* 防止超出最大页码数 */
		$this->page_total = ceil($this->total/$this->number);
		if($this->page > $this->page_total) $this->page = $this->page_total;

		/* 额外条件url设置 */
		if(!empty($this->where) && is_array($this->where))
		{
			foreach($this->where as $k=>$v) $this->url .= '&'.$k.'='.$v;
		}
		
	}

	/**
	 * [GetPageHtml 获取生成好的分页代码]
	 */
	public function GetPageHtml()
	{
		$this->html .= '<ul class="am-pagination am-pagination-centered"><li';
		$this->html .= ($this->page > 1) ? '' : ' class="am-disabled"';
		$this->html .= '><a href="'.$this->url.'&page='.($this->page-1).'" class="meila-radius">&laquo;</a></li>';
		$this->html .= $this->GetButtonNumberHtml();
		$this->html .= '<li';
		$this->html .= ($this->page > 0 && $this->page < $this->page_total) ? '' : ' class="am-disabled"';
		$this->html .= '><a href="'.$this->url.'&page='.($this->page+1).'" class="meila-radius">&raquo;</a></li></ul>';

		return $this->html;
	}

	/**
	 * [GetButtonNumberHtml 获取button显示个数的html]
	 * @return [string] [按钮个数html代码]
	 */
	private function GetButtonNumberHtml()
	{
		$html_before = '';
		$html_after = '';
		$html_page = '<li class="am-active"><a class="meila-radius">'.$this->page.'</a></li>';
		if($this->bt_number > 0)
		{
			/* 前按钮 */
			if($this->page > 1)
			{
				$total = ($this->page-$this->bt_number < 1) ? 1 : $this->page-$this->bt_number;
				for($i=$this->page-1; $i>=$total; $i--)
				{
					$html_before = '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>'.$html_before;
				}
			}

			/* 后按钮 */
			if($this->page_total > $this->page)
			{
				$total = ($this->page+$this->bt_number > $this->page_total) ? $this->page_total : $this->page+$this->bt_number;
				for($i=$this->page+1; $i<=$total; $i++)
				{
					$html_after .= '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>';
				}
			}
		}
		return $html_before.$html_page.$html_after;
	}

	/**
	 * [GetPageStarNumber 获取分页起始值]
	 */
	public function GetPageStarNumber()
	{
		return intval(($this->page-1)*$this->number);
	}
}
?>

阅读全文

Linux下使用webbench测试服务器压力
发表于 2015-8-31 | 浏览(582) | 服务器

webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便。

适用系统:Linux

1;安装

360云盘下载地址:

    http://yunpan.cn/ccQeU9IE5cRU7 (提取码:74e2)

wget http://blog.zyan.cc/soft/linux/webbench/webbench-1.5.tar.gz
tar zxvf webbench-1.5.tar.gz
cd webbench-1.5
make && make install

2;使用

webbench -c 500 -t 30 http://mei.la/

参数说明
  -c表示并发数,-t表示时间(秒)

3;测试结果

Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://mei.la/
500 clients, running 30 sec.

Speed=3230 pages/min, 11614212 bytes/sec.
Requests: 1615 susceed, 0 failed.

阅读全文

ubuntu下MySQL停止或启动失败
发表于 2015-8-31 | 浏览(501) | 服务器

有些时候遇到一些奇怪的问题就是mysql不管是启动还是停止都失败。

在这里先看看进程

ps -ef | grep mysql

根据进程号直接杀死进程

kill 27362 29144 29173

然后再停止或启动操作即可

/etc/init.d/mysql stop                停止
/etc/init.d/mysql start               启动
/etc/init.d/mysql restart             重启

阅读全文

win安装PHP的Redis扩展
发表于 2015-8-31 | 浏览(597) | 服务器

在win下安装php的redis扩展非常的简单,下载一个.dll扩展包放到php的ext目录下,在php.ini里边添加一行配置就可以了。

下载扩展包:

下载地址:https://github.com/nicolasff/phpredis/downloads

安装扩展包

解压后把dll放到php的ext目录下,打开php.ini 添加
extension=php_igbinary.dll
extension=php_redis.dll

检测是否安装成功,可以打开phpinfo看下
安装成功后,php测试

安装完成,进行测试

vim redis_test.php
<?php
  $r = new Redis();
  $r->connect('127.0.0.1', 6379);
  $r->set('devil', 'Hello World!', 10);
  echo $r->get('devil');
?>

阅读全文

CentOS安装PHP的Redis扩展
发表于 2015-8-31 | 浏览(606) | 服务器

1;下载扩展包

wget https://github.com/nicolasff/phpredis/archive/master.zip

360云盘下载地址:

http://yunpan.cn/ccXwiNS2B5Hpi  访问密码 2970

2;解压扩展压缩包

unzip master.zip

3;进入安装包

cd phpredis-master

4;安装php-devel,fastcgi模式,phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块

yum install php-devel

5;编译安装

phpize
./configure
make
make install

6;修改php.ini

vim /etc/php.ini 添加 extension=redis.so

阅读全文

CentOS安装Redis服务
发表于 2015-8-31 | 浏览(699) | 服务器

1;下载安装包

官网地址:http://www.redis.io/download

下载地址:http://download.redis.io/releases/redis-3.0.3.tar.gz

wget http://download.redis.io/releases/redis-3.0.3.tar.gz

2;解压

tar -zxvf redis-3.0.3.tar.gz
cd redis-3.0.3

3;编译

make
make && install

4;创建开机启动

vim /etc/init.d/redis-start 保存
/data/soft/redis-3.0.3/src/redis-server &

vim /etc/init.d/redis-stop 保存
/data/soft/redis-3.0.3/src/redis-cli shutdown

vim /etc/init.d/redis-cli 保存
/data/soft/redis-3.0.3/src/redis-cli
chmod 755 /etc/init.d/redis-*
echo "/etc/init.d/redis-start" >> /ect/rc.local

阅读全文

PHP localhost连接不上MySQL
发表于 2015-8-31 | 浏览(617) | PHP

连接MySQL数据库有两种方式:TCP/IP(一般理解的端口的那种)和Unix套接字(一般叫socket或者sock)。大部分情况下,可以用localhost代表本机127.0.0.1,但是在MySQL连接时,二者不可混用,而且MySQL中权限设置中localhost与127.0.0.1也是分开设置的。当设置为127.0.0.1时,系统通过TCP/IP方式连接数据库;当设置为localhost时,系统通过socket方式连接数据库。

找到mysql.sock所在位置,常在目录 tmp , var/run/mysqld

修改 php.ini

pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

mac重启apache
linux重启php-fpm

阅读全文

TOP