View on Github


Gravatar for
By Josh Cunningham

This tutorial demonstrates how to add user login to a PHP application. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: Apache 2.4.4 | PHP 5.6.14 and up | Auth0-PHP 5.0 and up

New to Auth? Learn How Auth0 works, how it integrates with Regular Web Applications and which protocol it uses.

Configure Auth0

Get Your Application Keys

When you signed up for Auth0, a new application was created for you, or you could have created a new one.

You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.

You need the following information:

  • Client ID
  • Domain

If you download the sample from the top of this page these details are filled out for you.

If you have more than one application in your account, the sample comes with the values for your Default App.

App Dashboard

Configure Callback URLs

A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated.

You need to whitelist the callback URL for your app in the Allowed Callback URLs field in your Application Settings. If you do not set any callback URL, your users will see a mismatch error when they log in.

If you are following along with the sample project you downloaded from the top of this page, you should set the Callback URL to http://localhost:3000/.

Configure Logout URLs

A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in the returnTo query parameter.

You can set the logout URL for your app in the Allowed Logout URLs field in your Application Settings. If you don't set a logout URL, your users will be unable to log out of the application and will get an error.

Configure PHP to Use Auth0

Add the Dependencies

To install dependencies, run the following

This sample uses Composer, a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs. Then, it installs them in your project.

Configure Auth0 PHP SDK

Configure the Auth0 PHP SDK for each page that will use it.

Add the Auth0 Callback Handler

Call $auth0->getUser() to retrieve user information. If you call it from the page that handles the callback, it will use the code provided by Auth0 to get the information after the successful login.

The user's information is stored in the session. Each time you call getUser(), it retrieves the information from the session.

Trigger Authentication

The redirect_uri specified in the Auth0 constructor must match the URL specified in the Add the Auth0 Callback Handler step.

Display User Information

You can access user information with the getUser method from Auth0.

To learn about all the available properties from the user's profile, read the user profile documentation.

Some of the user profile properties depend on the social provider you use.

Optional: Configure session data

By default, the SDK stores user information in the PHP session and discards the access and ID Tokens.

To keep the tokens, to the SDK configuration, pass the following:

  • 'persist_access_token' => true
  • 'persist_id_token' => true

To disable the session, pass 'store' => false to the SDK configuration.

Instead of using the PHP session to store information, you can use Laravel, Zend, Symfony or similar techniques. To do that, create a class that implements the get, set and delete methods and pass it to the SDK.

Use Auth0 for FREE