Page 1 of 1

why doesn't work $_POST["username"] method?

Posted: Thu Jan 08, 2015 2:55 pm
by anaksagoras
hi everybody. sorry for my bad english.
i have a problem with $_POST method. when i wrote that code

Code: Select all

print $_POST['username'];
in php its work on html posting.

Code: Select all

    <form action="http://siteaddress.pe.hu/tool/" method="POST">
    <input type="username" name="username"><br>
	<input type="password" name="password"><br>
    <input type="submit" value="submit" name="submitMe">
    </form>

but when i send this post action on c# its not working. i mean $_POST action returns null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://siteaddress.pe.hu/tool/auth");
request.Timeout = 0x7530;
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version11;
request.Method = "POST";

byte[] bytes = Encoding.ASCII.GetBytes("username=test&password=test");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
XmlDocument document = new XmlDocument();
document.LoadXml(new StreamReader(response.GetResponseStream()).ReadToEnd());

Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (XmlNode node in document.ChildNodes[1])
{
if (node.NodeType == XmlNodeType.Element)
{
dictionary.Add(node.Name, node.InnerText);
}
}
What causes it? why during on c# post action sended, php code doesn't work?

Re: why doesn't work $_POST["username"] method?

Posted: Thu Jan 08, 2015 3:09 pm
by requinix
The URL is a bit different (trailing slash) and you're not including the submit button in your request body. Those are the only two differences I see. Besides the extra v= but I don't think that would make a difference.

What's the rest of the PHP code? And what exactly happens when you use the C# code? What's the response it receives?

Re: why doesn't work $_POST["username"] method?

Posted: Thu Jan 08, 2015 3:21 pm
by anaksagoras
my php code are below:

Code: Select all

<?
  
  include("./../func.php");
  
  $username = $_POST['username'];
  $password = $_POST['password'];

  $xml 		= new SimpleXMLElement('<xml/>');

  $sonuc = $db->getQuery("select username from kullanici k where k.username='" . $username . "' and password='" . $password . "'");
  
  if ($sonuc->rowCount==1) {
	  if(session_id()=='') {
		session_start();
	  }
	  
	  $_SESSION["username"] = $username;
	  $_SESSION["sessionId"] = session_id();
	  $_SESSION["login"] = "true";
	  
	  $xml->addChild("result", "1");
	  $xml->addChild("session_id", $_SESSION["sessionId"]);
	  $xml->addChild("msg", "login success");
  }
  else {
	  $xml->addChild("result", "-1");
	  $xml->addChild("session_id", "-1");
	  $xml->addChild("msg", "login failed");
  }
  
  print($xml->asXml());
?>
a i tried remove v code but result is same. also i tried very very changes on code but not work :( my c# code only does post action. how can i place submit button in c#? i think its not possible.

when i send this post on c#, then php tells me $_POST["username"] is null.

Re: why doesn't work $_POST["username"] method?

Posted: Thu Jan 08, 2015 4:41 pm
by requinix
Can you post the output of

Code: Select all

print_r(function_exists("apache_request_headers") ? apache_request_headers() : "n/a");
print_r(file_get_contents("php://input"));

Re: why doesn't work $_POST["username"] method?

Posted: Tue Jan 13, 2015 8:34 am
by anaksagoras
result is n/a

Re: why doesn't work $_POST["username"] method?

Posted: Tue Jan 13, 2015 1:13 pm
by requinix
Unfortunate. IIS? What about

Code: Select all

print_r($_SERVER);
Only need to post the stuff that starts with "HTTP_".

Re: why doesn't work $_POST["username"] method?

Posted: Tue Jan 13, 2015 4:40 pm
by Christopher
Look at your form. Instead of <input type="username" name="username"> it should be <input type="text" name="username">.

There is an input type="password", but no type="username" -- use type="text".

Re: why doesn't work $_POST["username"] method?

Posted: Tue Jan 13, 2015 5:37 pm
by anaksagoras
dear christopher,

i don't send this post on web, i send on c# code. there is not a web form post. when i send this post on web, its work. but on c# code not work.