What is the best practice for determining whether a PHP page was called using a POST or GET method?
To determine whether a PHP page was called using a POST or GET method, you can check the `$_SERVER['REQUEST_METHOD']` variable. This variable contains the request method used to access the page. You can then use an if statement to check if it equals 'POST' or 'GET' to determine the method used.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Page was called using POST method
echo "Page was called using POST method";
} elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
// Page was called using GET method
echo "Page was called using GET method";
} else {
// Handle other request methods
echo "Unsupported request method";
}
Keywords
Related Questions
- What are the best practices for using header() function in PHP to avoid "headers already sent" error?
- What are the limitations of using PHP functions on a web server that only supports HTML files?
- What are the potential pitfalls of using a .htaccess file in PHP, especially when trying to redirect requests to a specific file?