Created
February 1, 2013 04:24
-
-
Save anthonycvella/4689203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function login() | |
{ | |
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) { | |
$token = generateToken(); | |
header('Content-type: application/json'); | |
echo json_encode(array( | |
'username' => $username, | |
'token' => $token, | |
'message' => 'Login success' | |
)); | |
return true; | |
} else { | |
header('Content-type: application/json'); | |
echo json_encode(array( | |
'message' => 'Error logging in user' | |
)); | |
return false; | |
} | |
} | |
//sendResponse(401, 'Not enough parameters'); | |
return false; | |
} | |
function register() | |
{ | |
global $db; | |
if (isset($_POST['username']) && isset($_POST['passwordMD5']) && isset($_POST['email'])) | |
{ | |
$username = $_POST['username']; | |
$passwordMD5 = $_POST['passwordMD5']; | |
$email = $_POST['email']; | |
$db->query("SELECT username, email FROM users WHERE username=? OR email=?")->bind(1, $username)->bind(2, $email)->execute(); | |
if ($db->getTotalRows()) { | |
$result = $db->fetch(); | |
$resultusername = $result['username']; | |
$resultemail = $result['email']; | |
} | |
if ($username != $resultusername OR $email != $resultemail) { | |
$db->query("INSERT INTO users ('username', 'password', 'email') VALUES (?, ?, ?)")->bind(1, $username)->bind(2, $passwordMD5)->bind(3, $email)->execute(); | |
header('Content-type: application/json'); | |
echo json_encode(array( | |
'username' => $username, | |
'email' => $email, | |
'status' => 'Registration Passed' | |
)); | |
return true; | |
} else { | |
header('Content-type: application/json'); | |
echo json_encode(array( | |
'username' => $username, | |
'email' => $email, | |
'status' => 'Registration Failed' | |
)); | |
return false; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment