Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP $_POST variables are sometimes empty

Tags:

post

php

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?

like image 907
Marc Avatar asked Dec 04 '09 12:12

Marc


2 Answers

You forgot the name= parameter on your INPUT tag.

<input type="text" id="xyz" name="xyz" value="123"/>
like image 181
user603749 Avatar answered Oct 01 '22 22:10

user603749


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.

like image 23
Gergely Orosz Avatar answered Oct 01 '22 22:10

Gergely Orosz