0

I'm trying to replace any global variables in my example to a specific value $var as shows in the following example:

(example.php)

<?php 
// before
$firstname = $_GET['firstname'];
$lastname = $_POST['lastname'];
$age = $_REQUEST['age'];
?>

As shown in the example above, I want to change any global variables $_POST, $_GET, $_REQUEST automatically in the php file to specific value $var in the php file.

Here is what I did to get each line and check if the line of code have $_POST or $_GET or $_REQUEST, then I'm trying to change any global variables in the file to specific value $var.

(test.php)

<?php
$file = file_get_contents("example.php");
$lines = explode("\n", $file);

$var = '$var';
foreach ($lines as $key => &$value) {

if(strpos($value, '$_GET') !== false){
     // Replace $_GET['firstname'] and put $var
}
elseif(strpos($value, '$_POST') !== false){
     // Replace $_POST['lastname'] and put $var
}
elseif(strpos($value, '$_REQUEST') !== false){
     // Replace $_REQUEST['age'] and put $var
}

}
?>

The expected results to be after replace any global variables to $var is as following:

(example.php)

<?php

// The expected results to be after replace all global variables by $var
// This is how I expect the file to looks like after replace

$firstname = $var;
$lastname = $var;
$age = $var;

?>

I appreciated if anyone anyone can help me to find a suitable way to replace any $_GET, $_POST, $_REQUEST exist in the file by $var.

  • note: I want to replace any $_GET[], $_POST[], $_REQUEST by $var, $var to be stored as following:
<?php
$firstname = $var;  // Just change text (remove $_GET['firstname', and put $var] in php file
$lastname = $var;  // Just change text (remove $_POST['lastname', and put $var] in php file
$age = $var;  // Just change text (remove $_REQUEST['age', and put $var] in php file
?>
  • Note: This is how I hope the php file to be looks like.
Abd AM
  • 119
  • 3
  • 14
  • $_REQUEST is the same as doing either $_GET or $_POST so you can probably just use $_REQUEST instead of the other two. – Mark Nov 30 '19 at 13:05
  • my issue is not about `REQUEST` or `POST` or `GET`, I want to replace all global variables in the php file to `$var` ,, see the `example.php` before and after please – Abd AM Nov 30 '19 at 13:07
  • I am aware, that's why I wrote my advice in a comment, and not an answer. – Mark Nov 30 '19 at 13:07
  • Thanks @Mark Overton, Kindly if u have an idea how to replace them automatically in the PHP context file – Abd AM Nov 30 '19 at 13:08
  • Just for info, why do you want to do that? – Toto Nov 30 '19 at 13:13
  • I want to pass a value later for all global variables in the file. – Abd AM Nov 30 '19 at 13:30

3 Answers3

4

Is that what you want?

$file = file_get_contents("example.php");
$lines = explode("\n", $file);

$var = '$var';
foreach ($lines as $key => &$value) {

    if(strpos($value, '$_GET') !== false){
        $value = preg_replace('/\$_GET\[.+?\]/', $var, $value);
    }
    elseif(strpos($value, '$_POST') !== false){
        $value = preg_replace('/\$_POST\[.+?\]/', $var, $value);
    }
    elseif(strpos($value, '$_REQUEST') !== false){
        $value = preg_replace('/\$_REQUEST\[.+?\]/', $var, $value);
    }
}
Toto
  • 89,455
  • 62
  • 89
  • 125
  • yes, exactly. But the `example.php` not changed ! @Toto – Abd AM Nov 30 '19 at 13:24
  • @AbdallaWasef: Just write to outputfile all the lines contained in `$lines` array. – Toto Nov 30 '19 at 13:26
  • Not me, I still not vote to anyone .. Someone else hehe – Abd AM Nov 30 '19 at 13:27
  • What u mean about writing to output file all line ? – Abd AM Nov 30 '19 at 13:28
  • I tried: `file_put_contents("example.php", implode("\n", $lines));` but it is not change anything in the file – Abd AM Nov 30 '19 at 13:31
  • @AbdallaWasef: Works for me, do you have the write access to the outputfile? – Toto Nov 30 '19 at 13:39
  • @AbdallaWasef: Don't worry about downvote, I'm sure you didn't. I just ask the anonymous downvoter why they've downvoted. – Toto Nov 30 '19 at 13:41
  • Great man, yes it is `file_put_contents(dataset.php): failed to open stream: Permission denied` .. bcz using XAMPP on Mac . will try solve this issue now and update u – Abd AM Nov 30 '19 at 13:42
  • I fixed the permission issue but still not changed, I will try with other laptop, maybe 'Window' to be far from the permission issue. – Abd AM Nov 30 '19 at 13:50
  • Anyway, u solve the main issue, Thanks @Toto. Sure this is the right answer – Abd AM Nov 30 '19 at 13:50
  • What if you had a mixture of say `$_GET` and `$_POST` on the same line? – Booboo Nov 30 '19 at 22:33
2

See Regex Demo

/\$_(GET|POST|REQUEST)\[[^\]]*\]/' will match, for example, $_GET[anything-other-than-a-right-bracket] and all we have to do is replace it with $var and rewrite the file:

<?php
$file = file_get_contents("example.php");
$file = preg_replace('/\$_(GET|POST|REQUEST)\[[^\]]*\]/', '$var', $file);
file_put_contents("example.php", $file);
Booboo
  • 38,656
  • 3
  • 37
  • 60
1

Solution:

The following code will turn $_REQUEST['thisvar'] into $thisvar, as well as any other $_GET/$_POST variables you have set.

As mentioned in the comments $_REQUEST covers both $_GET and $_POST.

foreach($_REQUEST as $key => $value) $$key = $value;

If I modify your example:

$file = file_get_contents("example.php");
$lines = explode("\n", $file);

foreach($lines as $key => $value) $$key = $value;
Mark
  • 1,852
  • 3
  • 18
  • 31
  • please can u reflect this code to my example, and show start from `foreach` loop how it will works to replace all global variables to `$var`. – Abd AM Nov 30 '19 at 13:11
  • I add a note in my question to be more clear, I want the `example.php` file to looks like ` $firstname = $var; ` , please have a look for the question – Abd AM Nov 30 '19 at 13:16