源码如下:
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Check Anti-CSRF token
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// Get input
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target );
// Split the IP into 4 octects
$octet = explode( ".", $target );
// Check IF each octet is an integer
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
// If all 4 octets are int's put the IP back together.
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
echo "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
相关函数如下:
isset()函数
检查变量是否已经设置或不为空
<?php
$myVar = "Hello World";
if(isset($myVar)){
echo "$myVar is set.";
}else{
echo "$myVar is not set.";
}
//输出:Hello World is set.
?>
checkToken()函数
用于检测用户的访问令牌是否有效。
<?php
// 定义checkToken()函数
function checkToken($token) {
// 假设$validToken是一个有效的访问令牌
$validToken = "abc123";
// 验证传入的令牌是否与有效令牌匹配
if ($token === $validToken) {
return true; // 令牌有效
} else {
return false; // 令牌无效
}
}
// 获取用户提交的访问令牌
$userToken = $_POST['token'];
// 调用checkToken()函数验证用户的访问令牌
if (checkToken($userToken)) {
echo "he access token is valid and allows the user to access the page";
} else {
echo "The access token is invalid and access is denied";
}
?>
token校验不通过:
token校验通过:
stripslashes()函数
stripslashes() 函数用于删除由 addslashes() 函数添加的反斜杠。
在某些情况下,需要对字符串进行转义,比如在将数据插入数据库之前。但是在从数据库中取出数据后,可能需要去除这些转义字符,这时就可以使用 stripslashes() 函数。
<?php
$str = "This is some text with a \"quoted\" word";
echo $str;
echo "<br>";
// 添加转义字符
$str = addslashes($str);
echo $str;
echo "<br>";
// 删除转义字符
$str = stripslashes($str);
echo $str;
?>
explode()函数
我们想要将这个字符串按照逗号分隔成一个数组,可以使用explode()函数来实现
<?php
$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);
//输出结果:Array ( [0] => apple [1] => banana [2] => orange )
?>
is_numeric()函数
is_numeric()函数用于检查变量是否是一个数字或数字字符串。
以下是一个示例:
<?php
$num1 = 123;
$num2 = "456";
$str1 = "456abc";
$str2 = "abc";
var_dump(is_numeric($num1)); // 输出: bool(true)
var_dump(is_numeric($num2)); // 输出: bool(true)
var_dump(is_numeric($str1)); // 输出: bool(false)
var_dump(is_numeric($str2)); // 输出: bool(false)
?>
stristr()
搜索字符串并返回从匹配点到字符串结束的部分
php_uname()
用于获取操作系统的信息,包括操作系统的名称、版本号、发布日期等。
shell_exec()
用于执行外部命令并返回输出。
以下是一个示例:
<?php
$output = shell_exec('whoami');
echo "<pre>$output</pre>";//输出结果:win-37qpun7no81\administrator
?>