13

I'm trying to build a simple login system using <form method="post">. Everything works fine on MAMP on my laptop but when I upload the script to the server (Windows) it doesn't work; it seems that the $_POST array is empty.

I commented out everything but the bare bones and it still doesn't work.

index.php:

<form id="login-form" method="POST" action="_scripts/check_login.php">
Email Address
<input name="login-email" type="text" id="login-email">
Password
<input name="login-password" type="text" id="login-password">
<input type="submit" name="Submit" id="login-button" value="Login">
</form>

_scripts/check_login.php: (I've removed everything except some var_dumps for debugging)

var_dump($_POST);

$loginEmail = trim($_POST['login-email']);
echo '<br>';
$loginPassword = ($_POST['login-password']);
var_dump($loginEmail);
echo '<br>';
var_dump($loginPassword);

When I submit the form, no matter what I put in the text fields, I see this:

array(0) { }
string(0) ""
NULL

If I change all the instances of "post" to "get" in the above two files, everything works fine. But I don't want to use get. ($_REQUEST doesn't work either if I submit the form using method="post").

NB this all works fine on localhost, but not on the server (which is running Windows.) So it would seem to be a problem with the server, but I have no idea what. You can see the PHPInfo here: http://brailleapps.org/phpinf0.php

Any ideas?

EDIT: Solved! See below.

GMA
  • 5,816
  • 6
  • 51
  • 80
  • What does `$_SERVER` contain for POST requests? Did you try enctype? Is the FastCGI client permitted to receive the [POST verb for IIS](http://stackoverflow.com/questions/313188/empty-post-array-in-php-5-2-6-iis-cgi)? – mario Mar 13 '13 at 13:06
  • we had the same issue but ajax post not working once we moved to live server. After a long struggle we found server's mod rewrite module turned off. Once turned on everything is fine.So Check that.But as per my knowledge there is no relation with POST and mod rewrite.Just give it a try. – Samy Mar 13 '13 at 13:08
  • 3
    This might be an issue with the content type. If no content type is recocgnized or it's missing, then `$_POST` is empty. Try setting `if(empty($_SERVER['CONTENT_TYPE'])) {$_SERVER['CONTENT_TYPE'] = "application/x-www-form-urlencoded";}`. And what does `var_dump(file_get_contents('php://input'));` say? – Quasdunk Mar 13 '13 at 13:09
  • Check this thread: http://stackoverflow.com/questions/9914979/php-post-not-working – dozed Mar 13 '13 at 13:10
  • 2
    Can you check the _SERVER["REQUEST_METHOD"] variable to be sure that is effectively been sent as Post? – Pedrom Mar 13 '13 at 13:30
  • @Quasdunk Just tried that `if(empty($SERVER[...` line but it doesn't seem to have made a difference. Your second bit of code outputs `string(0) ""` – GMA Mar 13 '13 at 13:32
  • @Pedrom `var_dump($_SERVER["REQUEST_METHOD"]` outputs `string(4) "POST"` – GMA Mar 13 '13 at 13:36
  • Alright, an empty `php://input` means that there is really no content in the request. This is weird, because that's the place to look at for *any* input or if the encoding/content type could not be determined. Looks like your server really drops the content of post-request... Did you make sure that this verb is allowed on your server? Is there maybe a firewall in between that may filter requests? Or did you maybe try with other, even more basic pieces of code, just to make sure that post definitely does not work? – Quasdunk Mar 13 '13 at 13:40
  • var_dump($_POST); may be the cause of your error see link: http://stackoverflow.com/questions/7547840/var-dump-post-is-empty-but-not-var-dump-post-die – M ten Klooster Mar 13 '13 at 13:47
  • the scope of a $_POST variable is restricted to a script ..so check if you have migrated to other scipts and came back to this script before var_dump() – funtime Mar 13 '13 at 13:51
  • @Quasdunk Thank you! Having the Content-Type set to 'application/json' in my POST requests is what was causing this issue for me. Sending the POST request using the default 'text/html' HTTP content-type fixed it, and now I can see my POST variables again! For anyone interested in properly handling 'application/json' requests in PHP check out: https://stackoverflow.com/questions/8893574/php-php-input-vs-post – TheKarateKid Jul 24 '17 at 17:55

4 Answers4

4

I got it fixed eventually, figure I might as well post what worked here in case someone else has the same problem in future.

Turns out one of these HTTP modules was interfering with POST:

RadCompression
RadUploadModule

With these modules turned off, POST worked fine.

(NB this was on a totally new app where I knew there wasn't any existing code that might have depended on one of those modules... turning them off may have unintended consequences I'm not aware of, YMMV.)

GMA
  • 5,816
  • 6
  • 51
  • 80
2

One possibility is that POST is not an allowed verb on the server:

http://www.iis.net/configreference/system.webserver/security/requestfiltering/verbs

Kermit
  • 33,827
  • 13
  • 85
  • 121
Bart
  • 6,694
  • 6
  • 43
  • 53
0

I've had the same problem with $_POST being empty/NULL, and after spending 30 minutes searching SO and Google I finally found a solution that worked for me.

Since I don't know how to change the php.ini file for MAMP I edited my htaccess to include this:

php_value post_max_size 60M
php_value upload_max_filesize 60M

and I can var_dump($_POST) with the submitted input fine now.

Reference link: http://helpfulcoder.blogspot.ca/2011/01/empty-post-array-in-mamp.html

cellen
  • 156
  • 1
  • 11
0

and another thing is if you have ever changed .htaccess to hide the format Example .php or .html

action="filename.php" instead of this give action="filename"

your problem solved.