代码如下 | 复制代码 |
<? //+------------------------+ //| pie3dfun.PHP//公用函数 | //+------------------------+ define("ANGLE_STEP", 5); //定义画椭圆弧时的角度步长 function draw_getdarkcolor($img,$clr) //求$clr对应的暗色 { $rgb = imagecolorsforindex($img,$clr); return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2); } function draw_getexy($a, $b, $d) //求角度$d对应的椭圆上的点坐标 { $d = deg2rad($d); return array(round($a*Cos($d)), round($b*Sin($d))); } function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr) //椭圆弧函数 { $n = ceil(($ed-$sd)/ANGLE_STEP); $d = $sd; list($x0,$y0) = draw_getexy($a,$b,$d); for($i=0; $i<$n; $i++) { $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP); list($x, $y) = draw_getexy($a, $b, $d); imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr); $x0 = $x; $y0 = $y; } } function draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) //画扇面 { $n = ceil(($ed-$sd)/ANGLE_STEP); $d = $sd; list($x0,$y0) = draw_getexy($a, $b, $d); imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr); for($i=0; $i<$n; $i++) { $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP); list($x, $y) = draw_getexy($a, $b, $d); imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr); $x0 = $x; $y0 = $y; } imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr); list($x, $y) = draw_getexy($a/2, $b/2, ($d+$sd)/2); imagefill($img, $x+$ox, $y+$oy, $clr); } function draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) //3d扇面 { draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr); if($sd<180) { list($R, $G, $B) = draw_getdarkcolor($img, $clr); $clr=imagecolorallocate($img, $R, $G, $B); if($ed>180) $ed = 180; list($sx, $sy) = draw_getexy($a,$b,$sd); $sx += $ox; $sy += $oy; list($ex, $ey) = draw_getexy($a, $b, $ed); $ex += $ox; $ey += $oy; imageline($img, $sx, $sy, $sx, $sy+$v, $clr); imageline($img, $ex, $ey, $ex, $ey+$v, $clr); draw_arc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr); list($sx, $sy) = draw_getexy($a, $b, ($sd+$ed)/2); $sy += $oy+$v/2; $sx += $ox; imagefill($img, $sx, $sy, $clr); } } function draw_getindexcolor($img, $clr) //RBG转索引色 { $R = ($clr>>16) & 0xff; $G = ($clr>>8)& 0xff; $B = ($clr) & 0xff; return imagecolorallocate($img, $R, $G, $B); } // 绘图主函数,并输出图片 // $datLst 为数据数组, $datLst 为标签数组, $datLst 为颜色数组 // 以上三个数组的维数应该相等 function draw_img($datLst,$labLst,$clrLst,$a=250,$b=120,$v=20,$font=10) { $ox = 5+$a; $oy = 5+$b; $fw = imagefontwidth($font); $fh = imagefontheight($font); $n = count($datLst);//数据项个数 $w = 10+$a*2; $h = 10+$b*2+$v+($fh+2)*$n; $img = imagecreate($w, $h); //转RGB为索引色 for($i=0; $i<$n; $i++) $clrLst[$i] = draw_getindexcolor($img,$clrLst[$i]); $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff); $clrt = imagecolorallocate($img, 0x00, 0x00, 0x00); //填充背景色 imagefill($img, 0, 0, $clrbk); //求和 $tot = 0; for($i=0; $i<$n; $i++) $tot += $datLst[$i]; $sd = 0; $ed = 0; 333 $ly = 10+$b*2+$v; for($i=0; $i<$n; $i++) { $sd = $ed; $ed += $datLst[$i]/$tot*360; //画圆饼 draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clrLst[$i]); //$sd,$ed,$clrLst[$i]); //画标签 imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrLst[$i]); imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt); //imagestring($img, $font, 5+2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)", $clrt); $str = iconv("GB2312", "UTF-8", $labLst[$i]); ImageTTFText($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "./simsun.ttf", $str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)"); $ly += $fh+2; } //输出图形 header("Content-type: image/png"); //输出生成的图片 $imgFileName = "../temp/".time().".png"; imagepng($img,$imgFileName); echo ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' } $datLst = array(30, 10, 20, 20, 10, 20, 10, 20); //数据 $labLst = array("中国科技大学", "安徽理工大学", "清华大学", "北京大学", "南京大学", "上海大学", "河海大学", "中山大学"); //标签 $clrLst = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); //画图 draw_img($datLst,$labLst,$clrLst); ?> |
效果图如下
代码如下 | 复制代码 |
if(isset($_COOKIE['sign'])){ <?php if(isset($_POST['username']) && isset($_POST['password'])){ if(empty($_COOKIE['sign'])){ <form method="post">
<?php <?php
|
计算执行的时间
通过下面这个简单的方法可以计算一段程序的执行时间(微妙)
代码如下 | 复制代码 |
$start_time = microtime(true); //一些需要计算时间的代码 print('代码的运行时间是:'.getExecTime($start_time)); function getExecTime($start_time) require_once 'Benchmark/Timer.php'; // A function that records the time when it is called // Return the times stored in profile, then erase it $profile[] = microtime(); // Set up a tick handler // Initialize the function before the declare block // Run a block of code, throw a tick every 2nd statement // Display the data stored in the profiler |
代码排错
主要介绍的是Advanced PHP Debugger(APD),通过设置可以生成跟踪文件,对文件进行分析可以得到脚本的详细信息
网站压力测试
人们常混淆压力测试和基准测试。基准测试是一种由单独的开发者完成的临时活动,常用Apache HTTP测试工具——ab,该工具可以测试一台HTTP服务器每秒能相应的请求数。压力测试是一种能中断你WEB应用程序的测试技术,通过对断点测试,能识别并修复应用程序中的弱点,为何时购置新硬件提供依据。常用的开源工具是Siege。
提速技巧
通过安装PHP加速器可以有效的提供PHP的执行速度,常见的三种加速器是Alternative PHP Cache(APC)、eAccelerator和ionCube PHP Accelerator(PHPA)。另外需要注意的是加速器的兼容性通常会滞后于新发布的PHP版本。
另外提速技巧是在能不使用正则的时候尽量不要用,通常可替代的方案会比使用正则效率更高。
要实现记住密码自动登录的功能我们大多数据都是利用了客户端的cookies来实现,我们利用php也不例外,有需要的朋友可以参考一下。php制作记住密码自动登录的解决思路,其实也就是对session,cookies的操作
//检查用户是否登录
代码如下 | 复制代码 |
function checklogin(){ if(empty($_SESSION['user_info'])){ //检查一下session是不是为空 if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){ //如果session为空,并且用户没有选择记录登录状 header("location:login.php?req_url=".$_SERVER['REQUEST_URI']); //转到登录页面,记录请求的url,登录后跳转过去,用户体验好。 }else{ //用户选择了记住登录状态 $user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //去取用户的个人资料 if(empty($user)){ //用户名密码不对没到取到信息,转到登录页面 header("location:login.php?req_url=".$_SERVER['REQUEST_URI']); }else{ $_SESSION['user_info'] = $user; //用户名和密码对了,把用户的个人资料放到session里面 } } } } |
二,用户提交登录信息
代码如下 | 复制代码 |
username = trim($_POST['username']); $password = md5(trim($_POST['password'])); $validatecode = $_POST['validateCode']; $ref_url = $_GET['req_url']; $remember = $_POST['remember']; $err_msg = ''; if($validatecode!=$_SESSION['checksum']){ $err_msg = "验证码不正确"; }elseif($username=='' || $password==''){ $err_msg = "用户名和密码都不能为空"; }else{ $row = getUserInfo($username,$password); if(empty($row)){ $err_msg = "用户名和密码都不正确"; }else{ $_SESSION['user_info'] = $row; if(!empty($remember)){ //如果用户选择了,记录登录状态就把用户名和加了密的密码放到cookie里面 setcookie("username", $username, time()+3600*24*365); setcookie("password", $password, time()+3600*24*365); } if(strpos($ref_url,"login.php") === false){ header("location:".$ref_url); }else{ header("location:main_user.php"); } } } |
三,当用户点退出时,清出记录登录状态
//退出登录
代码如下 | 复制代码 |
function logout(){ unset($_SESSION['user_info']); if(!empty($_COOKIE['username']) || !empty($_COOKIE['password'])){ setcookie("username", null, time()-3600*24*365); setcookie("password", null, time()-3600*24*365); } } |
代码如下 | 复制代码 |
<?php private $img_size; //取得图片的长宽 switch($imginfo[2]) switch($this->watertype) switch ($imginfo[2]) |