Wrong: use of superglobals

If you uses these in your code, stop.

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

While it’s important to know how PHP handles the request, server, or environment variables, it’s a big mistake to use them directly.

Don’t use low-level PHP. There’s just not any good reason for it. Sticking with low-level PHP is a clear sign of misundersting of the language, its ecosystem and your ability to keep up with the technology.

Solution

PHP has evolved very quickly in the 2010’s and layers of reusable components have emerged. One of them is HttpFoundation. If your project needs to access superglobals, then you need HttpFoundation. Follow the installation instructions in the documentation.

Then, instead of using your superglobals, use this:

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

You need $_GET['param']? Use $request->query->get('param').
You need $_POST['param']? Use $request->request->get('param').

Explore the documentation to know more on how to use this component. But don’t ever use superglobals anymore in your code.

More

Read the series You’re not doing it right to step up your PHP game.

Leave a comment