安卓系统PHP服务器[英] Android PHP server

本文是小编为大家收集整理的关于安卓系统PHP服务器的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用向PHP服务器发送信息的Android应用程序. Android应用程序应首先登录,如果登录成功开始发送数据.

这是PHP页面(登录):

<?php
$data = file_get_contents('php://input');
$json = json_decode($data);
$id=$json->{'im'};

$con = mysql_connect('localhost','root','1111');
mysql_select_db('root') or die(' ');
$sql = "SELECT name FROM chiled WHERE `im` LIKE $id ";

$query = mysql_query( $sql );

$a=mysql_fetch_row($query);
print(json_encode($a[0]));
$n=$json->{'name'};


mysql_close();
?>

PHP页面从Android应用程序接收ID,并检查DB中是否存在ID,然后向Android应用程序发送响应.根据收到的请求,应用程序将决定登录是否成功,如果登录成功,则开始发送数据.

您认为接收PHP服务器中数据的最佳方法(如果登录成功)?我可以使用新页面接收(在连接到另一页的Android应用程序中创建一个新连接,然后开始发送数据)数据或我可以在同一登录页面中进行数据吗?

推荐答案

如果登录成功,那么您可以启动存储会话和验证的PHP会话.大多数网站都在这个fasion(例如堆栈溢出)中工作.

默认情况下PHP中的会话通过cookie phpsessid传递.因此,如果登录是正确的,那么您可以返回cookie和本地(服务器端)与用户会话会话.

也,PHP允许创建和操作会话cookie.

唯一的详细信息是您必须将会话与IP与IP相关联,因为安全问题.

所以,循环可以是:

android --User/password - > login.php --- phpsessid - > android

android --phpsessid - > content_page.php ---信息---> android

,如果未登录(或会话已过期)

android --phpsessid - > content_page.php ---错误页面(不允许您)---> android

其他推荐答案

您实际上应该是创建一个Web服务(SOAP/WSDL),因为它将删除PHP和Android/Java之间的数据传输. SOAP =(简单对象访问协议)

香草php方法:

http://davidwalsh.name/web-service-php-mysql- XML-JSON

对于用户身份验证系统,我会考虑合适的框架工作(如Zend等)以获得最佳实践,并且可能甚至使用帧工作本身构建Android的身份验证系统和WSDL服务应用程序.

zf方法:

http://www.opensourceuniverse.com/zend-framework/quickstart-web-services-with-soap-and-zend-framework-263.html

在Android中消耗SOAP:

http://code.google.com/p/ksoap2-android/

其他推荐答案

您可以在相同的登录页面中进行,发布变量以便登录并在PHP中具有IF(login = true),或者您可以在a之后创建与不同页面的新连接成功登录,这取决于您,取决于您在做什么.

本文地址:https://www.itbaoku.cn/post/102714.html

问题描述

I am working with an Android app that sends information to a PHP server. The Android app should login first and if the login is successful start to send the data.

This is the php page (login):

<?php
$data = file_get_contents('php://input');
$json = json_decode($data);
$id=$json->{'im'};

$con = mysql_connect('localhost','root','1111');
mysql_select_db('root') or die(' ');
$sql = "SELECT name FROM chiled WHERE `im` LIKE $id ";

$query = mysql_query( $sql );

$a=mysql_fetch_row($query);
print(json_encode($a[0]));
$n=$json->{'name'};


mysql_close();
?>

The PHP page receives the id from the Android App and checks if the id exists in the DB then sends response to the Android App. According to the received request the App will decided if the login is successful or not, if the login is successful then it will start sending the data.

What do you think is the best way to receive the data in the PHP server (if the login is successful)? Can I use a new page to receive (create a new connection in Android App that connects to another page then start sending the data) the data or can I do that in the same login page?

推荐答案

if the login is successful then you can start a php session that store the session and validation. Most websites works in this fasion (for example Stack Overflow).

A session in php by default is passed through cookie PHPSESSID. So, if the login is correct then you can return the cookie and locally (server side) associate the session with the user.

Also, PHP allow to create and manipulate a session cookie-less.

The only detail is that you must associate a session with the ip because security issues.

So, the cycle can be:

android --user/password--> login.php ---phpsessid--> android

android --phpsessid --> content_page.php ---information--->android

and if you are not logged (or the session has expired)

android --phpsessid --> content_page.php ---error page (you are not allowed)--->android

其他推荐答案

You really should be creating a web service (SOAP/WSDL) for such, as it will take the sting out of the transport of data between PHP and Android/Java. SOAP = (Simple Object Access Protocol)

Vanilla PHP method:

http://davidwalsh.name/web-service-php-mysql-xml-json

As for the user authentication system, I'd look into well established frame works (like Zend, etc.) for best practices, and possibly even use the frame work itself to build the authentication system and the WSDL service for your Android app.

ZF method:

http://www.opensourceuniverse.com/zend-framework/quickstart-web-services-with-soap-and-zend-framework-263.html

Consuming the SOAP in Android:

http://code.google.com/p/ksoap2-android/

其他推荐答案

You could do it in the same login page, posting a variable to say if logged in and having an if (login = true) in the php, or you could make a new connection to a different page after a successfull login, it's up to you and depends what you are doing really.