PHP Quickstart
This guide will help you integrate Descope's PHP SDK into your backend application. The same SDK is also used with Laravel. Follow the steps below to get started.
Set up Environment file
Create a .env
file in the root directory of your project with your Descope Project ID
, which can be found in the Console
If you plan to use Management functions, include a Descope Management Key
here as well, which can be found here.
Setup Backend SDK
You'll need to initialize a DescopeSDK
object using your Project ID.
If you're using a custom domain with your Descope project, make sure to export the Base URL (e.g. export DESCOPE_BASE_URI="https://api.descope.com"
) when initializing descope_client
.
Implement Session Validation
You will need to then fetch the session token from the Authorization header of each request, and use the SDK to validate the token.
The frontend SDK will store the session token in either a cookie or your browser's local storage. If using a cookie, the token will be sent to your app server automatically with every request.
The $descopeSDK->verify($sessionToken)
function can be used to verify a user's session as shown below. This returns a TRUE or FALSE depending on if the JWT is valid or not.
By default, the SDK uses APCu for caching, provided it is enabled and configured in your environment. If APCu is not available, and no other caching mechanism is provided, caching is disabled. Read about custom caching for the Descope SDK here.
Once you've implemented the basic session validation, you can enhance your application with these additional features:
User Management
Learn how to create, update, and manage users in your application.
Role-Based Access Control
Implement fine-grained access control using roles and permissions.
API Gateway Session Validation
Secure your API endpoints with session validation middleware.
Set up M2M Authentication
Implement M2M authentication with Descope.
Additional Resources
Have You Implemented the Frontend Yet?
When integrating Descope into your application, you have three options depending on how much control you want over your frontend authentication experience and session management:
Option | Description | Best For |
---|---|---|
Use Descope Flows | Design your authentication screens and flows visually in the Descope Console with little or no frontend code. We handle all session management for you. | Fastest setup with minimal custom frontend work. |
Use Descope Client SDKs | Build your own login screens and authentication experiences in your frontend using code, while relying on Descope's SDKs to manage sessions (login, logout, refresh). | Customizable UX with simplified session handling. |
Use Descope Backend SDKs | Build your own frontend and your own backend APIs for authentication. You fully manage sessions, tokens, and authentication logic yourself. | Maximum flexibility and control, at the cost of more engineering effort. |
Descope Flows
Design login screens and authentication flows visually without writing code, and embed them into your app.
Descope Client SDKs
Build custom login screens in your frontend using code, while we manage session tokens and refreshing for you.