PHP Web服务,用于在Android上验证用户登录 [英] PHP web service for authenticating user login on Android
问题描述
尝试在Android上进行用户身份验证工作.
Trying to get the user authentication work on Android.
Android应用程序使用POST方法发送用户名和密码.但是,我不断收到错误接收详细信息!"错误.我尝试使用REST控制台查看Web服务是否正常运行,但是没有成功,即使在那儿我也遇到相同的错误.
The Android app sends username and password using POST method. But, I keep getting "Error receiving detail!!" error. I tried to use REST console to see if the web service works, but no success, I get the same error even there.
任何帮助或指导将不胜感激.
Any help or direction would be appreciated.
已构建了一个PHP Web服务,代码为:
Have built a PHP web service and the code is:
<?php
require_once '../site_info.php';
require_once '../database_connect.php';
require_once '../functions.php';
if(isset($_REQUEST['email']) and $_REQUEST['email']!='' and isset($_REQUEST['password']) and $_REQUEST['password']!='')
{
$email = mysql_real_escape_string($_REQUEST['email']);
$pass = mysql_real_escape_string($_REQUEST['password']);
$query = 'select id from users where email = "'.$email.'" and password = "'.sha1($pass).'"';
$result=mysql_query($query) or die('error getting admin details : '.mysql_error());
if(mysql_num_rows($result)==1)
{
echo 'Login Success!!';
}
else
{
echo 'Incorrect login details!!';
}
}
else
{
echo 'Error recieving data!!';
}
?>
Android应用将请求发送为json:
The Android app sends request as json:
JOSN请求数据:{密码":密码",电子邮件":"test@gmail.com"}
JOSN request data:{"password":"password","email":"test@gmail.com"}
响应
状态:200
响应正文:数组(0){}接收数据出错!
response body:array(0) {}Error recieving data!!
推荐答案
找到了解决方法
<?php
require_once '../site.php';
require_once '../database.php';
require_once '../functions.php';
$data = file_get_contents('php://input');
$json = json_decode($data);
$email = $json->{'email'};
$password = $json->{'password'};
if(!empty($email) and !empty($password))
{
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$query = 'select id from users where email = "'.$email.'" and password = "'.sha1($password).'"';
$result=mysql_query($query) or die('error getting admin details : '.mysql_error());
if(mysql_num_rows($result)==1)
{
$array = array("success" => "TRUE");
print(json_encode($array));
}
else
{
$array = array("error" => "Login invalid.");
print( json_encode($array));
}
}
else
{
$array = array("error" => "Data not recieved.");
print( json_encode($array));
}
?>
这篇关于PHP Web服务,用于在Android上验证用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!