Skip to content

Instantly share code, notes, and snippets.

@anthonycvella
Last active December 11, 2015 23:48
Show Gist options
  • Save anthonycvella/4679311 to your computer and use it in GitHub Desktop.
Save anthonycvella/4679311 to your computer and use it in GitHub Desktop.
<?php
require "/classes/Database.php";
header("Content-type: text/json");
$dbinfo = array(
"host" => "127.0.0.1",
"user" => "root",
"pass" => "",
"name" => "hiskor"
);
$db = new Database ( $dbinfo );
$_POST['type'] = isset($_POST['type']) ? $_POST['type'] : null ;
switch ($_POST['type'])
{
case 'login':
login();
break;
}
function sendResponse($status = 200, $body = '', $content_type = 'text/json')
{
$status_header = 'HTTP/1.1 ' . $status . ' ';
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
function login()
{
global $HTTP_RAW_POST_DATA;
global $db;
// remove the second argument or pass false if you want to use an object
//$user_info = json_decode($HTTP_RAW_POST_DATA, false);
// Check for required parameters
if (isset($_POST['username']) && isset($_POST['password']))
{
//Put parameters into local variables
$username = $_POST['username'];
$password = $_POST['password'];
$db->query("SELECT password FROM users WHERE username=?")->bind(1, $username)->execute();
if ($db->getTotalRows()) {
$result = $db->fetch();
$resultpassword = $result['password'];
}
// Username or password invalid
if ($password == $resultpassword) {
sendResponse(100, json_encode(array('username' => $username)));
return true;
}
else
{
sendResponse(400, 'Invalid Username or Password');
return false;
}
}
//sendResponse(401, 'Not enough parameters');
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment