Re: How to pass POST data to php-cgi

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I solved the problem myself.

Following command works:

export CONTENT_LENGTH=4; export REQUEST_METHOD=POST; export SCRIPT_FILENAME=
test.php ; export CONTENT_TYPE=application/x-www-form-urlencoded;  echo s=44
| php-cgi

output:

X-Powered-By: PHP/5.2.3-1ubuntu6.3
Content-type: text/html

44

a working C fork/exec example is also attached.

=========== test.c ===========

#include <unistd.h>
#include <stdio.h>
#include <string.h>

main()
{
    int outfd[2];
    int infd[2];
    int oldstdin, oldstdout;
    pipe(outfd); // Where the parent is going to write to
    pipe(infd); // From where parent is going to read
    if(!fork())
    {
        close(0);
        close(1);
        dup2(outfd[0], 0); // Make the read end of outfd pipe as stdin
        dup2(infd[1],1); // Make the write end of infd as stdout
        setenv("CONTENT_LENGTH", "4", 1);
        setenv("REQUEST_METHOD", "POST", 1);
        setenv("SCRIPT_FILENAME", "test.php", 1);
        setenv("CONTENT_TYPE", "application/x-www-form-urlencoded", 1);
        execl("/usr/bin/php-cgi","/usr/bin/php-cgi", NULL);
    }
    else
    {

        char output[1000];
        close(outfd[0]); // These are being used by the child
        write(outfd[1],"s=444", 5); // Write to child's stdin
        close(outfd[1]);
        close(infd[1]);
        output[read(infd[0],output,1000)] = 0; // Read from child's stdout
        printf("%s", output);
    }
}


On Sun, Mar 16, 2008 at 4:30 PM, Hui Chen <usa.chen@xxxxxxxxx> wrote:

> Hi,
>
> I'm writing a simple httpd with php support via cgi/fastcgi. I tried
> several ways to pass POST data to php-cgi (by writing to child process's
> stdin from parent process), however, none of them worked
>
> Here's the most simple not working example:
>
> $ export CONTENT_LENGTH=4; export HTTP_METHOD=POST; echo 's=33' | php-cgi
> test.php
>
> and test.php has only one line:
>
> <?php echo $_POST["s"]; ?>
>
> Running it gave me:
>
> X-Powered-By: PHP/5.2.3-1ubuntu6.3
> Content-type: text/html
>
> <br />
> <b>Notice</b>:  Undefined index:  s in <b>/home/chen/temp/test.php</b> on
> line <b>1</b><br />
>
> Please help me figure out what's missing. Part of php-cgi's phpinfo()
> output is attached, I will provide more if you need.
>
> Thanks in advance,
> Hui
>
> =========phpinfo() output==========
> _SERVER["TERM"]xterm
> _SERVER["SHELL"]/bin/bash
> _SERVER["SSH_TTY"]/dev/pts/1
> _SERVER["CONTENT_LENG"]4
> _SERVER["LD_LIBRARY_PATH"]/usr/local/lib
>
> _SERVER["LS_COLORS"]no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:
> _SERVER["LANG"]en_US.UTF-8
> _SERVER["HISTCONTROL"]ignoredups
> _SERVER["SHLVL"]1
> _SERVER["CONTENT_LENGTH"]4
> _SERVER["LESSOPEN"]| /usr/bin/lesspipe %s
> _SERVER["HTTP_METHOD"]POST
> _SERVER["CONTENT_TYPE"]application/x-www-f­orm-urlencoded
> _SERVER["LESSCLOSE"]/usr/bin/lesspipe %s %s
> _SERVER["_"]/usr/bin/php-cgi
> _SERVER["PHP_SELF"]<i>no value</i>
> _SERVER["REQUEST_TIME"]1205702297
> _SERVER["argv"]</td><td class="v"><pre>Array
> (
>     [0] =&gt; phpinfo.php
> )
> </pre>
>
> _SERVER["argc"]1
> _ENV["TERM"]xterm
> _ENV["SHELL"]/bin/bash
> _ENV["SSH_TTY"]/dev/pts/1
> _ENV["CONTENT_LENG"]4
> _ENV["LD_LIBRARY_PATH"]/usr/local/lib
>
> _ENV["LS_COLORS"]no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:
> _ENV["LANG"]en_US.UTF-8
> _ENV["HISTCONTROL"]ignoredups
> _ENV["SHLVL"]1
> _ENV["CONTENT_LENGTH"]4
> _ENV["LESSOPEN"]| /usr/bin/lesspipe %s
> _ENV["HTTP_METHOD"]POST
> _ENV["CONTENT_TYPE"]application/x-www-f­orm-urlencoded
> _ENV["LESSCLOSE"]/usr/bin/lesspipe %s %s
> _ENV["_"]/usr/bin/php-cgi
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux