请大家把自己的经常用的技巧和常用函数贴上来,方便大家参考和使用!(贴者有分) 支持!N久没写PHP,手头也没什么好东东,~_~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我通常把一些每个页面比较常用到的都独立成一个文件,比如:一个通用的数据操作类一个session验证程序等。然后在需要的页面include进去,这样的并不算上面技巧吧不过一般来说我的整站程序所作的数据库操作都是用一个类来作的,无论是数据库连接、查询、更新等 我改编的月历函数:<?/*月历函数array calendar(year,month)输出周期为星期一到星期天(not sunday to saturday)如果1号是星期n(n>1),则数组前面空出n-1个位置*/function calendar($year,$month,$style="world"){ if(empty($year)){$year=date('Y',time());} if(empty($month)){$month=date('m',time());} $return = array(); #重新设定一些变量 $date=01; #当月的天数 $day=01; #循环中的日 #算出当月的天数 while (checkdate($month,$date,$year)){ $date++; } $no=date('w', mktime(0,0,0,$month,$day,$year)); if ($no == 0){ $no=7; } if($style == "world"){ $no=$no-1; } else{ $no=$no-2; } for($i=0;$i<=$no;$i++){ $return[$i]=""; } for($i=1;$i<$date;$i++){ $return[$i+$no]=$i; } return $return;}#end function calendar?> 我写的生成随机字符的函数:/*随机字符串生成调用格式:$password = rndstr(10,int type); 生成一个10位的随机密码*/function rndstr($length,$type=0) { $hash = ''; switch($type){ case 0:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';break; case 1:$chars = '0123456789';break; case 2:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';break; case 3:$chars = 'abcdefghijklmnopqrstuvwxyz';break; case 4:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';break; case 5:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';break; case 6:$chars = '0123456789abcdefghijklmnopqrstuvwxyz';break; case 7:$chars = 'ABCEFGHJKLMNTWXYZ2346789';break; default:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';break; } $max = strlen($chars) - 1; mt_srand((double)microtime() * 1000000); for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; }#end function rndstr <?/*生成4位的验证码图片需要结合rndstr函数(string.func.php)使用说明:直接调用imgauth(string authcode)*/function imgauth($authcode="ERR!"){ Header("Content-type: image/PNG"); srand((double)microtime()*1000000); $im = imagecreate(45,20); $black=ImageColorAllocate($im, 0,0,0); $backgnd=array( 1=>ImageColorAllocate($im, 230,230,0), 2=>ImageColorAllocate($im, 230,230,230), 3=>ImageColorAllocate($im, 0,230,230), 4=>ImageColorAllocate($im, 250,150,250), ); $background=$backgnd[rand(1,4)]; imagefill($im,0,0,$background); imagestring($im, 5, 4, 3, $authcode, $black); for($i=0;$i<100;$i++){ imagesetpixel($im, rand()%70 , rand()%30 , $background); } ImagePNG($im); ImageDestroy($im);}#end function imgauth?>以上函数需要结合我上面的生成随机字符串的函数这样就可以实现验证码的生成建立auth.php:<?include "func.string.php";include "func.image.php";$authcode=rndstr(4,7); #注意,type设定为7,因为用来显示的字母必须没有异议,比如1和I就不行imgauth($authcode);?>然后在其它页面调用<img src="auth.php">对于验证码的保存,可以使用SESSION或者COOKIE或者数据库存储 俺的文件上传ftp的:<?php $ftp_server="ftp服务器(最好用IP)"; $ftp_username="ftp用户名"; $ftp_password="登录密码"; $ftp_dir="目录(如设置为 /)"; if ($action == "OK") { $con = ftp_connect($ftp_server); //连接ftpftp_login($con, $ftp_username, $ftp_password); //用户登录$suss=$con;ftp_chdir($suss, $ftp_dir); //cd到指定目录$ok_code = ftp_put($suss, $upload_file_name, $upload_file, FTP_BINARY); //put文件 if ($ok_code == 1) { echo "文件上传成功!\n"; } else { echo "文件上传失败!\n"; } ftp_quit($suss); // 关闭ftp连接} ?> 很好!!谢谢大家!!!!study!! GD显示中文的时候就用的到了//***********************************************************////下面这两个函数是将汉字由 Gb2 转化成 Utf8 格式////调用方式是:string gb2utf(string source);//// ////***********************************************************function gb2utf($str){ global $MB,$VdpSystemRoot; if($MB==""){ $filename = "$VdpSystemRoot"."include/html.txt"; $fd = fopen ($filename, "r"); $MB = fread ($fd, filesize ($filename)); fclose ($fd); } $string=""; $a=0; $b=strlen($str); while($a<$b){ if(ord($str[$a])>128){ //高位偏移量 $c=(ord($str[$a])-161)*94; $a++; //低位偏移量 $c=$c+ord($str[$a])-161; $c=$c*5; if(bin2hex(substr($MB,$c+2,1))==0x20){ $string.=substr($MB,$c,2); }else{ $string.=substr($MB,$c,3); } $tem=bin2hex($string); $a++; }else{ $string.=$str[$a]; $a++; } } return $string;} header();setcookie();俩函数之前必须没输出及htm代码 html.txt 太长了,如果谁确实需要,到时候给我发邮件就可以了[email protected] 连接数据库最好用类封装代码在http://www.phpe.com上有 这个文本为什么替换不了?只执行循环的第一次。。。谁帮看下问题在哪! 求高手们,看看这段代码有没有冗余的代码,或有什么改进的地方。 preg_replace call别的function问题 一个奇怪的现象 How to delete mysql password? 请问php页面用什么语句来消除页面缓存? 關於網站策劃,大家提提意見~~~~網絡如何包裝報紙? mssql,limit n,m 一个得到访客IP的函数 请教:怎样php通过odbc连接access数据库? php的字符串操作? 请教怎么么做注册时的验证码功能??
一个session验证程序等。然后在需要的页面include进去,这样的并不算上面技巧吧
不过一般来说我的整站程序所作的数据库操作都是用一个类来作的,无论是数据库连接、查询、更新等
<?/*
月历函数
array calendar(year,month)
输出周期为星期一到星期天(not sunday to saturday)
如果1号是星期n(n>1),则数组前面空出n-1个位置
*/
function calendar($year,$month,$style="world"){ if(empty($year)){$year=date('Y',time());}
if(empty($month)){$month=date('m',time());} $return = array(); #重新设定一些变量
$date=01; #当月的天数
$day=01; #循环中的日 #算出当月的天数
while (checkdate($month,$date,$year)){
$date++;
} $no=date('w', mktime(0,0,0,$month,$day,$year));
if ($no == 0){
$no=7;
}
if($style == "world"){
$no=$no-1;
}
else{
$no=$no-2;
} for($i=0;$i<=$no;$i++){
$return[$i]="";
} for($i=1;$i<$date;$i++){
$return[$i+$no]=$i;
} return $return;}#end function calendar?>
调用格式:
$password = rndstr(10,int type);
生成一个10位的随机密码
*/
function rndstr($length,$type=0) {
$hash = '';
switch($type){
case 0:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';break;
case 1:$chars = '0123456789';break;
case 2:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';break;
case 3:$chars = 'abcdefghijklmnopqrstuvwxyz';break;
case 4:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';break;
case 5:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';break;
case 6:$chars = '0123456789abcdefghijklmnopqrstuvwxyz';break;
case 7:$chars = 'ABCEFGHJKLMNTWXYZ2346789';break;
default:$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';break;
}
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}#end function rndstr
/*
生成4位的验证码图片
需要结合rndstr函数(string.func.php)
使用说明:直接调用imgauth(string authcode)
*/
function imgauth($authcode="ERR!"){
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(45,20); $black=ImageColorAllocate($im, 0,0,0); $backgnd=array(
1=>ImageColorAllocate($im, 230,230,0),
2=>ImageColorAllocate($im, 230,230,230),
3=>ImageColorAllocate($im, 0,230,230),
4=>ImageColorAllocate($im, 250,150,250),
);
$background=$backgnd[rand(1,4)];
imagefill($im,0,0,$background);
imagestring($im, 5, 4, 3, $authcode, $black); for($i=0;$i<100;$i++){
imagesetpixel($im, rand()%70 , rand()%30 , $background);
} ImagePNG($im);
ImageDestroy($im);
}#end function imgauth
?>
以上函数需要结合我上面的生成随机字符串的函数
这样就可以实现验证码的生成建立auth.php:
<?
include "func.string.php";
include "func.image.php";$authcode=rndstr(4,7); #注意,type设定为7,因为用来显示的字母必须没有异议,比如1和I就不行
imgauth($authcode);
?>然后在其它页面调用<img src="auth.php">对于验证码的保存,可以使用SESSION或者COOKIE或者数据库存储
<?php $ftp_server="ftp服务器(最好用IP)";
$ftp_username="ftp用户名";
$ftp_password="登录密码";
$ftp_dir="目录(如设置为 /)"; if ($action == "OK")
{
$con = ftp_connect($ftp_server); //连接ftp
ftp_login($con, $ftp_username, $ftp_password); //用户登录
$suss=$con;
ftp_chdir($suss, $ftp_dir); //cd到指定目录$ok_code = ftp_put($suss, $upload_file_name, $upload_file, FTP_BINARY); //put文件 if ($ok_code == 1)
{
echo "文件上传成功!\n";
}
else
{
echo "文件上传失败!\n";
}
ftp_quit($suss); // 关闭ftp连接
} ?>
//
//下面这两个函数是将汉字由 Gb2 转化成 Utf8 格式
//
//调用方式是:string gb2utf(string source);
//
//
//
//***********************************************************function gb2utf($str)
{
global $MB,$VdpSystemRoot; if($MB==""){
$filename = "$VdpSystemRoot"."include/html.txt";
$fd = fopen ($filename, "r");
$MB = fread ($fd, filesize ($filename));
fclose ($fd);
}
$string="";
$a=0;
$b=strlen($str);
while($a<$b){
if(ord($str[$a])>128){
//高位偏移量
$c=(ord($str[$a])-161)*94;
$a++;
//低位偏移量
$c=$c+ord($str[$a])-161;
$c=$c*5;
if(bin2hex(substr($MB,$c+2,1))==0x20){
$string.=substr($MB,$c,2);
}else{
$string.=substr($MB,$c,3);
}
$tem=bin2hex($string);
$a++;
}else{
$string.=$str[$a];
$a++;
}
}
return $string;
}
setcookie();
俩函数之前必须没输出及htm代码
代码在http://www.phpe.com上有