PHP 搞笑的函数

在javascript里面,我们可以这样写代码:

var a = function(){
   var b = function(){alert('hello');};
   b();
}

在PHP里面,我们也可以这么写滴 :D

<?php 
  function a(){
    function b(){
      echo 'hello';
    }
   b();
 }
 
a();
?>

结果会怎么样呢?是我们预期需要的 hello,但是再来执行一次
a();
a();
程序就会出现function exist 错误了。
原因是:php 函数定义可以放在另一个函数里面,或者对象的方法也可以。当外面的函数被执行的时候,PHP就会将里面的函数提升成为全局函数,因此我们可以在其他的代码里面使用里面的函数b()。但是当外面的函数被多次执行时候,由于php再一次定义里面的函数,因此就会出现function exist错误了。
如果我们还需要这样弄的话,可以加一个 function_exist()来判断之后再去定义,这样就不会出现错误了。

ignore_user_abort 利用

如果一个服务器,你拿不到shell 操作账号,有没有办法实现crontab效果呢?
有!如果服务器对php的函数没有做限制(没有禁用ignore_user_abort函数、set_time_limit函数)就可以实现:
下面这个文件就是实现读取:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
read())){
	if($entry != 'crontab.run.php' &amp;&amp; filesize($entry) &gt; 0 &amp;&amp;
           array_pop(explode('.', $entry)) == 'php'){
		include_once $entry;
	}
}
 
//read crontab list
$cron_list = $GLOBALS['PROJECT_CRONTAB_LIST'];
if(empty($cron_list)){
	exit('NO CRONTAB LIST');
}
 
//init set
ignore_user_abort(true);
set_time_limit(60);
ini_set('time_zone', 'Asia/Shanghai');
$interval = 1;
 
//process
while(true){
	$c_y = intval(date('Y'));
	$c_m = intval(date('m'));
	$c_d = intval(date('d'));
	$c_h = intval(date('h'));
	$c_i = intval(date('i'));
	$c_s = intval(date('s'));
 
	foreach($cron_list as $cron){
		foreach($cron as $t=&gt;$func){
			list($date, $time)= explode(' ', $t);
			list($y,$m,$d) = array_map('intval',explode('-', $date));
			list($h,$i,$s) = array_map('intval',explode(':', $time));
 
			if( ($y == $c_y || $y == 0) &amp;&amp;
				($m == $c_m || $m == 0) &amp;&amp;
				($d == $c_d || $d == 0) &amp;&amp;
				($h == $c_h || $h == 0) &amp;&amp;
				($i == $c_i || $i == 0) &amp;&amp;
				($s == $c_s || $s == 0)
			){
				call_user_func($func);
			}
		}
	}
 
	sleep($interval);
}
 
?&gt;

php不用框架不要紧,只要你注意到了下面的问题

框架对PHP来说并不是最重要的。
一个完整的程序能够实现它自己的功能就已经可以了。 如果实现功能的同时考虑到常见问题的解决,那就更完美
下面的问题是一个常见web程序需要考虑到的问题,你的系统考虑到了没有呢?
1、可以方便的替换模板,可以使用替换模板样式文件方式更换主题
2、可以提供多语言
3、可以对模板进行缓存
4、可以方便的生成静态HTML
5、可以实现数据库引擎灵活更换
6、可以方便实现权限控制
7、可以方便实现URL REWRITE
8、可以安全防止代码泄露问题
9、可以快速进行全局内容过滤实现
10、可以方便进行安装、配置
11、可以实现插件、二次开发
12、可以实现代码共享
13、可以实现对外接口开发
….