Search code examples
phphttp-postcgi

Can't pass variables to CGI with POST method


To test things out I did this .sh:

#!/bin/bash
export GATEWAY_INTERFACE=CGI/1.1
export REQUEST_METHOD=POST 
export SCRIPT_FILENAME=/www2/post_example.php 
export CONTENT_LENGTH=12
export REDIRECT_STATUS=true
export username=bob
exec echo 'username=bob' | /usr/bin/php-cgi7.0 -e

The php that I am using to test is the following:

<?php
     echo $_POST['username'];
?>

And whenever I try to run the script it says:

PHP Notice:  Undefined index: username in /www2/post_example.php on line 2
Content-type: text/html; charset=UTF-8

<br />
<b>Notice</b>:  Undefined index: username in <b>/www2/post_example.php</b> on line <b>2</b><br />

I don't know what I'm missing...


Solution

  • I figured out that I missed the Content-Type header.

    CONTENT_TYPE=application/x-www-form-urlencoded