Replacing global variables, how and why

Ok, so I worked and used my global variables so far, and that is not a problem in just calling $ USER to get the user id.

In most cases, from people on SO and some readings here and there, it was all so negative about using the global var.

So now I am ready to change the way and not use global variables.

I am currently turning on a function, protect (), which checks your session and makes global variables like $ USER, $ USERTYPE.

And then I can just use these variables as much as I want in the file.

What can replace this? I was thinking about creating $ USER = grabUserid (); which will return the user id, and the same for the type of user that already has 2 functions, and I will need to perform a function with database queries for each variable?

And what about my $ connect, which is the PDO object for the database, processing all my queries, I will need to do $ connect = connectdb (); also

What about http requests from js to another php file, I will need to transfer it, but then between them it may be unsafe (if you think about security), since you can just manipulate the user ID between them.

, , ? ? , , , ? ? ?

+5
2

, : .

-, , , :
, , . , ( , ). , :


  • , , . , , , . , . .

  • Concurrency
    , , concurrency . , , , - .

  • //
    , OOPHP, , , . , , . - .

-, :

  • Vars/
    - "" . , , , , , . , , , .

  • Refactor
    , . ? , ( ) $Session? . , , .

, . , , , , .

, " script, ". , " , ", , , , .

+9

.

php . , php , .

, , , , $Session [ "memberID" ], $Session [ "last_page_visited" ] .. , , .

, . , . .

  • . , , .
  • , - .
  • . , , . , ( -).
+2

All Articles