I prefer not to write my own login function, so instead I would like to use openId. I found the library lightopenid and with some modifications of the google-example file it looks like this:
<?php
session_start();
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try {
# Change 'localhost' to your domain name.
$openid = new LightOpenID('127.0.0.1');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'https://www.google.com/accounts/o8/id';
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<button>Login with Google</button>
</form>
<?php
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
if ($openid->validate()) {
$_SESSION['auth'] = true;
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
So I added a $_SESSION thing there... Now think I may use some code on the top of each protected page, like this:
<?php session_start();
if (!$_SESSION['auth']) { exit; } ?>
I want this to be done the right way, so that it's all safe and so on. Would you have done it this way or am I doing something wrong? I might use cookies instead...