分享下$_GET,$_POST,$_REQUEST和$_SERVER的用法,以及parse_str的用法。
首先,学习下$_GET、$_POST的用法。
$_GET变量获取通过get方式提交过来的数据。
$_POST 变量获取通过post方式提交过来的数据。
注意:使用的键名都是表单的name属性的值。
例如:
<input type="textbox" name="xingming" value="">
<input type="textbox" name="age" value="">
<input type="submit" value="get">
</form>
<form action="/blog_article/action.html" method="post">
<input type="textbox" name="xingming" value="">
<input type="textbox" name="age" value="">
<input type="submit" value="post">
</form>
<?php
echo $_GET['xingming']."<br>";
echo $_GET['age']."<br>";
echo $_POST['xingming']."<br>";
echo $_POST['age']."<br>";
?>
$_REQUEST的官方说明:
$_REQUEST — HTTP Request 变量默认情况下包含了 $_GET,$_POST 和 $_COOKIE 的数组。
在不关心是get过来的还是post,总之要取得某一个键的值,就用$_REQUEST即可。
不过$_REQUEST速度会比$_GET,$_POST稍慢点。
通过$_SERVER获取服务器端信息
$_SERVER是一个数组,包含了headers, paths, script locations等多种信息。早期的php版本使用$HTTP_server_VARS数组,现在已经弃用了。
不同的服务器$_SERVER的信息也不一定相同的。通常的用法如下:
echo $_SERVER['QUERY_STRING'];
比较常用的获取QUERY_STRING的方法,通过$_SERVER['QUERY_STRING']得到的是诸如name=mike&age=30这样的。要把这种格式的值转成变量,PHP中有个方法parse_str,可以实现这个功能。
官方的示例:
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>
在本地环境通过代码:
var_dump($_SERVER);
echo "";
用带格式的数据打印出数组内容:
["HTTP_ACCEPT"]=>
string(3) "*/*"
["HTTP_ACCEPT_LANGUAGE"]=>
string(5) "zh-CN"
["HTTP_USER_AGENT"]=>
string(205) "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2; Avant Browser)"
["HTTP_ACCEPT_ENCODING"]=>
string(13) "gzip, deflate"
["HTTP_HOST"]=>
string(14) "localhost:8080"
["HTTP_CONNECTION"]=>
string(10) "Keep-Alive"
["HTTP_COOKIE"]=>
string(154) "codehilite=IsPre=True&IsShowRowNumber=False&IsUseBR=False&Language=Csharp; iGHA2Cas=key=s8hoPBw6cWYHJ34NowHt%2f77gsEiQ9U9K0dDGPYjwLCFzQbqnNjlYMnUw9OOCF68u"
["PATH"]=>
string(540) "C:\Program Files (x86)\ActiveState Komodo IDE 7\;C:\Program Files (x86)\ActiveState Komodo Edit 7\;E:\app\Administrator\product\11.1.0\client_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;d:\php-5.4.4-Win32-VC9-x86\;"
["SystemRoot"]=>
string(10) "C:\Windows"
["COMSPEC"]=>
string(27) "C:\Windows\system32\cmd.exe"
["PATHEXT"]=>
string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
["WINDIR"]=>
string(10) "C:\Windows"
["SERVER_SIGNATURE"]=>
string(0) ""
["SERVER_SOFTWARE"]=>
string(31) "Apache/2.2.22 (Win32) PHP/5.4.4"
["SERVER_NAME"]=>
string(9) "localhost"
["SERVER_ADDR"]=>
string(9) "127.0.0.1"
["SERVER_PORT"]=>
string(4) "8080"
["REMOTE_ADDR"]=>
string(9) "127.0.0.1"
["DOCUMENT_ROOT"]=>
string(13) "D:/phpwwwroot"
["SERVER_ADMIN"]=>
string(11) "dds@wwd.com"
["SCRIPT_FILENAME"]=>
string(29) "D:/phpwwwroot/RecJP/test2.php"
["REMOTE_PORT"]=>
string(5) "23827"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["REQUEST_METHOD"]=>
string(3) "GET"
["QUERY_STRING"]=>
string(0) ""
["REQUEST_URI"]=>
string(16) "/RecJP/test2.php"
["SCRIPT_NAME"]=>
string(16) "/RecJP/test2.php"
["PHP_SELF"]=>
string(16) "/RecJP/test2.php"
["REQUEST_TIME_FLOAT"]=>
float(1351577790572)
["REQUEST_TIME"]=>
int(-1336907668)
}
php实现的操作session与cookie的类,可以对session、cookie进行加密与解密。
代码:
<?php /** * session与cookie操作类 * by www. */ class Session { private $use_cookie; private $preStr; private $maxCookie; private $cookieLenght; private $stringone; private $duratacookie; private $secret; public function __construct ($cook = "zipcook") { $this->use_cookie = $cook; //choose mode $this->preStr= "_KK_"; //prefix for cookies $this->maxCookie=20; //since cookie lenght is limited, I've limited the number of cookies $this->cookieLenght=3096; //max cookie length (it depends on browser) $this->duratacookie=3600*24;//cookie life time $this->secred="secret"; //secret keyword to crypt/decrypt, change this to customize encryption if ($this->use_cookie=="yes") { } elseif ($this->use_cookie=="zipcook") { $this->stringone = $this->prelevaStringaTotale(); } else { ini_set("session.gc_maxlifetime","432000"); ini_set("url_rewriter.tags",""); ini_set("session.use_trans_sid", false); session_start(); } } /* --------------------------- */ /* parse_str 变量 */ /* -------------------------- */ private function build_str($ar) { $qs = array(); foreach ($ar as $k => $v) { $qs[] = $k.'='.$v; } return join('&', $qs); } /* ------------------------------ */ /* 获取加密后的cookie列表 */ /* cookies */ /* ----------------------------- */ private function prelevaStringaTotale() { $cookiesSet = array_keys($_COOKIE); $out = ""; for ($x=0;$x<count($cookiesSet);$x++) { if (strpos(" ".$cookiesSet[$x],$this->preStr)==1) $out.=$_COOKIE[$cookiesSet[$x]]; } return $this->decrypta($out); } public function debug() { // for debug return $this->prelevaStringaTotale(); } /* ---------------------------- */ /* 确定可用的cookies */ /* ---------------------------- */ private function calcolaCookieLiberi() { $cookiesSet = array_keys($_COOKIE); $c=0; for ($x=0;$x<count($cookiesSet);$x++) { if (strpos(" ".$cookiesSet[$x],$this->preStr)==1) $c+=1; } return $this->maxCookie - count($cookiesSet) + $c; } /* ------------------------ */ /* 分割字符串存储进cookie */ /* ------------------------ */ private function my_str_split($s,$len) { $output = array(); if (strlen($s)<=$len) { $output[0] = $s; return $output; } $i = 0; while (strlen($s)>0) { $s = substr($s,0,$len); $output[$i]=$s; $s = substr($s,$len); $i++; } return $output; } /* ----------------------------- */ /* 保存session与cookie值 */ /* ----------------------------- */ public function register($var,$value) { $this->set($var,$value); } public function set($var,$value) { if ($this->use_cookie=="yes") { setcookie($var,$this->crypta($value),time()+$this->duratacookie,"/", $_SERVER['HTTP_HOST'] ); } elseif ($this->use_cookie=="zipcook") { if ($this->stringone!="") { parse_str($this->stringone, $vars); } else { $vars=array(); } $vars[$var] = $value; //aggiungo-modifico valore $str = $this->crypta($this->build_str($vars)); $arr = $this->my_str_split($str,$this->cookieLenght); $cLiberi = $this->calcolaCookieLiberi(); if (count($arr) < $cLiberi) { // c'ho spazio, posso registrare $this->stringone = $this->build_str($vars); for ($i=0;$i<count($arr);$i++) { setcookie($this->preStr.$i,$arr[$i],time()+$this->duratacookie,"/", $_SERVER['HTTP_HOST'] ); } } else { //cookie overflow return "errore cookie overflow"; } } else { session_register($var); } } /* ------------------------------------------- */ /* 获取加密后的cookie值,未加密,请直接取得 */ /* ------------------------------------------- */ public function get($var) { if ($this->use_cookie=="yes") { global $_COOKIE; return $this->decrypta($_COOKIE[$var]); } elseif ($this->use_cookie=="zipcook") { if ($this->stringone!="") { parse_str($this->stringone, $vars); } else { return ""; } if(!isset($vars[$var])) { return ""; } return $vars[$var]; } else { if ($this->is_registered($var)) { $this->$var=$GLOBALS[$var]; } else if(isset($GLOBALS[$var])) $this->$var = $GLOBALS[$var]; else $this->$var=""; return($this->$var); } } /* --------------------------- */ /* 清空session或cookie */ /* -------------------------- */ public function finish() { if ($this->use_cookie=="yes") { $cookiesSet = array_keys($_COOKIE); for ($x=0;$x<count($cookiesSet);$x++) { //echo $cookiesSet[$x]."<br/>"; setcookie($cookiesSet[$x],"",time()-3600*24,"/", $_SERVER['HTTP_HOST'] ); //faccio scadere il cookie } } elseif ($this->use_cookie=="zipcook") { $cookiesSet = array_keys($_COOKIE); for ($x=0;$x<count($cookiesSet);$x++) { if (strpos(" ".$cookiesSet[$x],$this->preStr)==1) setcookie($cookiesSet[$x],"",time()-3600*24,"/",$_SERVER['HTTP_HOST']); $this->stringone=""; } } else { session_destroy(); $_SESSION = array(); } } /* 加密 */ private function crypta($t){ if ($t=="") return $t; $r = md5(10); $c=0; $v=""; for ($i=0;$i<strlen($t);$i++){ if ($c==strlen($r)) $c=0; $v.= substr($r,$c,1) . (substr($t,$i,1) ^ substr($r,$c,1)); $c++; } return (base64_encode($this->ed($v))); } /* 解密 */ private function decrypta($t) { if ($t=="") return $t; $t = $this->ed(base64_decode(($t))); $v = ""; for ($i=0;$i<strlen($t);$i++){ $md5 = substr($t,$i,1); $i++; $v.= (substr($t,$i,1) ^ $md5); } return $v; } /* used to crypt/decrypt */ private function ed($t) { $r = md5($this->secret); $c=0; $v=""; for ($i=0;$i<strlen($t);$i++) { if ($c==strlen($r)) $c=0; $v.= substr($t,$i,1) ^ substr($r,$c,1); $c++; } return $v; } } ?>
分享两个实例,学习这个类的用法。
例1:
<? //test1.php include("session.class.php"); $s = new Session(); $s->set("userid","27"); ?>
例2:
<? //test2.php include("session.class.php"); $s = new Session(); echo $s->get("userid"); //27 ?>
使用phpexcel类库,实现导出数据,代码如下:
<?php /** * phpexcel实例 导出数据 * by www. */ public function export_data($data = array()) { # code... include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/IWriter.php') ; include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/Excel5.php') ; include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel.php') ; include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/IOFactory.php') ; $obj_phpexcel = new PHPExcel(); $obj_phpexcel->getActiveSheet()->setCellValue('a1','Key'); $obj_phpexcel->getActiveSheet()->setCellValue('b1','Value'); if($data){ $i =2; foreach ($data as $key => $value) { # code... $obj_phpexcel->getActiveSheet()->setCellValue('a'.$i,$value); $i++; } } $obj_Writer = PHPExcel_IOFactory::createWriter($obj_phpexcel,'Excel5'); $filename = "outexcel.xls"; header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="'.$filename.'"'); header("Content-Transfer-Encoding: binary"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Pragma: no-cache"); $obj_Writer->save('php://output'); } //at 2013-8-24 11:22:18
您可能感兴趣的文章:
PHPExcel常用方法举例
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
phpExcel类的使用方法分享
phpexcel导出excel的经典实例
PHPExcel读取excel文件的例子
phpexcel类库实例 支持(excel2003 excel2007)
phpexcel导入excel到数据库的代码
phpexcel快速开发指南(不错)
phpExcel中文帮助手册(知识点)
使用PHPExcel判别和格式化Excel中的日期格式的例子
phpexcel导出excel的颜色与网页中颜色不一致的解决方法
CI中使用PHPExcel导出数据到Excel