29

I want to know how to detect if $_POST is set or not.

Right now I detect it like this:

if(isset($_POST['value']))

But I'm not looking if value is set anymore. Basically, any POST will work.

if(isset($_POST))

I'm not sure how PHP handle this. Perhabs isset($_POST) is always returns true since it's a PHP global?

Basically, how can I do this?

Salman A
  • 262,204
  • 82
  • 430
  • 521
Lisa Miskovsky
  • 896
  • 2
  • 9
  • 18
  • 3
    `$_POST` always accompanies a header request, and **will** contain values. Maybe you want `$_SERVER['REQUEST_METHOD'] == 'POST'`. – BenM Mar 05 '13 at 09:55
  • 3
    Do you want to figure out whether `$_POST` contains *any* data or whether the request used the HTTP POST method? – deceze Mar 05 '13 at 09:57

6 Answers6

63

Try with:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {}

to check if your script was POSTed.

If additional data was passed, $_POST will not be empty, otherwise it will.

You can use empty method to check if it contains data.

if ( !empty($_POST) ) {}
hsz
  • 148,279
  • 62
  • 259
  • 315
  • 5
    Since `$_POST` always exists `if ($_POST)` will do just fine, no need for `empty`. – deceze Mar 05 '13 at 09:58
  • 1
    Note that `if ( !empty($_POST) ) {}` may not work if the data is sent not in key-value pairs. You might need to see `$HTTP_RAW_POST_DATA` in that case – Ranty Mar 05 '13 at 09:59
  • 1
    @DipeshParmar Describe your case. – hsz Mar 05 '13 at 10:03
  • So which one I should use for a good practise? Empty or server request method? – Lisa Miskovsky Mar 05 '13 at 10:05
  • Server request method will give you a definitive answer on how the page was accessed. Empty or otherwise just lets you know if the variable has a value in it or not. – niaccurshi Mar 05 '13 at 10:06
  • 2
    @LisaMiskovsky if you want to check if script was posted, use `REQUEST_METHOD` - it's good practise. If you additionaly want to check if any data was posted, check `$_POST` variable. – hsz Mar 05 '13 at 10:06
  • @LisaMiskovsky if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {} is the best – Dipesh Parmar Mar 05 '13 at 10:07
  • Okay. I already check $_POST variable for sanitization and controlling purposes. I'll go with request method. Selecting this as the answer in few minutes. Thank you. – Lisa Miskovsky Mar 05 '13 at 10:07
  • @DipeshParmar; Thank you too, mate. Your reply was also helpful to me. – Lisa Miskovsky Mar 05 '13 at 10:08
13

$_POST is an array. You can check:

count($_POST)

If it is greater than zero that means some values were posted.

Salman A
  • 262,204
  • 82
  • 430
  • 521
3

A simple solution may well be to just use

if (!empty($_POST))
niaccurshi
  • 1,265
  • 9
  • 9
3

Just use it as below. because its super global so its always return true when checking for isset and empty.

<?php
    if($_POST)
    {
        echo "yes";
    }
?>
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
2

I know this answer has already been answered, but here's a simple method I'm using in one of my classes to figure whether the post has been set (perhaps someone will find it useful):

public function isPost($key = null) {

    if ($_SERVER['REQUEST_METHOD'] != 'POST') {

        return false;

    }

    if (!empty($key)) {

        return isset($_POST[$key]);

    }

    return true;

}
Spencer Mark
  • 5,263
  • 9
  • 29
  • 58
-1

Best way to check $_POST

<?php 
if(count($_POST)){}
Baker Hasan
  • 334
  • 2
  • 11
  • Be careful with `empty()`! A string with a zero (`'0'`) is considered empty. Furthermore, it's redundant to use it together with `isset()`. – Álvaro González Apr 04 '17 at 15:43