本文介绍下,php实现的mysql数据库操作类,此类的特点是可以兼容php4与php5,值得学习借鉴。有需要的朋友参考下吧。
分享一个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 style="color: '.$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数据库操作方面,您即将成为高手了。 |

防止会话固定攻击的有效方法包括:1.在用户登录后重新生成会话ID;2.使用安全的会话ID生成算法;3.实施会话超时机制;4.使用HTTPS加密会话数据,这些措施能确保应用在面对会话固定攻击时坚不可摧。

实现无会话身份验证可以通过使用JSONWebTokens(JWT)来实现,这是一种基于令牌的认证系统,所有的必要信息都存储在令牌中,无需服务器端会话存储。1)使用JWT生成和验证令牌,2)确保使用HTTPS防止令牌被截获,3)在客户端安全存储令牌,4)在服务器端验证令牌以防篡改,5)实现令牌撤销机制,如使用短期访问令牌和长期刷新令牌。

PHP会话的安全风险主要包括会话劫持、会话固定、会话预测和会话中毒。1.会话劫持可以通过使用HTTPS和保护cookie来防范。2.会话固定可以通过在用户登录前重新生成会话ID来避免。3.会话预测需要确保会话ID的随机性和不可预测性。4.会话中毒可以通过对会话数据进行验证和过滤来预防。

销毁PHP会话需要先启动会话,然后清除数据并销毁会话文件。1.使用session_start()启动会话。2.用session_unset()清除会话数据。3.最后用session_destroy()销毁会话文件,确保数据安全和资源释放。

如何改变PHP的默认会话保存路径?可以通过以下步骤实现:在PHP脚本中使用session_save_path('/var/www/sessions');session_start();设置会话保存路径。在php.ini文件中设置session.save_path="/var/www/sessions"来全局改变会话保存路径。使用Memcached或Redis存储会话数据,如ini_set('session.save_handler','memcached');ini_set(

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然后使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

在PHP会话中可以存储数组。1.启动会话,使用session_start()。2.创建数组并存储在$_SESSION中。3.通过$_SESSION检索数组。4.优化会话数据以提升性能。

PHP会话垃圾回收通过概率机制触发,清理过期会话数据。1)配置文件中设置触发概率和会话生命周期;2)可使用cron任务优化高负载应用;3)需平衡垃圾回收频率与性能,避免数据丢失。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

禅工作室 13.0.1
功能强大的PHP集成开发环境