Tuesday, April 17, 2012

Android Login System with PHP,MySQL and SQLite

Download the complete project.

PHP API

[sourcecode language="php"]
<?php
require_once '../init.php';
?>
<?php
/**
* File to handle all API requests
* Accepts GET and POST
*
* Each request will be identified by TAG
* Response will be JSON data

/**
* check for POST request
*/

if (isset($_POST['tag']) && $_POST['tag'] != '')
{
$tag = $_POST['tag'];
$response = array("tag" => $tag, "success" => 0, "error" => 0);

// check for tag type
if ($tag == 'login')
{
$uname = $_POST['username'];
$password = $_POST['password'];

if ((!$uname) || (!$password))
{

$response["error"] = 1;
$response["error_msg"] = "Please Fill In Both Fields";
//echo json_encode($response);

}
else
{
$uname    = mysql_real_escape_string($uname); //Secure the string before adding to database
$password = mysql_real_escape_string($password); //Secure the string before adding to database
//$password = md5($password); // Add MD5 Hash to the password variable


global $gauthObj;
$isValid =  $gauthObj->login($uname, $password);
if($isValid)// Valid member
{
//update user login data
global $guserObj;
$guserObj->setUserId($id);
$guserObj->updateUserGeoData();

//handle user remember be option
if($remember == "yes")
{
$gauthObj-> rememberMe();
}

$response["success"] = 1;
//$response["uid"] = $guserObj->getUserId($id);
$response["user"]["name"] = $uname;
$response["user"]["password"] = $password;
//echo json_encode($response);
}
else // invalid member
{
$response["error"] = 1;
$response["error_msg"] = "Invalid username  or password";
//echo json_encode($response);

}
}
}
else
{
echo "Invalid Request";
$response["error"] = 1;
$response["error_msg"] = "Invalid Request";

}

}
else
{
echo "Access Denied";
$response["error"] = 1;
$response["error_msg"] = "Access Denied";
}
echo json_encode($response);
?>
[/sourcecode]

4 comments:

  1. init.php and index.php are the same?

    i found this >> loginURL = "http://10.0.2.2/business_directory/includes/mobile/index.php

    ReplyDelete
  2. Obviously not. index.php is the file which is going to validate user credentials sent with HTTP Request. It gets the request and generate a json response. init.php is only for defining some PHP variables.(database username,password)
    If you still have problems pls let me know.

    ReplyDelete
  3. Hey ,I have a problem with "Acces Denied /JSON Parser / org.JSONException: Value Access of type java.lang.String cannot be converted to JsonObject".
    I think there is a problem when I pas my "TAG" to url , it don´t creat me a JSON.
    Can you help me?

    ReplyDelete
  4. Hi liviu,
    Sorry for late reply. It seems that json response can not be parsed well. Make sure your response does not include unnecessary HTML.
    Server may produce standard HTML with headers.
    Try sending the header('Content-type: json/application') before printing json_encode($response).

    ReplyDelete

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...