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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anaksagoras
Forum Newbie
Posts: 4
Joined: Thu Jan 08, 2015 2:48 pm

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

Post 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?
Last edited by anaksagoras on Thu Jan 08, 2015 3:24 pm, edited 3 times in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
anaksagoras
Forum Newbie
Posts: 4
Joined: Thu Jan 08, 2015 2:48 pm

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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"));
anaksagoras
Forum Newbie
Posts: 4
Joined: Thu Jan 08, 2015 2:48 pm

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

Post by anaksagoras »

result is n/a
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Unfortunate. IIS? What about

Code: Select all

print_r($_SERVER);
Only need to post the stuff that starts with "HTTP_".
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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".
(#10850)
anaksagoras
Forum Newbie
Posts: 4
Joined: Thu Jan 08, 2015 2:48 pm

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

Post 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.
Post Reply