2

Possible Duplicate:
PHP detecting request type (GET, POST, PUT or DELETE)

I want to display a warning when people submit a form more than one time, and when he use GET to request a page the count variable reset to 0;

I'm using this technique:

<input type="hidden" name="submissioncount" value="<?php echo $subCount; ?>" /> 

But it seems to me $_GET and $_POST alway exist:

<?php
if(isset($_GET)){
  $warning = "GET exist";
}else{
  $warning = 'GET not exist';
}
if(isset($_POST)){
  $warning2 = "POST exsit";
}else{
  $warning2 = 'POST not exist ';
}


?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
  echo $warning;
  echo $warning2; 
?>
<form acion='<?php echo $_SERVER['PHP_SELF']?>' method='POST' >
<input type='submit' />
</form>
</body>
</html>

It turns out it aways return exist? Where I did wrong or any other work round for it?

Community
  • 1
  • 1
mko
  • 21,334
  • 49
  • 130
  • 191

1 Answers1

3

$_GET and $_POST superglobals exist whether passed or not, so checking for them will return true. Check entries within them to ascertain if they exist.

if(isset($_POST['submissioncount']) or if(isset($_GET[some_GET_variable])


Alternately,

$meth = $_SERVER['REQUEST_METHOD'];
if($meth == 'GET')
//do something
else if($meth == 'POST')
//do something else
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • for `if(isset($_GET[some_variable_set])` part, how to define the variable_set?, the rest part of your answer is great! – mko Aug 03 '12 at 06:10
  • No no. I meant, if you are passing some `$_GET` parameter in your URL as `urlquery?param1=key1&param2=key2`, you can query for param1 as `if(isset($_GET['param1'])` – Anirudh Ramanathan Aug 03 '12 at 06:11
  • Got it! Is there any existing urlquery as variable I can use without append param1=key in the end to achieve this? otherwise $_SERVER['REQUEST_METHOD'] work fine – mko Aug 03 '12 at 06:15
  • Yes. That is correct. :) – Anirudh Ramanathan Aug 03 '12 at 06:17