PHP: Checking if a form was posted with “REQUEST_METHOD” server global

Something so simple, let’s say even in a framework like Symfony which I use all the time you want to just check to see if the incoming request was initiated by an actual form post.  Here’s the best way to check:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   //Do something unique here like redirect or  pass post data to another method
}

I implement this switch in a lot of handlers where I deploy API clients that deliver forms on a site but the forms aren’t actually built from the site — the API pulls the form from some centralized controller.

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *