<?php
/*来源:phpwind.net*/
Class DB {
var $query_num = 0;
function DB($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
}
function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
$pconnect==0 ? @mysql_connect($dbhost, $dbuser, $dbpw) : @mysql_pconnect($dbhost, $dbuser, $dbpw);
mysql_errno()!=0 && $this->halt("Connect($pconnect) to MySQL failed");
if($this->server_info() > '4.1' && $GLOBALS['charset']){
mysql_query("SET NAMES '".$GLOBALS['charset']."'");
}
if($this->server_info() > '5.0'){
mysql_query("SET sql_mode=''");
}
if($dbname) {
if (!@mysql_select_db($dbname)){
$this->halt('Cannot use database');
}
}
}
function close() {
return mysql_close();
}
function select_db($dbname){
if (!@mysql_select_db($dbname)){
$this->halt('Cannot use database');
}
}
function server_info(){
return mysql_get_server_info();
}
function query($SQL,$method='') {
$GLOBALS['PW']=='pw_' or $SQL=str_replace('pw_',$GLOBALS['PW'],$SQL);
if($method=='U_B' && function_exists('mysql_unbuffered_query')){
$query = mysql_unbuffered_query($SQL);
}else{
$query = mysql_query($SQL);
}
$this->query_num++;
//echo $SQL.'<br>'.$this->query_num.'<br>';
if (!$query) $this->halt('Query Error: ' . $SQL);
return $query;
}
function get_one($SQL){
$query=$this->query($SQL,'U_B');
$rs =& mysql_fetch_array($query, MYSQL_ASSOC);
return $rs;
}
function pw_update($SQL_1,$SQL_2,$SQL_3){
$rt=$this->get_one($SQL_1);
if($rt){
$this->update($SQL_2);
} else{
$this->update($SQL_3);
}
}
function update($SQL) {
$GLOBALS['PW']=='pw_' or $SQL=str_replace('pw_',$GLOBALS['PW'],$SQL);
if($GLOBALS['db_lp']==1){
if(substr($SQL,0,7)=='REPLACE'){
$SQL=substr($SQL,0,7).' LOW_PRIORITY'.substr($SQL,7);
} else{
$SQL=substr($SQL,0,6).' LOW_PRIORITY'.substr($SQL,6);
}
}
if(function_exists('mysql_unbuffered_query')){
$query = mysql_unbuffered_query($SQL);
}else{
$query = mysql_query($SQL);
}
$this->query_num++;
//echo $SQL.'<br>'.$this->query_num.'<br>';
if (!$query) $this->halt('Update Error: ' . $SQL);
return $query;
}
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
function affected_rows() {
return mysql_affected_rows();
}
function num_rows($query) {
$rows = mysql_num_rows($query);
return $rows;
}
function free_result($query) {
return mysql_free_result($query);
}
function insert_id() {
$id = mysql_insert_id();
return $id;
}
function halt($msg='') {
require_once(R_P.'require/db_mysql_error.php');
new DB_ERROR($msg);
}
}
?>
<?php
/**
* @author YangHuan
* @datetime
* @version 1.0.0
*/
/**
* Short description.
*
* Detail description
* @author
* @version 1.0
* @copyright
* @access public
*/
class Tree
{
/**
* Description
* @var
* @since 1.0
* @access private
*/
var $data = array();
/**
* Description
* @var
* @since 1.0
* @access private
*/
var $child = array(-1=>array());
/**
* Description
* @var
* @since 1.0
* @access private
*/
var $layer = array(-1=>-1);
/**
* Description
* @var
* @since 1.0
* @access private
*/
var $parent = array();
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function Tree ($value)
{
$this->setNode(0, -1, $value);
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function setNode ($id, $parent, $value)
{
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
if (!isset($this->layer[$parent]))
{
$this->layer[$id] = 0;
}
else
{
$this->layer[$id] = $this->layer[$parent] + 1;
}
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getList (&$tree, $root= 0)
{
foreach ($this->child[$root] as $key=>$id)
{
$tree[] = $id;
if ($this->child[$id]) $this->getList($tree, $id);
}
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getValue ($id)
{
return $this->data[$id];
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getLayer ($id, $space = false)
{
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getParent ($id)
{
return $this->parent[$id];
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getParents ($id)
{
while ($this->parent[$id] != -1)
{
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getChild ($id)
{
return $this->child[$id];
} // end func
/**
* Short description.
*
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getChilds ($id = 0)
{
$child = array($id);
$this->getList($child, $id);
return $child;
} // end func
} // end class
?>
使用方法
PHP代码:
<?php
//new Tree(根目录的名字);
//根目录的ID自动分配为0
$Tree = new Tree('根目录');
//setNode(目录ID,上级ID,目录名字);
$Tree->setNode(1, 0, '目录1');
$Tree->setNode(2, 0, '目录2');
$Tree->setNode(3, 0, '目录3');
$Tree->setNode(4, 3, '目录3.1');
$Tree->setNode(5, 3, '目录3.2');
$Tree->setNode(6, 3, '目录3.3');
$Tree->setNode(7, 2, '目录2.1');
$Tree->setNode(8, 2, '目录2.2');
$Tree->setNode(9, 2, '目录2.3');
$Tree->setNode(10, 6, '目录3.3.1');
$Tree->setNode(11, 6, '目录3.3.2');
$Tree->setNode(12, 6, '目录3.3.3');
//getChilds(指定目录ID);
//取得指定目录下级目录.如果没有指定目录就由根目录开始
$category = $Tree->getChilds();
//遍历输出
foreach ($category as $key=>$id)
{
echo $Tree->getLayer($id, '|-').$Tree->getValue($id)."<br>\n";
}
PHP无限分类-PHP100代码
<?php
//无限分类,从子类找所有父类
//$id 子类ID
function php100_xd($id){
$sql="select * from fl where id='$id'";
$q=mysql_query($sql);
$rs=mysql_fetch_array($q);
$rs['fid']==0 ? "" : fl($rs['fid']);
echo $rs['name']."-";
}
//读取所有父类下面的子类
//$f顶级分类从什么开始,$s样式
function php100_dx($f=0,$s=""){
$sql="select * from fl where fid=$f";
$q=mysql_query($sql);
$s=$s."-";
while($rs=mysql_fetch_array($q)){
echo "<br>$s".$rs['name'];
flt($rs['id'],$s);
}
}
验证码的一般思路,就是每次登陆的地方访问一个脚本文件,该文件生成含验证码的图片并将值写入到Session里,提交的时候验证登陆的脚本就会判断提交的验证码是否与Session里的一致。
问题出现了,在登陆密码错误之后,我们不去访问生成验证图片的文件,那么如果Session中的验证码没有被清空,此时验证码就是跟上次的一样,辛辛苦苦构建的验证码机制就形同虚设了。
下面我们先来看一段有问题的代码:
登陆部分:
<td>管理员姓名:td>
<td><input type="text" name="username" />td>
tr>
<tr>
<td>管理员密码:td>
<td><input type="password" name="password" />td>
tr>
<tr>
<td>验证码:td>
<td><input type="text" name="captcha" onkeyup="pressCaptcha(this)" />td>
tr>
<tr>
<td colspan="2" align="right">
<img src="/blog_article/index/act/captcha/amp;1628020115.html" width="145" height="20" alt="CAPTCHA" border="1" onclick= this.src="/blog_article/index/act/captcha/amp;.html"+Math.random() title="看不清?点击更换另一个验证码。" />
td>
tr>
?>
//-- 验证登陆信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'signin')
{
include('../includes/cls_captcha.php');
/* 检查验证码是否正确 */
$validator = new captcha();
if (!$validator->check_word($_POST['captcha']))
{
sys_msg($_LANG['captcha_error'], 1);
}
/* 检查密码是否正确 */
$sql = "SELECT user_id, user_name, password, action_list FROM " .$ecs->table('admin_user').
" WHERE user_name='$_POST[username]' AND password='" .md5($_POST['password']). "'";
$row = $db->GetRow($sql);
if ($row)
{
// 登录成功
set_admin_session($row['user_id'], $row['user_name'], $row['action_list']);
// 更新最后登录时间和IP
$db->Execute("UPDATE " .$ecs->table('admin_user').
" SET last_time='" .date('Y-m-d H:i:s', time()). "', last_ip='" .real_ip(). "'".
" WHERE user_id=$_SESSION[admin_id]") OR die($db->ErrorMsg());
if (isset($_POST['remember']))
{
setcookie('ECSCP[admin_id]', $row[0], time() + 3600 * 24 * 360);
setcookie('ECSCP[admin_pass]', md5($row['password'] . $_CFG['hash_code']), time() + 3600 * 24 * 360);
}
header('location:./');
}
else
{
sys_msg($_LANG['login_faild'], 1);
}
}
?>
大家可以看下面的图片,增强点直观的认识。
解决方法:我们需要在检查密码错误后更新验证码,对于留言等类型的,还要在提交成功后更新验证码。
安全就是这样,我们总是想让自己的程序更安全,但是一般情况下,我们又总是走在常规思维里跳不出来,于是导致我们的程序出现了很多"非常规漏洞",或者叫做"缺陷",总之就是不完美。我写这篇文章除了指出上面这个问题之外,还希望大家都能行动起来,用"非常规"眼光,重新检查下自己的程序,把更多以前自己没有发现的小问题帖出来,让大家共同提高!