I'm trying to create a login function for my php files so I don't repeat the login code on every single page. I'm still working out how to work with functions in PHP. I've created the following function in my functions.php file that I include at the top of every page:
function login($hostname, $version, $database, $username, $password) {
$url = 'https://' . $hostname . '/fmi/data/' . $version . '/databases/' . rawurlencode($database) . '/sessions';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
'Content-Type: '. 'application/json',
'Authorization: Basic ' . base64_encode ($username . ':' . $password)
));
curl_setopt($ch, CURLOPT_POSTFIELDS, '{}');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec ($ch);
$error = curl_error ($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$loginResultArray = json_decode ($result, true);
// Check for Error Message
$errorCode = $loginResultArray['messages'][0]['code'];
$errorMessage = $loginResultArray['messages'][0]['message'];
if ($errorCode !== '0') {
// Login Error
$loginError = 'Login Error: '. $errorMessage. ' (' . $errorCode . ')';
} else {
$loginError = '';
$token = $loginResultArray['response']['token'];
}
}
At the top of each page I'm now adding:
require_once 'inc/functions.php';
login($hostname, $version, $database, $username, $password) {
}
to call the function. I'd like to include some error checking here so that if the function returned an error or success result I can branch accordingly after calling the function. I'm not sure how to return an error or success result from the function so I can determine if the function was successful or not?
CodePudding user response:
There are multiple solutions, I think. One option is to throw an exception if the login fails and if there is no error: Return (or maybe store?) the token. Throwing an exception will stop the execution (if you don't catch it where your method is called).
Example:
function login($hostname, $version, $database, $username, $password) {
// ...
if ($errorCode !== '0') {
// Login Error
throw new \Exception('Login Error: '. $errorMessage. ' (' . $errorCode . ')');
} else {
return $loginResultArray['response']['token'];
}
}
If you don't want to stop the execution in case of an error, you may return a result array and handle the content where your login()
method was called.
Example:
function login($hostname, $version, $database, $username, $password) {
// ...
if ($errorCode !== '0') {
// Login Error
return [
'error' => 'Login Error: '. $errorMessage. ' (' . $errorCode . ')',
'token' => null,
];
} else {
return ['token' => $loginResultArray['response']['token']];
}
}
// ... In your other file:
require_once 'inc/functions.php';
$loginResult = login($hostname, $version, $database, $username, $password);
if (isset($loginResult['token'])) {
echo 'You are logged in!';
} else {
echo $loginResult['error'];
}
CodePudding user response:
if ($errorCode !== '0') {
> // Login Error
> $loginError = 'Login Error: '. $errorMessage. ' (' . $errorCode . ')';
> return false;
> } else {
> $loginError = '';
> $token = $loginResultArray['response']['token'];
> return true; //here can return $token and check token avail or not
>
> }