How to declare an array variable as global in PHP securely?

Asked

Viewed 202 times

-1

In the function below I receive a message that the form used is not safe... and even ignoring I am unable to collect the contents of the variable $a after function

$a = array();

function teste1(){
  global $a;
  $a[] = "Ola";
  $a[] = "Passei";
  $a[] = "Por";
  $a[] = "Aqui"; 
}

function teste2(){
  global $a;
  $a[] = "Também";
  $a[] = "Passei";
  $a[] = "Por";
  $a[] = "Aqui"; 
}

var_dump($a); // Aqui esta vindo vazia

This is a basic example, in the real world the function involves several layers that per hour would be impossible to return $a how to Return the test function.

I need to log and I need to fill the array with several strings throughout the process and at the end write a text file, but at that point my problem is to declare a variable that can be used outside the function, because it goes through several functions.

  • Your question opens a range of solutions, it is no use to us to answer what is there, everything is assumption, maybe what you also intend to do is not the best solution, access a global variable is not always a good way, everything depends, everything depends also on context, time, place and form. The bad thing about these questions is that they don’t portray your real problem but always assumptions and this goes against the site.

  • I get it... but look at this, I have an old complex system that I can’t just rewrite... for now I would need a global array variable to debug

  • You now edited and put more information, face without having a logical study pass the variable $a by reference or return at the end over the old values.

  • Marcelo you put a variable at the beginning of script you have access to the end, so face depends on how this system is done is a page with everything, I really asked for more clarification on your question because this is not how it should be done.

1 answer

1

You must communicate the data with the function. Functions receive and return values.

That code makes no sense, but I’ll show you what I’d do if he did:

function teste($a) {
    $a[] = "Ola";
    $a[] = "Passei";
    $a[] = "Por";
    $a[] = "Aqui";
    return $a;
}

function teste2($a) {
    $a[] = "Também";
    $a[] = "Passei";
    $a[] = "Por";
    $a[] = "Aqui"; 
    return $a;
}
$a = Array();
$a = teste($a);
$a = teste2($a);
var_dump($a);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

So I create a variable outside, it’s not global, step as an argument to the function, modify its value and return this value, always communicating without making anything global, everything respects the scope and is organized.

Note that it is the same size, so there is no reason to do the worst, although after giving more context the global would not be so bad.

You can do with reference there does not need the return, but this is more advanced and do not find interesting in PHP in most situations.

Variables scope and you need to understand this to be able to program correctly.

Avoid using global variable whenever you do not have a very strong need and some use if understand very well all the consequences, consider yourself prohibited to use without deeply understanding the problems that can occur with its use:

  • I understood your example, but my need is another... I can’t just take the function’s Return because the variable $a will be used within several functions to collect data to be analyzed at the end of the whole process, I will edit the question to be clearer

  • Your question does not speak of another need, I can only answer for what has been asked. And in fact there is almost never this need that you are saying now, which is also unclear. With editing the example makes even less sense. But still worth what I said, the only thing that changes is that it passes value too.

  • I thought I could create a global variable only to debug an old script I have and then discard it... is more a need in development time, will not go into production so I am not putting the question security, but it was worth the clarifications

  • If you read the links you’ll see that you can, but it depends on what you want to do.

  • Yes I read and tried some examples, but the variable was still blank, reading the php manual more deeply I saw that the option was removed in the last https://www.php.net/manual/en/security.globals.phpversions, I’m going to go to other alternatives

  • @Marcelo Did the answer solve what was in doubt? Do you need something better? Do you think it is possible to accept it now?

  • Honestly, from what I could tell from the answers, I was hoping someone would come and say, What you are wanting is not possible I would accept calmly... but I think I was not clear on the issue and I do not know how to put to be clearer without causing a problem to the list I ended up doing otherwise as I said was just a branch break to do a debug in a old code, how should I close this question?

  • Okay, the answer is this, you don’t want it, but it’s possible to do it and it’s like this, I’ve demonstrated it clearly by running and it’s not even a bigger code. Stay to instruct other people.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.