php中global和static两个关键字详解,有需要的朋友可参考一下。
1.global在整个页面起作用。
2.static只在function和class内起作用。
global和$GLOBALS使用基本相同,但在实际开发中大不相同。
global在函数产生一个指向函数外部变量的别名变量,而不是真正的函数外部变量,一但改变了别名变量的指向地址,就会发生一些意料不到情况,例如例子1.
$GLOBALS[]确确实实调用是外部的变量,函数内外会始终保持一致!
实例
代码如下 |
复制代码 |
<?php
// 比较global、GLOBALS、static
$k = 0;
function test1() {
global $k;
static $i = 0;
echo 'i:', ++$i, '<br/>';
echo 'k:', ++$k, '<br/>';
}
test1();
test1();
echo 'i:', $i, '<br/>';
echo 'k:', $k, '<br/>';
echo '------------------<br/>';
function test2() {
global $k;
static $i = 0;
echo 'i:', ++$i, '<br/>';
echo 'k:', ++$k, '<br/>';
}
test2();
test2();
echo 'i:', $i, '<br/>';
echo 'k:', $k, '<br/>';
$m = 0;
$n = 0;
function test3() {
global $m;
echo 'm:', $m++ , '<br/>';
echo 'n:', $GLOBALS['n']++ , '<br/>';
unset($m, $GLOBALS['n']);
}
echo '------------------<br/>';
test3();
echo 'm:', $m, '<br/>';
echo 'n:', $n, '<br/>';
echo '------------------<br/>';
static $x = 0;
function test4() {
echo 'x:', $x++ , '<br/>';
}
test4();
结果如下:
i:1
k:1
i:2
k:2
i:
k:2
------------------
i:1
k:3
i:2
k:4
i:
k:4
------------------
m:0
n:0
m:1
n:
------------------
x:
|
global关键字如果用在function内部,则说明这个function内用的这个变量是全局的,全局变量就是在整个页面里都能起作用
static就是表示静态。所谓的静态,其实是在function或者class内部而言的。function中static的变量,在funciton执行完之后,不会消失,可以在下次执行的时候继续使用
一个可以统计你程序的运行时间长知的php类,有需要的朋友可参考一下。
代码如下 |
复制代码 |
class Timer {
private $StartTime = 0;//程序运行开始时间
private $StopTime = 0;//程序运行结束时间
private $TimeSpent = 0;//程序运行花费时间
function start(){//程序运行开始
$this->StartTime = microtime();
}
function stop(){//程序运行结束
$this->StopTime = microtime();
}
function spent(){//程序运行花费的时间
if ($this->TimeSpent) {
return $this->TimeSpent;
} else {
list($StartMicro, $StartSecond) = explode(" ", $this->StartTime);
list($StopMicro, $StopSecond) = explode(" ", $this->StopTime);
$start = doubleval($StartMicro) + $StartSecond;
$stop = doubleval($StopMicro) + $StopSecond;
$this->TimeSpent = $stop - $start;
return substr($this->TimeSpent,0,8)."秒";//返回获取到的程序运行时间差
}
}
}
$timer = new Timer();
$timer->start();
//...程序运行的代码
$timer->stop();
echo "程序运行时间为:".$timer->spent();
|
php curl 太强大了,它不但可以模仿用户登录,还可以模仿用户IP地址哦,为伪造IP来源,本实例仅供参考哦。
curl发出请求的文件fake_ip.php:
代码
代码如下 |
复制代码 |
<?php
$ch = curl_init();
$url = "http://localhost/target_ip.php";
$header = array(
'CLIENT-IP:58.68.44.61',
'X-FORWARDED-FOR:58.68.44.61',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$page_content = curl_exec($ch);
curl_close($ch);
echo $page_content;
?>
请求的目标文件target_ip.php:
代码
<?php
echo getenv('HTTP_CLIENT_IP');
echo getenv('HTTP_X_FORWARDED_FOR');
echo getenv('REMOTE_ADDR');
?>
|
目标文件target_ip里面的IP打印顺序是目前很多开源系统的IP获取顺序
访问fake_ip.php,看到结果:
58.68.44.61
58.68.44.61
127.0.0.1
实例
CURL确实很强悍,可以伪造IP和来源。
1.php 请求 2.php 。
代码如下 |
复制代码 |
1.php代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/2.php");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8')); //构造IP
curl_setopt($ch, CURLOPT_REFERER, "http://www.111cn.net/ "); //构造来路
curl_setopt($ch, CURLOPT_HEADER, 1);
$out = curl_exec($ch);
curl_close($ch);
2.php代码如下:
function getClientIp() {
if (!empty($_SERVER["HTTP_CLIENT_IP"]))
$ip = $_SERVER["HTTP_CLIENT_IP"];
else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if (!empty($_SERVER["REMOTE_ADDR"]))
$ip = $_SERVER["REMOTE_ADDR"];
else
$ip = "err";
return $ip;
}
echo "IP: " . getClientIp() . "";
echo "referer: " . $_SERVER["HTTP_REFERER"];
|
伪造成功,这是不是给“刷票”的朋友提供了很好的换IP的方案!!
恶意刷新就是不停的去刷新提交页面,导致大量无效数据了,下面我们来总结一下php 防止恶意刷新页面方法总结
防止恶意刷页面的原理是
要求在页面间传递一个验证字符串,
在生成页面的时候 随机产生一个字符串,
做为一个必须参数在所有连接中传递。同时将这个字符串保存在session中。
点连接或者表单进入页面后,判断session中的验证码是不是与用户提交的相同,如果相同,则处理,不相同则认为是重复刷新。
在处理完成后将重新生成一个验证码,用于新页面的生成
代码
代码如下 |
复制代码 |
<?php
session_start();
$k=$_GET['k'];
$t=$_GET['t'];
$allowTime = 1800;//防刷新时间
$ip = get_client_ip();
$allowT = md5($ip.$k.$t);
if(!isset($_SESSION[$allowT]))
{
$refresh = true;
$_SESSION[$allowT] = time();
}elseif(time() - $_SESSION[$allowT]>$allowTime){
$refresh = true;
$_SESSION[$allowT] = time();
}else{
$refresh = false;
}
?>
|
ie6提交两次我也碰到过,大致是用图片代替submit时,图片上有个submit(),这样会提交两次,如果只是submit钮我没碰到过提交两次的情况。
现在整理一下:
方法基本上前面几位说得差不多
接收的页即2.php分为两部分,一部分处理提交过来的变量,一部分显示页面
处理变量完毕用header( "location: ".$_SERVER[ 'PHP_SELF '])跳转到自身页
本部分要做判断,如果没有post的变量就跳过。当然也可以跳到别的页面。
跳到别的页面返回时会有问题,建议做在一个php文件里。
如果上页穿过来得变量不符合要求可以强制返回
代码如下 |
复制代码 |
<script>
history.go(-1);
</script> |
只说了一下大体思路,也许高手们不会遇到此类问题,可是并不是每个人都是高手。
2.php的流程
代码如下 |
复制代码 |
if(isset($_POST))
{ 接收变量
if(变量不符合要求)
<script> history.go(-1); </script>
else
操作数据
...
if(操作完成)
header( "location: ".$_SERVER[ 'PHP_SELF ']);
}
<script language= "JavaScript ">
<!--
javascript:window.history.forward(1);
//-->
</script>
|
•得到当前要处理的月份总共有多少天$days
•得到当前要处理的月份的一号是星期几$dayofweek
$days的作用:知道要处理的月份共有多少天,就可以通过循环输出天数了
使用PHP实现万年历功能的要点:
•得到当前要处理的月份总共有多少天$days
•得到当前要处理的月份的一号是星期几$dayofweek
$days的作用:知道要处理的月份共有多少天,就可以通过循环输出天数了
$dayofweek的作用:只有知道每个月的1号是星期几,才能知道在输出天数之前需要输出多少空格(空白)
代码如下 |
复制代码 |
<?php
/**
* PHP万年历
* @author Fly 2012/10/16
*/
class Calendar{
protected $_table;//table表格
protected $_currentDate;//当前日期
protected $_year; //年
protected $_month; //月
protected $_days; //给定的月份应有的天数
protected $_dayofweek;//给定月份的 1号 是星期几
/**
* 构造函数
*/
public function __construct()
{
$this->_table="";
$this->_year = isset($_GET["y"])?$_GET["y"]:date("Y");
$this->_month = isset($_GET["m"])?$_GET["m"]:date("m");
if ($this->_month>12){//处理出现月份大于12的情况
$this->_month=1;
$this->_year++;
}
if ($this->_month<1){//处理出现月份小于1的情况
$this->_month=12;
$this->_year--;
}
$this->_currentDate = $this->_year.'年'.$this->_month.'月份';//当前得到的日期信息
$this->_days = date("t",mktime(0,0,0,$this->_month,1,$this->_year));//得到给定的月份应有的天数
$this->_dayofweek = date("w",mktime(0,0,0,$this->_month,1,$this->_year));//得到给定的月份的 1号 是星期几
}
/**
* 输出标题和表头信息
*/
protected function _showTitle()
{
$this->_table="<table><thead><tr align='center'><th colspan='7'>".$this->_currentDate."</th></tr></thead>";
$this->_table.="<tbody><tr>";
$this->_table .="<td style='color:red'>星期日</td>";
$this->_table .="<td>星期一</td>";
$this->_table .="<td>星期二</td>";
$this->_table .="<td>星期三</td>";
$this->_table .="<td>星期四</td>";
$this->_table .="<td>星期五</td>";
$this->_table .="<td style='color:red'>星期六</td>";
$this->_table.="</tr>";
}
/**
* 输出日期信息
* 根据当前日期输出日期信息
*/
protected function _showDate()
{
$nums=$this->_dayofweek+1;
for ($i=1;$i<=$this->_dayofweek;$i++){//输出1号之前的空白日期
$this->_table.="<td> </td>";
}
for ($i=1;$i<=$this->_days;$i++){//输出天数信息
if ($nums%7==0){//换行处理:7个一行
$this->_table.="<td>$i</td></tr><tr>";
}else{
$this->_table.="<td>$i</td>";
}
$nums++;
}
$this->_table.="</tbody></table>";
$this->_table.="<h3><a href='?y=".($this->_year)."&m=".($this->_month-1)."'>上一月</a> ";
$this->_table.="<a href='?y=".($this->_year)."&m=".($this->_month+1)."'>下一月</a></h3>";
}
/**
* 输出日历
*/
public function showCalendar()
{
$this->_showTitle();
$this->_showDate();
echo $this->_table;
}
}
$calc=new Calendar();
$calc->showCalendar();
|
标签:[!--infotagslink--]