php小代码

20 篇文章 0 订阅

常用函数

stripslashes(): 去掉反斜杠\。比如,\'变成'\\变成\
在这里插入图片描述
addslashes():对单引号、双引号、反斜杠、空字节等需要escape的字符增加反斜杠\
在这里插入图片描述

get_magic_quotes_gpc()
在PHP 5.4.0之前,PHP的magic_quotes_gpc这个directive默认是on的,并且它会对所有GET, POST and COOKIE数据进行addslashes()操作,所以不该再对已经addslashes() 对数据再次addslashes(),不然失去了addslashes()的功效。于是,get_magic_quotes_gpc()就是用来判断PHP的magic_quotes_gpc这个directive是否为on的。

生成随机数

            // 'Reset code' generation
            $reset_code = random_string();
            $reset_code = hash("sha1", $reset_code, false);

CSRF检查

来自dedecms V5.7 SP2

function csrf_check()
{
    global $token;

    if(!isset($token) || strcasecmp($token, $_SESSION['token']) != 0){
        echo '<a href="http://bbs.dedecms.com/907721.html">DedeCMS:CSRF Token Check Failed!</a>';
        exit;
    }
}

测试

>>> $cqq = array('cqq', 'hello', 'world');
=> [
     "cqq",
     "hello",
     "world",
   ]
>>> $cqq = implode('|', $cqq);
=> "cqq|hello|world"
>>> $num = array(1,2,3,4);
=> [
     1,
     2,
     3,
     4,
   ]
>>> $num = implode('\\', $num)
=> "1\2\3\4"

看到implode()的作用就是在一个数组的每个元素之间插入一个字符串,然后得到一个新的字符串啊。对int类型也适用,反正最终要输出一个字符串。

拦截白名单

参考:
https://www.leavesongs.com/PENETRATION/360webscan-bypass.html

/**
 *  拦截目录白名单
 */
function webscan_white($webscan_white_name,$webscan_white_url_t=array()) {
  $url_path=$_SERVER['PHP_SELF'];
  $url_var=$_SERVER['QUERY_STRING'];
  if (preg_match("/".$webscan_white_name."/is",$url_path)==1) {
    return false;
  }
  foreach ($webscan_white_url_t as $webscan_white_url) {
	  foreach ($webscan_white_url as $key => $value) {
		if(!empty($url_var)&&!empty($value)){
		  if (stristr($url_path,$key)&&stristr($url_var,$value)) {
			return false;
		  }
		}
		elseif (empty($url_var)&&empty($value)) {
		  if (stristr($url_path,$key)) {
			return false;
		  }
		}

	  }
  }
  return true;
}

套路

index.php

<?php
    require_once('class.php');
    if($_SESSION['username']) {
        header('Location: profile.php');
    }
?>

而在class.php

<?php
    //...
    session_start();
?>

不然在index.php$_SESSION对象的值为空。

在一个php文件中后面写class,前面做测试

<?php
//session_start();
//var_dump($_SESSION)

function filter($string) {
     // 这里的array中的两个元素一个是' 另一个是\\。加上\是为了转义
     $escape = array('\'', '\\\\');
     // print_r()函数比var_dump()更易读
     print_r($escape);
     // implode用于向一个数组的每个元素之间插入一个字符串,然后得到一个新的字符串
     $escape = '/' . implode('|', $escape) . '/';
     print_r($escape);
     $string = preg_replace($escape, '_', $string);

     $safe = array('select', 'insert', 'update', 'delete', 'where');
     // implode用于向一个数组的每个元素之间插入一个字符串,然后得到一个新的字符串
     $safe = '/' . implode('|', $safe) . '/i';
     return preg_replace($safe, 'hacker', $string);
}
// 加入@就可以避免终端输出错误信息
@filter();

function test_serialize(){
    $cqq = new User("caiqiqi", "qcc");
    echo serialize($cqq);
    echo "\n";
    echo gettype($cqq);
    //var_dump($cqq);

}

function test_class(){
    $user = new User("caiqiqi", "qcc");
    echo $user->__tostring();
}

//test_class();

class User{

    private $name = null;
    private $pass = null;

    function __construct($pname, $ppasswd){

        $this->name = $pname;
        $this->pass = $ppasswd;
    }

    public function __tostring(){
        return __class__;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值