[英]How to use PHP-Parser to get the global variables name and change it
我想使用PHP-Parser庫來獲取全局方法( _POST, _GET, _REQUEST
)以獲取 PHP 中的值。 我正在使用PHP-Parser ,我想檢查節點名稱是否等於 ( _POST, _GET, _REQUEST
)。 我仍然是 PHP-Parser 的初學者,不知道如何獲取這些全局變量。 例如,如果我有以下源代碼:
代碼:
<?php
$firstname = $_POST['firstname];
到目前為止,我使用的 PHP 解析器如下所示:
<?php
require_once('vendor/autoload.php');
use PhpParser\Error;
use PhpParser\NodeDumper;
use PhpParser\ParserFactory;
$code = <<<'CODE'
<?php
$firstname= $_POST['firstname'];
CODE;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$ast = $parser->parse($code);
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
return;
}
use PhpParser\{Node, NodeTraverser, NodeVisitorAbstract};
$traverser = new NodeTraverser;
// This code exist in the PHP-Parser documentation
$traverser->addVisitor(new class extends NodeVisitorAbstract {
public function leaveNode(Node $node) {
if ($node instanceof Node\Stmt\Expression
&& $node->expr instanceof Node\Expr\FuncCall
&& $node->expr->name instanceof Node\Name
&& $node->expr->name->toString() === '_POST'
) {
// Change the $_POST['firstname'] and replace it with XXX value
}
}
});
print_r($modifiedStmts = $traverser->traverse($ast) );
執行上述 PHP-Parser AST 后,得到以下結果:
Array ( [0] => PhpParser\Node\Stmt\Expression Object ( [expr] => PhpParser\Node\Expr\Assign Object ( [var] => PhpParser\Node\Expr\Variable Object ( [name] => firstname [attributes:protected] => Array ( [startLine] => 2 [endLine] => 2 ) ) [expr] => PhpParser\Node\Expr\ArrayDimFetch Object ( [var] => PhpParser\Node\Expr\Variable Object ( [name] => _POST [attributes:protected] => Array ( [startLine] => 2 [endLine] => 2 ) ) [dim] => PhpParser\Node\Scalar\String_ Object ( [value] => firstname [attributes:protected] => Array ( [startLine] => 2 [endLine] => 2 [kind] => 1 ) ) [attributes:protected] => Array ( [startLine] => 2 [endLine] => 2 ) ) [attributes:protected] => Array ( [startLine] => 2 [endLine] => 2 ) ) [attributes:protected] => Array ( [startLine] => 2 [endLine] => 2 ) ) )
_POST, _GET, REQUEST
? 例如:// Something like this
if($node->value === '_POST' OR $node->value === '_GET' or $node->value === '_REQUEST')
{
// Do next step to change the global variable to specific text value
}
hello world! or any text
更改$_POST[firstname]
hello world! or any text
hello world! or any text
值? 例如:之前$firstname = $_POST['firstname'];
后
$firstname = "hello World!";
謝謝你的幫助
這應該適用於您突出顯示的特定實例,它只適用於 POST 實例,但這應該很容易擴展。
主要部分是當您看到代碼的 AST 時,請嘗試確保您可以識別_POST
訪問的基礎。 結果是一個Node\\Expr\\ArrayDimFetch
,然后在它里面你想檢查它使用的變量是否是_POST
。
一旦你確定了這一點,你可以用一個新的節點替換這個節點,它只是一個字符串Node\\Scalar\\String_("Hello World!");
.
$traverser->addVisitor(new class extends NodeVisitorAbstract {
public function leaveNode(Node $node) {
if ($node instanceof Node\Expr\ArrayDimFetch
&& $node->var instanceof Node\Expr\Variable
&& $node->var->name === '_POST'
) {
// Change the $_POST['firstname'] and replace it with XXX value
return new Node\Scalar\String_("Hello World!");
}
}
});
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
echo $prettyPrinter->prettyPrintFile($traverser->traverse($ast));
從你的原始代碼
<?php
$firstname= $_POST['firstname'];
此代碼輸出....
<?php
$firstname = 'Hello World!';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.