-2

guys i use Parse Post to receive my username and password But when my username included with Space its not work. my question is How can I parse a spaced-string in my php code?

<?php
function ParsePost( )
{
    $username = '';
    $password = '';

    $post = file_get_contents( "php://input" );

    $post = str_replace( "&", " ", $post );

    sscanf( $post, "%s  %s", $username, $password );

    return array( 'user' => $username,
              'pass' => $password
                );
}

?>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ramin
  • 9
  • 2
  • 1
    I have a question my self: If the data is being posted to you, why not access them through $_POST ??? If it is not beign posted, then why name the variable $post, which will confuse other people or you after a few weeks ? – gkalpak May 29 '13 at 05:41
  • I sent username and password with c++ programing (&CURL)... – Ramin May 29 '13 at 05:43
  • 1
    @gkalpak https://stackoverflow.com/questions/8893574/php-php-input-vs-post – inetphantom Mar 05 '21 at 12:25

2 Answers2

0

You could use sscanf( $post, "%s&%s", $username, $password );

OR

Use following style code:

function ParsePost( )
{

    //$post = "Username&Password";

    $post = file_get_contents( "php://input" );

    $pieces = explode('&', $post);

    return array( 'user' => $pieces[0],
              'pass' => $pieces[1]
                );
}
Sunny R Gupta
  • 5,026
  • 1
  • 31
  • 40
-1

Just add:

$post = str_replace( " ", "_", $post );

Before:

$post = str_replace( "&", " ", $post );

The username will result with _s so you may want to convert them back to spaces before the return:

$username = str_replace( "_", " ", $username);

This will replace _ with a space as well.

The best way to do this is actually using an explode.

list($username, $password) = explode('$', $post);
n1xx1
  • 1,971
  • 20
  • 26