PHP实用小程序(五)
<HTML> <HEAD> <TITLE>图像函数</TITLE> </HEAD> <BODY> <img src="64.jpg"> <br> <? $image_file = "64.jpg"; $image_size = getimagesize($image_file); print("图片的宽度:". $image_size[0]."<br>"); print("图片的高度:". $image_size[1]."<br>"); print("文件的格式为:". $image_size[2]."<br>"); ?> </BODY> </HTML>
<HTML> <HEAD> <TITLE>session函数</TITLE> </HEAD> <BODY> <?php $username="guest1"; if(isset($username)) { session_name($username); } echo "您的名字为:".session_name()."\n"; ?> </BODY> </HTML>
<HTML> <HEAD> <TITLE>拼写检查</TITLE> </HEAD> <BODY> <? $aspell_link=aspell_new("english");//载入一个新的字典 if (aspell_check($aspell_link,"testt")) //检查testt的拼写 { echo "testt:正确的拼写"; } else { echo "testt :是一个错误的拼写"; print("<hr>"); } if (aspell_check($aspell_link,"test")) //检查test的拼写 { echo "test :是一个正确的拼写"; } else { echo "test:是一个错误的拼写"; } ?> </BODY> </HTML>
<HTML> <HEAD> <TITLE>循环中调用函数</TITLE> </HEAD> <BODY> <? function calculate($period) //定义函数 { return(cos(time()%$period)); } $start = time(); //循环前 for($index = 0; $index < 2000; $index++) { $value += calculate(360); } $finish = time(); //循环后 print("这次循环使用的时间为:" . "<br>".($finish - $start) . " seconds<BR>\n"); print ("<hr>"); print ("value的值为:".$value); ?> </BODY> </HTML>
<? print("<HTML>\n"); print("<HEAD>\n"); print("<TITLE>转换所有的标记为PHP</TITLE>\n"); print("</HEAD>\n"); $Hour = date("H"); $Intensity = round(($Hour/24.0)*(0xFF)); $PageColor = dechex($Intensity) . dechex($Intensity) . dechex($Intensity); print("<BODY BGCOLOR=\"#$PageColor\">\n"); print("<H1>转换所有的标记为PHP</H1>\n"); print("</BODY>\n"); print("</HTML>\n"); ?>
<? $URL = "http://www.microsoft.com/"; //start HTML page print("<HTML>\n"); print("<HEAD>\n"); print("<TITLE>取得页面的链接</TITLE>\n"); print("</HEAD>\n"); print("<BODY>\n"); $page = fopen($URL, "r");//打开URL print("链接 $URL<BR>\n"); print("<UL>\n"); while(!feof($page))//在页面中循环 { $line = fgets($page, 255); while(ereg("HREF=\"[^\"]*\"", $line, $match)) { //打印出URL链接 print("<Li>"); print($match[0]); print("<BR>\n"); } } print("</UL>\n"); fclose($page); //关闭页面 print("</BODY>\n"); print("</HTML>\n"); ?>
<HTML> <HEAD> <TITLE>对分查找法</TITLE> </HEAD> <BODY><? /* 定义一个函数 */ function byName($left, $right) { return(strcmp($left[0], $right[0])); } /* ** 创建数组:Name, Title, Wage */ $employee[] = array("tom, John", "Programmer", 20); $employee[] = array("jas, John", "Programmer", 20); $employee[] = array("june, John", "Programmer", 20); $employee[] = array("aili, John", "Programmer", 20); $employee[] = array("Doe, Jane", "Programmer", 20); $employee[] = array("Mann, Joe", "Manager", 35); $employee[] = array("Smith, John", "CEO", 50); $employee[] = array("Leeds, Al", "President", 100); /* ** 排序 */ usort($employee, "byName"); /* ** 查找对象 */ $Name = "Mann, Joe"; $lower_limit = 0; $upper_limit = count($employee) - 1; while($lower_limit < $upper_limit) { /* ** 取得中间点 */ $index = intval(($lower_limit + $upper_limit)/2); if(strcmp($employee[$index][1], $Name) > 0) { /* ** 结果在上半部分 */ $lower_limit = $index + 1; } elseif(strcmp($employee[$index][1], $Name) < 0) { /* ** 结果在下半部分 */ $upper_limit = $index - 1; } else { /* ** 发现结果 */ $lower_limit = $index; $upper_limit = $index; } } /* ** 打印结果 */ print("Mann, Joe 在数组中的位置:"." $index<BR>\n"); ?> </BODY> </HTML>
<HTML> <HEAD> <TITLE>文件上传界面</TITLE> </HEAD> <BODY><TABLE><CENTER> <? if($UploadAction){ $UploadAction=0; $TimeLimit=60; /*设置超时限制时间默认时间为 30s,设置为0时为不限时 */ set_time_limit($TimeLimit); If(($Upfile != "none")&& ($Upfile != "")) { $Filepath="d:\php\\"; //上载文件存放路径 $FileName=$Filepath.$Upfile_name; if($Upfile_size <1024) //上载文件大小 {$FileSize = (string)$Upfile_size . "字节";} elseif($Upfile_size <(1024 * 1024)) { $FileSize = number_format((double)($Upfile_size / 1024), 1) . " KB"; } else { $FileSize = number_format((double)($Upfile_size/(1024*1024)),1)."MB"; } if(!file_exists($FileName)) { if(copy($Upfile,$FileName)) {unlink($Upfile); echo "<br><br>\n"; echo "文件 $Upfile_name 已上载成功!"; echo "<br><br>\n"; echo "文件位置:$FileName"; echo "<br><br>\n"; echo "文件大小:$FileSize"; echo "<br><br>\n"; } else {echo "文件 $Upfile_name上载失败!"; } } else {echo "文件 $Upfile_name已经存在!"; } } else {echo "你没有选择任何文件上载!"; } set_time_limit(30); //恢复默认超时设置 } ?> <FORM ENCTYPE = "multipart/form-data" NAME = "SubmitForm" ACTION = "upfile.php" METHOD = "POST"> <INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE ="1000000"> <INPUT TYPE = "hidden" NAME = "UploadAction" VALUE = "1"> <TR><TD><INPUT NAME = "Upfile" TYPE = "file" SIZE = "30"></TD> </TR><TR><TD><INPUT NAME = "submit" VALUE = "提交" TYPE = "submit"> <INPUT NAME = "reset" VALUE = "重置" TYPE = "reset"></TD> </TR></FORM></CENTER></TABLE> </BODY> </HTML>
<? //本例用PHP4实现顶级域名查询 if ($domain) { if ($domain) { //连接到internic域名服务器 $fp = fsockopen( "rs.internic.net", 43, &$errno, &$errstr, 10); if (!$fp) { echo "$errstr ($errno)<br>\n"; } else { fputs($fp, "$domain\r\n"); while(!feof($fp)) { $buf = fgets($fp,128); if (ereg( "Whois Server:", $buf)) { $server = str_replace( "Whois Server: ", "", $buf); $server = trim($server); } } fclose($fp); } if ($server) { echo "<B>$domain 的注册服务器是 $server:</B><BR>"; echo "<PRE>"; $fp = fsockopen($server, 43, &$errno, &$errstr, 10); if(!$fp) { echo "不能连接到服务器 $server 的43端口.\n"; echo "$errstr ($errno)<br>\n"; } else { fputs($fp, "$domain\r\n"); while(!feof($fp)) { echo fgets($fp,128); } fclose($fp); } } else { echo( "<b>$domain 没有注册。</b><BR>"); } echo ( "</PRE><BR>"); } } ?> <FORM ACTION=" <?PHP echo($PHP_SELF); ?>" METHOD="post"> 只能查询.com,.net和.org的域名<br> 输入域名:<INPUT TYPE="text" NAME="domain" SIZE="30" MAXLENGTH="100"> <INPUT TYPE=submit VALUE="查 询"> </FORM>
<? /* 这个函数可以用来检查年月日形式的日期合法性,您只要稍作修改便可以检查其它形式的日期合法性. 功能:日期合法性检查 返回: true(合法)/false(不合法) 参数:$ymd 年月日形式的日期 $sep 年月日之间的分隔符,默认为- */ function datecheck($ymd,$sep='-'){ $parts = explode($sep,$ymd); $year = $parts[0]; $month = $parts[1]; $day = $parts[2]; if(isint($year) && isint($month) && isint($day)){ if(checkdate($month,$day,$year)) return true; else return false; } else return false; } // [ php/inc/isint.php ] cvs 1.2 function isint($str){ $str = (string)$str; $pos = 0; $len = strlen($str); for($i=0;$i<$len;$i++){ if($str[$i]=='0') $pos++; else break; } $str = substr($str,$pos); $int = (int)$str; if($str==(string)$int) return true; else return false; } //测试 $dates = array( '2000-4-11' => '-', '1900-2-1' => '-', '2001-03-01' => '-', 'abaf' => '-', '20.03.05' => '.' ); while(list($date,$sep)=each($dates)){ if(datecheck($date,$sep)) echo $date.' 是合法日期<br>'; else echo $date.' 不是合法日期<br>'; } // ?>
<? /* ** 这个程序将检测一个邮政编码,必须是 ** 6位的数字 */ //HTML页面开始 print("<HTML>\n"); print("<HEAD>\n"); print("<TITLE>检查一个邮政编码</TITLE>\n"); print("</HEAD>\n"); print("<BODY>\n"); /* ** 如果得到一个邮政编码 */ if(isset($zip)) { if(ereg("^([0-9]{6})(-[0-9]{5})?$", $zip)) { print("$zip 是一个正确的邮政编码。<BR>\n"); } else { print("$zip <B>不是</B> 一个正确的邮政编码<BR>\n"); } } //表单开始 print("<FORM ACTION=\"2.php\">\n"); print("<INPUT TYPE=\"text\" NAME=\"zip\">\n"); print("<INPUT TYPE=\"submit\">\n"); print("</FORM>\n"); //关闭网页 print("</BODY>\n"); print("</HTML>\n"); ?>
<HTML> <HEAD> <TITLE>得到100个随机数字</TITLE> </HEAD> <BODY> <? /* ** 返回一个随机数字 */ function RandomNumber($new_seed="") { static $seed; $constant = bcadd(bcpow("2", "13"), "1"); $prime = 37; $max_value = bcpow("2", "50", $scale); /* ** 如果得到参数 */ if($new_seed != "") { $seed = $new_seed; } /* ** 如果没有,则使用时间戳 */ if($seed == "") { $seed = strval(time()); } /* ** 得到下一个数值 */ $seed = bcmul($constant, $seed); $seed = bcadd($seed, $prime); $seed = bcmod($seed, $max_value); return($seed); } /* ** 设置 BC Math 函数 */ bcscale(0); /* ** 得到100个数字 */ for($index = 0; $index < 100; $index++) { print(RandomNumber() . "<BR>\n"); } ?> </BODY> </HTML>
<HTML> <HEAD> <TITLE>得到十个随机数</TITLE> </HEAD> <BODY> <BR> <FONT SIZE=5> <? /* ** 生成一个随机数 */ srand(time()); /* ** 从1到100中取得随机数 */ for($index = 0; $index < 10; $index++) { $number = (rand()%100)+1; print("$number \n"); } ?> </FONT> </BODY> </HTML>
<? // HTML文档开始 print("<HTML>\n"); // 编写头文件 print("<HEAD>\n"); print("<TITLE>"); print("查看句中单词的个数"); print("</TITLE>\n"); print("</HEAD>\n"); // 编写HTML主体 print("<BODY>\n"); /* ** 如果得到提交的语句则执行下面的语句 */ if(isset($sentence)) { print("<B>得到的语句:</B>"); print("$sentence<BR><BR>\n"); $separators = " "; //得到每个单词 for($token = strtok($sentence, $separators); $token; $token = strtok($separators)) { // 计算每个单词 $word_count[$token]++; $total++; } //分成数组 arsort($word_count); print("<B>找到$total 个单词</B>\n"); print("<UL>\n"); while(list($key, $value) = each($word_count)) { print("<LI>$key ($value)\n"); } print("</UL>\n"); } print("<FORM ACTION=\"77.php\">\n"); print("<INPUT NAME=\"sentence\" SIZE=\"10\">\n"); print("<INPUT TYPE=\"submit\" VALUE=\"提交\">\n"); print("</FORM>\n"); print("</BODY>\n"); // End HTML document print("</HTML>\n"); ?>
<? /* * 计算星座的函数 string get_zodiac_sign(string month, string day) * 输入:月份,日期 * 输出:星座名称或者错误信息 */ ?> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <style type="text/css"> <!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --> </style> <title>计 算 星 座</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <br><br><center> <form action="<? echo $PHP_SELF; ?>" method="get"> <select size="1" name="month"> <? for($i=1;$i<13;$i++) { ?> <option value="<?echo $i?>"><?echo $i?></option> <? } ?> </select>月 <select size="1" name="day"> <? for($i=1;$i<32;$i++) { ?> <option value="<?echo $i?>"><?echo $i?></option> <? } ?> </select>日 <input type="submit" value="提交"> </form> <? if($month) { ?> <?echo $month?>月<?echo $day?>日的星座是:<font color=red> <? // 调用例子:生日为$month月$day日的星座 echo get_zodiac_sign( $month,$day); } ?> </font></center> <? function get_zodiac_sign($month, $day) { // 检查参数有效性 if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return (false); // 星座名称以及开始日期 $signs = array( array( "20" => "宝瓶座"), array( "19" => "双鱼座"), array( "21" => "白羊座"), array( "20" => "金牛座"), array( "21" => "双子座"), array( "22" => "巨蟹座"), array( "23" => "狮子座"), array( "23" => "处女座"), array( "23" => "天秤座"), array( "24" => "天蝎座"), array( "22" => "射手座"), array( "22" => "摩羯座") ); list($sign_start, $sign_name) = each($signs[(int)$month-1]); if ($day < $sign_start) list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]); return $sign_name; }//函数结束 ?>
<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <title>检测OICQ用户是否在线</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="20" bgColor="#FFFFFF"> <? if($HTTP_REFERER=="") { echo "非法调用<br>正确用法:<script src=\"http://yourhost/oicq.php?uin=你的OICQ号\"></script>"; exit(); } function get_status($uin) { $fp = @fsockopen ("infocenter.tencent.com", 80, &$errno, &$errstr, 30); if(!$fp) { return '出错'; } else { fputs($fp, "GET /$uin/l/00/0/ HTTP/1.0\n\n"); $do = 'yes'; while(!feof($fp)) { $line = fgets ($fp,128); $do = ($do == 'yes')?'yes':(eregi("^GIF89", $line))?'yes':'no'; if($do == 'yes') { if(ereg("Online", $line)) { return '在线'; } elseif(ereg("Offline", $line)) { return '没在线'; } } } fclose($fp); } return '未知'; } if(isset($uin)) { switch(get_status($uin)) { case "在线": $oicqimg="on.GIF"; $mstatus="在线"; break; case "没在线": $oicqimg="off.GIF"; $mstatus="没在线"; break; case "未知": $oicqimg="na.GIF"; $mstatus="未知"; break; } } ?> document.write("<img src=\"<?echo $oicqimg?>\"><sup><?echo $mstatus?><sup>");