By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

how does PHP5 process POST data in creating $_POST array?

P: n/a
I need to save a wav file that is HTTP POSTed to a php page.
What does PHP5 do automatically to a POSTed variable when it puts it in
the $_POST[] superglobal array? I haven't been able to find any info on
this....

I have a script that does what I want in PERL, but I need to do it in
PHP.
I think the PERL does something magic when it does this:
#### PERL CODE ####
read ( STDIN, $buffer, $ENV { 'CONTENT_LENGTH' } );

@pairs = split ( /&/, $buffer );

foreach $pair ( @pairs )
{
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$FORM { $name } = $value;
}
#### END PERL CODE ####

This is where it separates out the name,value pairs, and for the value,
does the equivalent of urldecode(), base64decode() (am I correct?)

It looks like PHP5 is automatically doing base64decode() and
urldecode(), because when I look at the raw tcpdump of the HTTP
session, I see this:

POST.<obscured>/save_wav.php.HTTP/1.0..
Content-Type:.application/x-www-form-urlencoded..
User-Agent:.BeVocal/2.7.VoiceXML/2.0.BVPlatform/1.8.5.rc1a..
Host:.<obscured>..
Content-Length:.147341..
Via:.1.1.bvcapxy002:8080.(squid/2.5.STABLE4)..
X-Forwarded-For:.10.0.121.215..
Cache-Control:.max-age=0..
Connection:.keep-alive..
...
message=RIFF%E4%C6%00%00WAVEfmt+%10%00%00%00%01%00 %01%00
(90K more of this follows)

But when I look at the data as $_POST['message'], I see this:
RIFFt\0\0WAVEfmt \0\0\0\0\0@\0\0@\0\0\0\0\0\0dataīs\0\0’

the + was translated to " " and the %NN was translated back to normal
value.

So, my question is, if PHP5 is doing base64decode and urldecode
automatically, why is my data still coming across corrupt?

Here is how I'm trying to save it:

// PHP CODE

$writepath=<some path>;
$filename="wavfile";

$wavdata=$_POST["message"];
$lpn=$_POST["license_plate_number"];
$callerid=$_POST["callerid"];

file_put_contents("$writepath/$filename-raw.wav", "$wavdata");

file_put_contents("$writepath/$filename-u.wav", urldecode($wavdata));
// exactly same as raw file

// END PHP CODE

This writes all the data to a file, and I see the wav header, but the
data doesn't look right when I compare it to the output from the PERL
script (i'm not sure if visually comparing two wav files get me
anything)

Is there another way I should be saving this besides
file_put_contents()?

Background info:
I have a voicexml app that sends three things to my php page:
callerid (string)
license_plate_number (string)
message (wav data)

I have no problem accessing:
$_POST["callerid"]
$_POST["license_plate_number"]

My voiceXML app sends the wav data as "audio/wav", and the enctype is
"application/x-www-form-urlencoded"

I've been searching all over the internet for the past few days and I'm
stuck.

help, please?

Billy Becker

Jul 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
More Info:

a hex dump of the wav file from perl shows lots of 0x00 when there is
silence, but a hex dump from php shows lots of this pattern 5C 30 F8 FF
F8 FF F8 FF F8 FF F8 FF 08 where there should be silence in the file.

any ideas what php is doing (or what I'm making php do?)

--
Billy

Jul 17 '05 #2

P: n/a
On 9 May 2005 18:09:15 -0700, bi**********@gmail.com wrote:
I need to save a wav file that is HTTP POSTed to a php page.


Did I read the headers right that the file is posted using a file input
control on a form? Then see
http://php.net/manual/en/features.file-upload.php
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3

P: n/a
I'm not using a file input control. Right now, the data is sent:
Content-Type:.application/x-www-form-urlencoded..
This method works with PERL, and I would like to make it work with PHP.

I can see the data in PHP, but the format is wrong, and I was wondering
what PHP does to POST variables before putting them in the $_POST[]
array. I see that it is urldecoding and base64decoding... but it must
be doing something else in addition, because my data is corrupt. As the
snippet of hex data from my previous post shows, PHP is turning
changing 00 into high-bit characters. Is this a bug? or is this
intended functionality that just isn't explained anywhere?

Another question: is there any way to access the raw POST data? if so,
how?

Thanks,
Billy

Jul 17 '05 #4

P: n/a
I'm not using a file input control. Right now, the data is sent:
Content-Type:.application/x-www-form-urlencoded..
This method works with PERL, and I would like to make it work with PHP.

I can see the data in PHP, but the format is wrong, and I was wondering
what PHP does to POST variables before putting them in the $_POST[]
array. I see that it is urldecoding and base64decoding... but it must
be doing something else in addition, because my data is corrupt. As the
snippet of hex data from my previous post shows, PHP is turning
changing 00 into high-bit characters. Is this a bug? or is this
intended functionality that just isn't explained anywhere?

Another question: is there any way to access the raw POST data? if so,
how?

Thanks,
Billy

Jul 17 '05 #5

P: n/a
On 10 May 2005 10:49:49 -0700, bi**********@gmail.com wrote:
Another question: is there any way to access the raw POST data?


No, yes. See first few hits for
http://www.google.com/search?hl=en&q...=Google+Search
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #6

P: n/a
On 10 May 2005 10:49:49 -0700, bi**********@gmail.com wrote:
Another question: is there any way to access the raw POST data?


No, yes. See first few hits for
http://www.google.com/search?hl=en&q...=Google+Search
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #7

P: n/a
On 2005-05-10, bi**********@gmail.com <bi**********@gmail.com> wrote:
Another question: is there any way to access the raw POST data? if so,
how?


Afaik it's only possible if php doesn't recognize the content-type.
And you should be able to read like $file = file_get_contents('php://input');

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #8

P: n/a
On 2005-05-10, bi**********@gmail.com <bi**********@gmail.com> wrote:
Another question: is there any way to access the raw POST data? if so,
how?


Afaik it's only possible if php doesn't recognize the content-type.
And you should be able to read like $file = file_get_contents('php://input');

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #9

P: n/a
I just discoverd the "always_populate_raw_post_data = on" directive
that you can add to the php.ini file. With that in place, I am able to
get to the $HTTP_RAW_POST_DATA string, and see my values. From here, I
just need to parse the string, and then urldecode() and base64decode()
the part I want.

It's a hassle, but atleast I'll get to what I want.

Still would like to know what exactly PHP5 does when it stick variables
in the $_POST[] array...

--
Billy

Jul 17 '05 #10

P: n/a
I just discoverd the "always_populate_raw_post_data = on" directive
that you can add to the php.ini file. With that in place, I am able to
get to the $HTTP_RAW_POST_DATA string, and see my values. From here, I
just need to parse the string, and then urldecode() and base64decode()
the part I want.

It's a hassle, but atleast I'll get to what I want.

Still would like to know what exactly PHP5 does when it stick variables
in the $_POST[] array...

--
Billy

Jul 17 '05 #11

P: n/a
OK... I got it to work. First thing, though... there is no
base64_decode() going on. I was mistaken. All that is needed is
urldecode().

Here is my process for getting to POST variables from
$HTTP_RAW_POST_DATA:

<?php

//not sure if this is needed, because it works whether it's set or not
set_magic_quotes_runtime( "0" );

function &parse_http_raw_post_data(&$data, $split1str = '&', $split2str
= '=')
// data: the initial string to split up
// composed of key=value pairs, separated by & characters
{
$split1dat = explode($split1str, $data);
$num = count($split1dat);

if (! $num) {
return false;
}

for ($i = 0; $i < $num; $i++) {
$split2dat=explode($split2str, $split1dat[$i]);
$ret[$split2dat[$i]] = $split2dat[$i + 1];
}

return $ret;
}

$vxml_prolog = <<<EOPL
<?xml version="1.0"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
<form>
<block>
EOPL;

$vxml_exit = <<<EOXIT
</block>
<disconnect/>
</form>
</vxml>
EOXIT;

$writepath = "/home/billy/public_html/writedir";
$filename="wavefile";

if ($_GET['usefile'] == 1)
{
//I use this for testing: pass ?usefile=1 to enable the script to
read from a file.
$rawdata=file_get_contents("$writepath/test.dat",rb);
$lpn=$_GET['lpn'];
$callerid=$_GET['callerid'];
}
else
{
//this is the normal method
$rawdata=$HTTP_RAW_POST_DATA;
$lpn=$_POST['license_plate_number'];
$callerid=$_POST['callerid'];
$postdata=$_POST['message'];
}

$parsedata=parse_http_raw_post_data($rawdata);
$wavdata=$parsedata['message'];

file_put_contents("$writepath/$filename-raw.wav", $wavdata);
file_put_contents("$writepath/$filename-post.wav", $postdata);
file_put_contents("$writepath/$filename-u.wav", urldecode($wavdata));

$vxml_body = <<<EOBODY
<prompt>
Report on License Plate <say-as type="spell-out">$lpn</say-as>,
reported by <say-as type="telephone">$callerid</say-as>
has been saved to: $filename.
Thank you for calling. Good Bye!
</prompt>
EOBODY;

// Form is submitted from vxml app
// I need to send a response wrapped inside of proper voicexml tags
// this stuff has nothing to do with the wavfile processing
print ($vxml_prolog);
print ($vxml_body);
print ($vxml_exit);

?>

This creates a useable file named wavefile-u.wav, and a corrupt file
named wavefule-post.wav. Comparing wavefile-u.wav and
wavefile-post.wav, I see that -post has about 200 bytes of extra data,
and the header section looks completely different. If anyone wants to
take a look at them, email me and i'll send them to you.

So, although I have a functional system that does what I want, I'd
still like to know what PHP5 does to POSTed variables when it puts them
in the $_POST[] array.

--
Billy

Jul 17 '05 #12

P: n/a
OK... I got it to work. First thing, though... there is no
base64_decode() going on. I was mistaken. All that is needed is
urldecode().

Here is my process for getting to POST variables from
$HTTP_RAW_POST_DATA:

<?php

//not sure if this is needed, because it works whether it's set or not
set_magic_quotes_runtime( "0" );

function &parse_http_raw_post_data(&$data, $split1str = '&', $split2str
= '=')
// data: the initial string to split up
// composed of key=value pairs, separated by & characters
{
$split1dat = explode($split1str, $data);
$num = count($split1dat);

if (! $num) {
return false;
}

for ($i = 0; $i < $num; $i++) {
$split2dat=explode($split2str, $split1dat[$i]);
$ret[$split2dat[$i]] = $split2dat[$i + 1];
}

return $ret;
}

$vxml_prolog = <<<EOPL
<?xml version="1.0"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
<form>
<block>
EOPL;

$vxml_exit = <<<EOXIT
</block>
<disconnect/>
</form>
</vxml>
EOXIT;

$writepath = "/home/billy/public_html/writedir";
$filename="wavefile";

if ($_GET['usefile'] == 1)
{
//I use this for testing: pass ?usefile=1 to enable the script to
read from a file.
$rawdata=file_get_contents("$writepath/test.dat",rb);
$lpn=$_GET['lpn'];
$callerid=$_GET['callerid'];
}
else
{
//this is the normal method
$rawdata=$HTTP_RAW_POST_DATA;
$lpn=$_POST['license_plate_number'];
$callerid=$_POST['callerid'];
$postdata=$_POST['message'];
}

$parsedata=parse_http_raw_post_data($rawdata);
$wavdata=$parsedata['message'];

file_put_contents("$writepath/$filename-raw.wav", $wavdata);
file_put_contents("$writepath/$filename-post.wav", $postdata);
file_put_contents("$writepath/$filename-u.wav", urldecode($wavdata));

$vxml_body = <<<EOBODY
<prompt>
Report on License Plate <say-as type="spell-out">$lpn</say-as>,
reported by <say-as type="telephone">$callerid</say-as>
has been saved to: $filename.
Thank you for calling. Good Bye!
</prompt>
EOBODY;

// Form is submitted from vxml app
// I need to send a response wrapped inside of proper voicexml tags
// this stuff has nothing to do with the wavfile processing
print ($vxml_prolog);
print ($vxml_body);
print ($vxml_exit);

?>

This creates a useable file named wavefile-u.wav, and a corrupt file
named wavefule-post.wav. Comparing wavefile-u.wav and
wavefile-post.wav, I see that -post has about 200 bytes of extra data,
and the header section looks completely different. If anyone wants to
take a look at them, email me and i'll send them to you.

So, although I have a functional system that does what I want, I'd
still like to know what PHP5 does to POSTed variables when it puts them
in the $_POST[] array.

--
Billy

Jul 17 '05 #13

P: n/a
Welll.. I figured out what was going on.....
magic_quotes_gpc = on

magic quotes was changing all my NULLS (0x00) into \0.
I turned magic quotes off and my data was intact in the $_POST array.

I wish I had read the manual more carefully on that magic quote
stuff.... it is quite a pain in the butt.

Hopefully some other poor soul will stumble across this thread and be
spared a big headache.

--
Billy

Jul 17 '05 #14

P: n/a
On 11 May 2005 00:34:07 -0700, bi**********@gmail.com wrote:
Welll.. I figured out what was going on.....
magic_quotes_gpc = on


How the heck did you have magic quotes turned on in PHP5 -- the entire
option is depreciated with heavy warnings in the PHP.INI file. It's
slightly more common on PHP4 installations for historical reasons.

Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.