常见危险函数和特殊函数

PHP
eval()
eval() 函数把字符串按照 PHP 代码来计算。

该字符串必须是合法的 PHP 代码,且必须以分号结尾。

如果没有在代码字符串中调用 return 语句,则返回 NULL。如果代码中存在解析错误,则 eval() 函数返回 false。

<?php $string = "beautiful"; $time = "spring"; $str = 'This is a $string $time morning!'; echo $str. "
"; eval("\$str = \"$str\";"); echo $str; ?>

assert() 会检查指定的 assertion 并在结果为 FALSE 时采取适当的行动。
如果 assertion 是字符串,它将会被 assert() 当做 PHP 代码来执行。 assertion 是字符串的优势是当禁用断言时它的开销会更小,并且在断言失败时消息会包含 assertion 表达式。 这意味着如果你传入了 boolean 的条件作为 assertion,这个条件将不会显示为断言函数的参数;在调用你定义的 assert_options() 处理函数时,条件会转换为字符串,而布尔值 FALSE 会被转换成空字符串。

require()
require 在出错时产生 E_COMPILE_ERROR 级别的错误。换句话说将导致脚本中止而 include 只产生警告(E_WARNING),脚本会继续运行。

<?php require('somefile.php'); ?>

The following:

<?php require 'somefile.php'; ?>

当一个文件被包含时,其中所包含的代码继承了 include 所在行的变量范围。从该处开始,调用文件在该行处可用的任何变量在被调用的文件中也都可用。不过所有在包含文件中定义的函数和类都具有全局作用域。

<?php $color = 'green'; $fruit = 'apple'; ?>

test.php

<?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?>

require_once
require_once 语句和 require 语句完全相同,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含。

<?php define('__ROOT__', dirname(dirname(__FILE__))); require_once(__ROOT__.'/config.php'); ?>

exec()
exec() 执行 command 参数所指定的命令。

<?php // 输出运行中的 php/httpd 进程的创建者用户名 // (在可以执行 "whoami" 命令的系统上) echo exec('whoami'); ?>

同 exec() 函数类似, passthru() 函数 也是用来执行外部命令(command)的。 当所执行的 Unix 命令输出二进制数据, 并且需要直接传送到浏览器的时候, 需要用此函数来替代 exec() 或 system() 函数。 常用来执行诸如 pbmplus 之类的可以直接输出图像流的命令。 通过设置 Content-type 为 image/gif, 然后调用 pbmplus 程序输出 gif 文件, 就可以从 PHP 脚本中直接输出图像到浏览器。

proc_open ( string $cmd , array KaTeX parse error: Expected 'EOF', got '&' at position 24: …orspec , array &̲pipes [, string $cwd [, array $env [, array $other_options ]]] ) : resource
类似 popen() 函数, 但是 proc_open() 提供了更加强大的控制程序执行的能力。

shell_exec — 通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回。

说明
shell_exec ( string $cmd ) : string

<?php $output = shell_exec('ls -lart'); echo "
$output
"; ?>

copy()
copy ( string $source , string $dest [, resource $context ] ) : bool

<?php $file = 'example.txt'; $newfile = 'example.txt.bak'; if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; } ?>

和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE。

file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

<?php $homepage = file_get_contents('http://www.example.com/'); echo $homepage; ?> <?php // <= PHP 5 $file = file_get_contents('./people.txt', true); // > PHP 5 $file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH); ?>

file_put_contents()
和依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

parse_str()
( string KaTeX parse error: Expected 'EOF', got '&' at position 25: …tring [, array &̲result ] ) : void
如果 encoded_string 是 URL 传递入的查询字符串(query string),则将它解析为变量并设置到当前作用域(如果提供了 result 则会设置到该数组里 )。
encoded_string
输入的字符串。
result
如果设置了第二个变量 result, 变量将会以数组元素的形式存入到这个数组,作为替代。

glob ( string $pattern [, int $flags = 0 ] ) : array
盖布()函数依照 libc glob() 函数使用的规则寻找所有与pattern匹配的文件路径,类似于一般 shells 所用的规则一样。不进行缩写扩展或参数替代。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值