Created
September 9, 2012 16:44
-
-
Save aaronpk/3685554 to your computer and use it in GitHub Desktop.
Simple PHP example of using the indieauth.com API
This file contains 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
<?php | |
session_start(); | |
if(array_key_exists('token', $_GET)) { | |
// Verify the token with indieauth.com | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://indieauth.com/session?token=' . $_GET['token']); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$auth = json_decode(curl_exec($ch)); | |
// If we got a reply, store the domain in the session | |
if($auth && $auth->me) { | |
$_SESSION['user'] = preg_replace('|https?://|', '', $auth->me); | |
} | |
// Redirect back to this page | |
header('Location: ' . $_SERVER['PHP_SELF']); | |
die(); | |
} | |
// If the user is not logged in, display a login link | |
if(!array_key_exists('user', $_SESSION)) { | |
header('HTTP/1.1 403 Forbidden'); | |
echo "Not logged in.<br /><br />\n"; | |
?> | |
<form action="http://indieauth.com/auth" method="get"> | |
<label>Web Address:</label> | |
<input type="text" name="me" placeholder="yourdomain.com" /> | |
<p><button type="submit">Sign In</button></p> | |
<input type="hidden" name="redirect_uri" value="<?= 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] ?>" /> | |
</form> | |
<?php | |
die(); | |
} | |
// Now the user is logged in, and their domain is stored in $_SESSION['user'] | |
echo '<p>Welcome, ' . $_SESSION['user'] . '</p>'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment