我们以具体的实例来体会__FILE__和$_SERVER['SCRIPT_FILENAME']的区别。
假设有a.php和b.php2个文件,分别位于E:/var/www/中。
1,a.php
echo $_SERVER['SCRIPT_FILENAME'].'<br/>';
echo __FILE__;
执行a.php 得到:
E:\wamp\www\Text\a.php (__FILE__)
2,b.php
require 'a.php';
执行b.php,结果为:
E:\wamp\www\Text\a.php (__FILE__)
由此,我们得出二者的区别:
$_SERVER['SCRIPT_FILENAME'] 当前执行程序的绝对路径及文件名
__FILE__ 原始文件(被包含文件)的绝对路径及文件名
亲,你明白了吗?
有关__FILE__和$_SERVER['SCRIPT_FILENAME']的区别,大家好好体会以上例子中包含的意思吧。
php的全局变量存在漏洞,这一点,你是否有了解呢?
Discuz中有这样一句:
exit('Request tainting attempted.');
}
register_globals 是php中的一个控制选项,可以设置成off或者on ,默认为off,决定是否将 EGPCS(Environment,GET,POST,Cookie,Server)变量注册为全局变量。
如果 register_globals打开的话, 客户端提交的数据中含有GLOBALS变量名, 就会覆盖服务器上的$GLOBALS变量。
所以 这段代码, 就是判断, 如果提交的数据中有GLOBALS变量名, 就终止程序。
由此引起的安全问题成为PHP的“自动全局变量漏洞”,请一定要关掉register_globals选项。并且使用 $_GET, $_POST, $_COOKIE 而非 $_REQUEST 。
Discuz!论坛绕过全局变量防御漏洞
由于php5.3.x版本php.ini的设置中 request_order 默认值为 GP ,导致Discuz! 6.x/7.x中可以绕过全局变量防御。
在include/global.func.php中:
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if(!MAGIC_QUOTES_GPC || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
include/common.inc.php中:
foreach($$_request as $_key => $_value) {
$_key{0} != '_' && $$_key = daddslashes($_value);
}
}
在register_globals=on时通过提交GLOBALS变量就可以绕过上面的代码。
Discuz!中提供的防范方法:
exit('Request tainting attempted.');
}
$_REQUEST超全局变量的值受php.ini中request_order的影响,在最新的php5.3中,request_order默认值为GP,即默认配置下$_REQUEST只包含$_GET和$_POST而不包括$_COOKIE。
借助COOKIE即可提交GLOBALS变量。
临时解决方法:
更改php 5.3.x里的php.ini设置,设置 request_order 为 GPC。
有关php中全局变量的漏洞与临时解决方法,就介绍这些了,希望对大家有所帮助吧。
分享一个php与mysql数据库类,良好的兼容了php4与php5。
代码如下:
<?php
// mysql数据库类
// Version 1.01
class clsTbsSql {
var $Id;
var $Mode;
function clsTbsSql($srv='',$uid='',$pwd='',$db='',$drv='') {
// Default values (defined here to be compatible with both PHP 4 & 5)
$this->Id = false;
$this->Mode = 1; // 0=silent, 1=normal, 2=debug, 3=trace
// Try to connect when instance is created
if ($srv!=='') $this->Connect($srv,$uid,$pwd,$db,$drv);
$this->_Dbs_Prepare();
// Hook for the TinyButStrong Template Engine
$GLOBALS['_TBS_UserFctLst']['k:tbssql'] = array('type'=>4,'open'=>array(&$this,'_Dbs_RsOpen'),'fetch'=>array(&$this,'_Dbs_RsFetch'),
'close'=>array(&$this,'_Dbs_RsClose'));
}
// Public methods
function Connect($srv,$uid,$pwd,$db,$drv='') {
$this->Id = $this->_Dbs_Connect($srv,$uid,$pwd,$db,$drv);
if ($this->Id===false) return $this->_SqlError(false);
return true;
}
function Close() {
if ($this->Id!==false) $this->_Dbs_Close();
}
function Execute($Sql) {
$ArgLst = func_get_args();
$this->_SqlProtect($Sql,$ArgLst,1);
$RsId = $this->_Dbs_RsOpen(null,$Sql);
if ($RsId===false) return $this->_SqlError($this->Id);
$this->_Dbs_RsClose($RsId);
return true;
}
function Value($DefVal,$Sql) {
$ArgLst = func_get_args();
$this->_SqlProtect($Sql,$ArgLst,2);
$RsId = $this->_Dbs_RsOpen(null,$Sql);
if ($RsId===false) return $this->_SqlError($this->Id);
$Row = $this->_Dbs_RsFetch($RsId);
if ($Row===false) {
$x = $DefVal;
} else {
$x = reset($Row);
}
$this->_Dbs_RsClose($RsId);
return $x;
}
function Row1($Sql) {
$ArgLst = func_get_args();
$this->_SqlProtect($Sql,$ArgLst,1);
$RsId = $this->_Dbs_RsOpen(null,$Sql);
if ($RsId===false) return $this->_SqlError($this->Id);
$x = $this->_Dbs_RsFetch($RsId);
$this->_Dbs_RsClose($RsId);
return $x;
}
function Rows($Sql) {
$ArgLst = func_get_args();
$this->_SqlProtect($Sql,$ArgLst,1);
$RsId = $this->_Dbs_RsOpen(null,$Sql);
if ($RsId===false) return $this->_SqlError($this->Id);
$x = array();
while ($r = $this->_Dbs_RsFetch($RsId)) {
$x[] = $r;
}
$this->_Dbs_RsClose($RsId);
return $x;
}
function AffectedRows() {
return $this->_Dbs_AffectedRows();
}
function LastRowId() {
return $this->_Dbs_LastRowId();
}
// Private methods
function _SqlError($ObjId) {
if ($this->Mode==0) return;
$x = 'Database error message: '.$this->_Dbs_Error($ObjId);
if ($this->Mode==2) $x .= "\r\nSQL = ".$this->LastSql;
$this->_SqlMsg($x);
return false;
}
function _SqlMsg($Txt,$Color='#FF0000') {
if ($this->Mode!=0) {
echo '<div .$Color.';">[TbsSql] '.nl2br(htmlentities($Txt)).'</div>'."\r\n";
flush();
}
}
function _SqlDate($Date,$Mode) {
// Return the date formated for the current Database
if (is_string($Date)) {
$x = strtotime($Date);
if (($x===-1) or ($x===false)) {
// display error message
$this->_SqlMsg('Date value not recognized: '.$Date);
$Mode = 0; // Try with the string mode
$x = $Date;
}
} else {
$x = $Date;
}
return $this->_Dbs_Date($x,$Mode);
}
function _SqlProtect(&$Sql,&$ArgLst,$IdxStart) {
// Replace all %i% and @i@ figures by corresponding protected values
$IdxMax = count($ArgLst) - 1;
for ($i=$IdxStart;$i<=$IdxMax;$i++) {
// Simple value
$n = $i - $IdxStart + 1;
$tag = '%'.$n.'%';
if (strpos($Sql,$tag)!==false) {
$x = $this->_Dbs_ProtectStr(''.$ArgLst[$i]);
$Sql = str_replace($tag,$x,$Sql) ;
}
// String value
$tag = '@'.$n.'@';
if (strpos($Sql,$tag)!==false) {
$x = '\''.$this->_Dbs_ProtectStr(''.$ArgLst[$i]).'\'';
$Sql = str_replace($tag,$x,$Sql) ;
}
// Date value
$tag = '#'.$n.'#';
if (strpos($Sql,$tag)!==false) {
$x = $this->_SqlDate($ArgLst[$i],1);
$Sql = str_replace($tag,$x,$Sql) ;
}
// Date and time value
$tag = '~'.$n.'~';
if (strpos($Sql,$tag)!==false) {
$x = $this->_SqlDate($ArgLst[$i],2);
$Sql = str_replace($tag,$x,$Sql) ;
}
}
if ($this->Mode==2) {
$this->LastSql = $Sql;
} elseif ($this->Mode==3) {
$this->_SqlMsg('Trace SQL: '.$Sql,'#663399');
}
}
// -------------------------------
// 指定数据库系统
// -------------------------------
// Database Engine: MySQL
// Version 1.02
function _Dbs_Prepare() {
if (@mysql_ping()) { // Check if a MySQL connection already exist
$this->Id = true;
}
}
function _Dbs_Connect($srv,$uid,$pwd,$db,$drv) {
$Id = @mysql_connect($srv,$uid,$pwd);
if (($Id!==false) and ($db!=='')) {
if (!@mysql_select_db($db)) $this->_SqlError(false);
}
return $Id;
}
function _Dbs_Close() {
if (is_resource($this->Id)) {
return @mysql_close($this->Id);
} else {
return @mysql_close();
}
}
function _Dbs_Error($ObjId) {
if (is_resource($this->Id)) {
return @mysql_error($ObjId);
} else {
return @mysql_error();
}
}
function _Dbs_RsOpen($Src,$Sql) {
// $Src is only for compatibility with TinyButStrong
if (is_resource($this->Id)) {
return @mysql_query($Sql,$this->Id);
} else {
return @mysql_query($Sql);
}
}
function _Dbs_RsFetch(&$RsId) {
return mysql_fetch_assoc($RsId);
}
function _Dbs_RsClose(&$RsId) {
if ($RsId===true) return true;
return @mysql_free_result($RsId);
}
function _Dbs_ProtectStr($Txt) {
return mysql_real_escape_string($Txt);
}
function _Dbs_Date($Timestamp,$Mode) {
switch ($Mode) {
case 1:
// Date only
return '\''.date('Y-m-d',$Timestamp).'\'';
case 2:
// Date and time
return '\''.date('Y-m-d H:i:s',$Timestamp).'\'';
case 0:
// Value is a string
return '\''.$this->_Dbs_ProtectStr($Timestamp).'\'';
default:
// Error in date recognization
return '\'0000-00-00\'';
}
}
function _Dbs_LastRowId() {
return $this->Value(false,'SELECT LAST_INSERT_ID()');
}
function _Dbs_AffectedRows() {
if (is_resource($this->Id)) {
return mysql_affected_rows($this->Id);
} else {
return mysql_affected_rows();
}
}
}
?>
这是小编见过的,相当牛X的一个mysql数据库类了,如果你有幸阅读读了这里,那么恭喜您。
在以后的php与mysql数据库操作方面,您即将成为高手了。