How to do single sign-on with PHP?

How to do single sign-on with PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to do single sign-on with PHP?

Single Sign-On (SSO) allows users to authenticate once and access multiple related services without needing to re-enter credentials for each one. You are likely familiar with this concept from platforms like Gmail, Orkut, or Feedburner, where a single Google account grants immediate access to various tools. While these legacy examples illustrate the utility of SSO, implementing it in a modern PHP application requires moving away from simple email/password forms and integrating with an Identity Provider (IdP) via standard protocols.

The industry standard for this integration is OAuth 2.0, often combined with OpenID Connect (OIDC). This combination separates authorization (what resource you can access) from authentication (who you are), which is crucial for security. In a PHP context, you generally do not handle the cryptographic exchange of tokens yourself; instead, you rely on well-documented library implementations to manage the handshake between your application and the IdP.

The implementation flow typically follows a specific sequence of events. When a user attempts to access a protected resource, your PHP backend redirects them to the IdP's authorization endpoint. The URL includes specific parameters: client_id identifies your application, redirect_uri tells the IdP where to send the user after they log in, and scope defines the permissions requested (e.g., reading profile information).

$authUrl = 'https://accounts.google.com/o/oauth2/v2/auth';
$params = [
    'client_id'     => 'YOUR_CLIENT_ID',
    'redirect_uri'  => 'https://your-app.com/callback',
    'response_type' => 'code',
    'scope'         => 'openid email profile',
    'access_type'   => 'offline' // Useful for refreshing tokens later
];

// Ensure the redirect URI is URL-encoded
$query = http_build_query($params);
$authUrl .= '?' . $query;
header('Location: ' . $authUrl);

Once the user authenticates on the IdP's interface, they are redirected back to your redirect_uri. The IdP appends an authorization_code to the URL. Your PHP application must intercept this request and exchange that short-lived code for an access token and an ID token. This step is critical because the authorization code can only be used once and has a very short expiration window.

$tokenUrl = 'https://oauth2.googleapis.com/token';
$postData = [
    'code'          => $_GET['code'],
    'client_id'     => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET', // Keep this secure, never in env files for production
    'redirect_uri'  => 'https://your-app.com/callback',
    'grant_type'    => 'authorization_code'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$response = curl_exec($ch);

After obtaining the tokens, you must verify the ID token's integrity. This involves validating the digital signature using the IdP's public keys and checking claims like iss (issuer) and aud (audience). This prevents attackers from forging user identities. Finally, you can extract the user's name, email, and picture to populate your database or session.

A common pitfall is storing sensitive data in the session without verifying the token first. Additionally, relying on third-party libraries like google/oidc or league/oauth2-client is safer than writing raw HTTP logic, as these packages handle edge cases in the protocol specification. For larger projects using Laravel, the ecosystem offers robust bindings that abstract much of this complexity, allowing you to focus on business logic rather than cryptographic handshake details.

When choosing an IdP, consider the trade-offs between self-hosting a solution like Keycloak versus integrating with managed providers like Google or Azure AD. Managed providers offer immediate scalability and security updates but may incur costs or strict branding requirements. Self-hosted solutions provide full control over data residency but require significant maintenance overhead. Regardless of the provider, ensuring your redirect_uri is explicitly whitelisted in the IdP dashboard is a mandatory configuration step that is frequently overlooked during initial setup.

Our network