Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Security Detection in PHP

Shulou Source: shulou.com Published: 2022-06-03 07:18:21 11月12日 Update

Editor to share with you how to achieve PHP security detection, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The code is as follows:

/ * *

* html conversion output (only escape'"keep Html running normally)

* @ param $param

* @ return string

, /

Function htmlEscape ($param) {

Return trim (htmlspecialchars ($param, ENT_QUOTES))

}

/ * *

* whether it is an array (also detect whether there is a value in the array)

* @ param $params

* @ return boolean

, /

Function isArray ($params) {

Return (! is_array ($params) | |! count ($params))? False: true

}

/ * *

* whether the variable exists in the array (parameter fault tolerance, whether the string exists in the array)

* @ param $param

* @ param $params

* @ return boolean

, /

Function inArray ($param, $params) {

Return (! in_array ((string) $param, (array) $params)? False: true

}

/ * *

* General multi-type mixed escape function

* @ param $var

* @ param $strip

* @ param $isArray

* @ return mixture

, /

Function sqlEscape ($var, $strip = true, $isArray = false) {

If (is_array ($var)) {

If (! $isArray) return "'"

Foreach ($var as $key = > $value) {

$var [$key] = trim (S::sqlEscape ($value, $strip))

}

Return $var

} elseif (is_numeric ($var)) {

Return. $var. "'"

} else {

Return. Addslashes ($strip? Stripslashes ($var): $var). "'"

}

}

/ * *

* get server variables

* @ param $keys

* @ return string

, /

Function getServer ($keys) {

$server = array ()

$array = (array) $keys

Foreach ($array as $key) {

$server [$key] = NULL

If (isset ($_ SERVER [$key])) {

$server [$key] = str_replace (array (','% 3C,','), $_ SERVER [$key])

}

}

Return is_array ($keys)? $server: $server [$keys]

}

/ * *

* variable escape

* @ param $array

, /

Function slashes (& $array) {

If (is_array ($array)) {

Foreach ($array as $key = > $value) {

If (is_array ($value)) {

S::slashes ($array [$key])

} else {

$array [$key] = addslashes ($value)

}

}

}

}

/ * *

* Directory conversion

* @ param unknown_type $dir

* @ return string

, /

Function escapeDir ($dir) {

$dir = str_replace (array ("'",'#','=','','$','%','&',';),'', $dir)

Return rtrim (preg_replace ('/ (\ /) {2,} | (\\) {1,} /,'/', $dir),'/')

}

/ * *

* generic multi-type conversion

* @ param $mixed

* @ param $isint

* @ param $istrim

* @ return mixture

, /

Function escapeChar ($mixed, $isint = false, $istrim = false) {

If (is_array ($mixed)) {

Foreach ($mixed as $key = > $value) {

$mixed [$key] = S::escapeChar ($value, $isint, $istrim)

}

} elseif ($isint) {

$mixed = (int) $mixed

} elseif (! is_numeric ($mixed) & & ($istrim? $mixed = trim ($mixed): $mixed) & & $mixed) {

$mixed = S::escapeStr ($mixed)

}

Return $mixed

}

/ * *

* character conversion

* @ param $string

* @ return string

, /

Function escapeStr ($string) {

$string = str_replace (array ("\ 0", "", "\ r"),'', $string); / / modified@2010-7-5

$string = preg_replace (array ('/ [\\ x00 -\\ x08\\ x0B\\ x0C\\ x0E -\\ x1F] /','/! (# [0-9] + | [Amurz] +);) / is'), array ('','&'), $string)

$string = str_replace (array ("% 3C",'', $string)

$string = str_replace (array ('','',\ t','), array ('', & # 39), $string)

Return $string

}

/ * *

* variable check

* @ param $var

, /

Function checkVar (& $var) {

If (is_array ($var)) {

Foreach ($var as $key = > $value) {

S::checkVar ($var [$key])

}

} elseif (paid W! = 'admincp') {

$var = str_replace (array ('..,')','

Tags: Variables arrays detection articles escape security security detection content multi-types characters not much code functions parameters and at the same time most strings more servers directories Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker Microsoft MySQL vpn