9

I am a PHP novice and I am having a problem with POST variable sometimes being empty when I submit them. The part which is making this difficult for me to figure out is that this doesn't happen every time, and I can usually get the post data in my PHP program by simply refreshing the page. Sometimes it will take a few times, but once the data has come through once, it will continue to come through fine.

Other PHP applications (Wordpress and others) work fine and never give any errors, so I am pretty sure there is a problem with my php app.

I have PHP 4.2.9 installed on a CentOS 5.2 server, and have KeepAliveTimeout set to 1.

The code for the app in which I process the submitted data:

<?php
    session_start(); 
    if (isset($_SESSION['username'])) {
        $expire = time() + (60*60*24*30);
        setcookie("username", $_SESSION['username'], $expire);
    }

    header("Cache-control: no-cache");

    if (!isset($_SESSION['username'])) {
        header('Location: ./login.php');
        die();
    }

    if(empty($_SERVER['CONTENT_TYPE'])){
        $type = "application/x-www-form-urlencoded";
        $_SERVER['CONTENT_TYPE'] = $type;
    }

    var_dump($_POST);
    echo "\n";
    var_dump($_SERVER);
?>

Any help at all would be appreciated

Edit: I have found one difference between working post requests and the ones that fail. Firebug tells me that when the post fails, the status is a 302 redirect instead of 200 ok. Im not really sure what can cause that, but I have the header cache control in sending form just as it appears in the above code snippet.

Any ideas?

7
  • How does the form look like you're POSTing with? Commented Dec 4, 2009 at 12:39
  • Does this happen in all browsers? Commented Dec 4, 2009 at 12:40
  • you could also check the $_REQUEST variable which holds pots and get data Commented Dec 4, 2009 at 12:44
  • Pekka - It happens with all forms, and I have checked them many times. I dont believe they are at fault here. DisgruntledGoat - I am using FireFox, but it also happens with IE and Chrome. Nicky De Maeyer - I have tried using the REQUEST variable as well with the same result. Commented Dec 4, 2009 at 12:56
  • Any way we can see the form you're dealing with? A second pair of eyes on it may help. Commented Dec 4, 2009 at 13:36

4 Answers 4

7

You forgot the name= parameter on your INPUT tag.

<input type="text" id="xyz" name="xyz" value="123"/>
Sign up to request clarification or add additional context in comments.

1 Comment

This answer should be marked as the most likely cause for a beginner. I just had exactly that problem - working in javascript I had all the "id" tags specified and they were working, but when the page was submitted to the server I was expecting the $_POST value to be the id. Adding "name" tags fixed it immediately.
1

You could check the $_REQUEST variable which is a combination of $_POST and $_GET arrays. If the variables aren't there, they haven't been sent and the problem is probably on the client side.

You can use network traffic analyzing tools e.g. Firebug's Net tab to see what's actually been sent to the server.

3 Comments

I have checked the REQUEST variable and the result is the same. I have also checked the post request with Firebug, and the variables are always passed correctly. It is just the php app which is not receiving them.
Looks like the PHP app is changing the variables within itself then. I would place a check at the entry point as well - the variables should be OK there - and then figure out which part of the code is "getting rid" of them.
I have tried doing this, however the problem still happens when the only thing I have in my php code is var_dump($_POST);
0

The $_POST variable is filled by the PHP engine, not by any application. So, if it's empty, it's just empty and your code needs to take that into account, in the same way as you already do for the $_SESSION and $_SERVER variables.

Comments

0
header('Location: ./login.php');

By default this will send the status code 302 as well. Most browsers will request the target of a 302 with a GET request, regardless of the original request type. If you want both requests to contain the POST data then you could try a 307:

header('Location: ./login.php', true, 307);

But you might this doesn't work across all browsers (it should work in anything non-ancient). You might want to re-examine your logic here though, it's not normal to require clients double-POST.

Also, are you aware that you read $_SESSION['username'] but don't appear to ever write to it?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.