Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a PHP web server with android

Tags:

android

php

Hi! I'm developing an internet based app in android. What i want to do is send a user id and password to a php web server and return a response from the server. The response could be a text, like "valid" or "invalid", and if the response is "valid" then a new activity should be launched. I don't know how to send data to a PHP server from android and read a response from the server. The following PHP code will generate a proper response. Please help me regarding this as it is important in my final year project of my BS in computer science. Thanks!

<?php
 $user= $_POST["uid"];
 $pwd=$_POST["pass"];
 $con= mysql_connect("localhost","root");
 if(!$con)
 {
     die("Not able to connect");

 }
 mysql_select_db("try",$con);
 $result=mysql_query("Select * from info where uid='$user'and pass='$pwd'");

 if( mysql_num_rows($result)<=0)
 {
     echo "unsuccessful";
 }
 else
 {
     echo "successful";
 }
 mysql_close($con);
?>
like image 256
Mirza Hassam Avatar asked Dec 04 '25 18:12

Mirza Hassam


1 Answers

Try to use something like this:

<?
$sock = fsockopen("url.com", 80, $errno, $errstr, 30);
if ($sock)
 {
 fwrite($sock,  "GET /some_request HTTP/1.0\r\n" .
            "Host: url.com\r\n" .
            "\r\n");

 $beg = 0;
 while (!feof($sock))
  echo fread($sock, 128);

 fclose($sock);
 }
?>
like image 68
Angrybambr Avatar answered Dec 06 '25 06:12

Angrybambr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!