title | name | image | tags | snippets | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
PHP Web App Tutorial |
PHP |
/media/platforms/php.png |
|
|
<%= include('../_includes/_package', { pkgRepo: 'auth0-PHP', pkgBranch: 'master', pkgPath: 'examples/basic-webapp', pkgFilePath: null, pkgType: 'server' + account.clientParam }) %>
Otherwise, Please follow the steps below to configure your existing PHP WebApp to use it with Auth0.
${snippet(meta.snippets.dependencies)}
This sample uses Composer, a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
use Auth0\SDK\Auth0;
$auth0 = new Auth0(array(
'domain' => '${account.namespace}',
'client_id' => '${account.clientId}',
'client_secret' => '${account.clientSecret}',
'redirect_uri' => '${account.callback}'
));
Now, we can call $auth0->getUser()
to retrieve the user information. If we call it from the page that will handle the callback, then it'll use the code
provided by Auth0 to get the information after the successful login.
// callback.php
...
$auth0->getUser();
if (!$userInfo) {
// We have no user info
// redirect to Login
} else {
// User is authenticated
// Say hello to $userInfo['name']
// print logout button
}
Once the user info is fetched, it'll be stored in the session. Therefore, from this moment on, each time you call getUser()
it will retrieve the information from the Session.
${include('./_callbackRegularWebApp')}
In this case, the callbackURL should look something like:
http://yourUrl/callback.php
${lockSDK}
Note: Please note that the
callbackURL
specified in theAuth0Lock
constructor must match the one specified in the previous step
You can access the user information via the getUser
method from Auth0
<?php
...
$userInfo = $auth0->getUser();
?>
<html>
<body class="home">
<div><?php echo $userInfo['name'] ?></div>
</body>
</html>
You can click here to find out all of the available properties from the user's profile. Please note that some of these depend on the social provider being used.
You have configured your PHP Webapp to use Auth0. Congrats, you're awesome!
By default, the SDK will store the user information in the PHP Session and it will discard the access token and the id token. If you like to persist them as well, you can pass 'persist_access_token' => true and 'persist_id_token' => true to the SDK configuration in step 2. You can also disable session all together by passing 'store' => false.
If you want to change PHP Session and use Laravel, Zend, Symfony or other abstraction to the session, you can create a class that implements get, set, delete and pass it to the SDK as following.
$laravelStore = new MyLaravelStore();
$auth0 = new Auth0(array(
// ...
'store' => $laravelStore,
// ...
));