Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if request is post back in PHP [duplicate]

Tags:

php

How can I check if the request is a post back in PHP, is the below ok?

if (isset($_POST["submit"]))

where submit is the name of the <input type="submit" />

like image 889
Jiew Meng Avatar asked Nov 22 '10 03:11

Jiew Meng


1 Answers

That will work if you know and expect such a submit button on the same page.

If you don't immediately know anything about the request variables, another way is to check the request method:

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')

As pointed out in the comments, to specifically check for a postback and not just any POST request, you need to ensure that the referrer is the same page as the processing page. Something like this:

if (basename($_SERVER['HTTP_REFERER']) == $_SERVER['SCRIPT_NAME'])
like image 157
BoltClock Avatar answered Sep 27 '22 21:09

BoltClock