GetSupportX
Portal sign-in docs
Portal sign-in guide

PHP

For PHP websites that aren’t WordPress and have their own login.

Needs a developer

Before you start

In GetSupportX, open Settings → Help center & portal → Portal sign-in and switch on Sign in through your website. Answer What is your website built on? with Other code — PHP, Node.js, Python.

Under 1 · Secret key, click Create secret key. Copy the key straight away: it is shown once. The code on this page needs it.

Copy the code from 2 · Add the code to your website (pick PHP under “Which language is your website’s code in?”): your callback URL is already filled in there. Copying the code from this page instead? Replace the whole https://<your-help-center>/portal/handoff address with the Callback URL Settings shows under For developers in step 2. Don’t build it by hand: it isn’t the same for every help center.

Steps

  1. Upload gsx-sign-in.php next to your other pages

  2. Read your own logged-in user

    Fill in gsx_current_user() so it returns the user’s id, email and name, or null when nobody is logged in.

  3. Set your site’s address and login page

    GSX_SITE is your website’s address; GSX_LOGIN is your login page. Your login page should send people on to the next address after they log in.

  4. Set your secret key on the server

    As an environment variable called GSX_SSO_SECRET (your host’s control panel or your developer can do this). Don’t paste the key into the file.

  5. Set your sign-in link

    https://yourwebsite.com/gsx-sign-in.php

The code

gsx-sign-in.php
<?php
/**
 * gsx-sign-in.php — GetSupportX sign-in for a PHP website (not WordPress).
 * Upload it next to your other pages, then make two changes: gsx_current_user()
 * below, and GSX_LOGIN if your login page lives elsewhere.
 *
 * Your sign-in link is:  https://yourwebsite.com/gsx-sign-in.php
 * Needs GSX_SSO_SECRET in your server's environment (never in this file).
 */
const GSX_CALLBACK = 'https://<your-help-center>/portal/handoff';
// Your site's address. It keeps customer ids unique if several websites share
// one GetSupportX workspace — set it once and never change it.
const GSX_SITE  = 'https://yourwebsite.com';
const GSX_LOGIN = '/login.php'; // your login page

// REPLACE THIS with however your site reads its logged-in user. Return
// array( 'id' => ..., 'email' => ..., 'name' => ... ), or null when logged out.
function gsx_current_user() {
  // session_start();
  // return isset( $_SESSION['user'] ) ? $_SESSION['user'] : null;
  return null;
}

// The portal page they were heading to (?gsx_return=). Only a plain path is
// passed on — GetSupportX checks it again before using it.
$back = isset( $_GET['gsx_return'] ) && is_string( $_GET['gsx_return'] ) ? $_GET['gsx_return'] : '';
if ( '/' !== substr( $back, 0, 1 ) || '//' === substr( $back, 0, 2 ) || '/\\' === substr( $back, 0, 2 ) ) $back = '';

$user = gsx_current_user();
// The redirect below signs one person in — never let a cache keep it. Sent
// AFTER gsx_current_user(): a session_start() in there can set a cacheable
// Cache-Control of its own (session.cache_limiter), and this one must win.
header( 'Cache-Control: no-store' );
if ( ! $user ) {
  // "next" is this full address, so they come back here after logging in.
  header( 'Location: ' . GSX_LOGIN . '?next=' . rawurlencode( $_SERVER['REQUEST_URI'] ) );
  exit;
}

$secret = getenv( 'GSX_SSO_SECRET' );
if ( ! $secret ) {
  http_response_code( 500 );
  echo 'GSX_SSO_SECRET is not set — paste your SSO secret into your server environment.';
  exit;
}

$host = parse_url( GSX_SITE, PHP_URL_HOST );
$port = parse_url( GSX_SITE, PHP_URL_PORT );
$payload = array(
  'externalId'       => $host . ( $port ? ':' . $port : '' ) . ':' . $user['id'],
  'site'             => GSX_SITE,
  'legacyExternalId' => (string) $user['id'], // keeps customers an older snippet created
  'email'            => isset( $user['email'] ) ? $user['email'] : '',
  'name'             => isset( $user['name'] ) ? $user['name'] : '',
  'iat'              => time(),
  'exp'              => time() + 300,
  'jti'              => bin2hex( random_bytes( 16 ) ), // one-time id: this exact link works once
);
// Drop empty values so a blank field never overwrites something an agent typed.
$payload = array_filter( $payload, function ( $v ) { return $v !== '' && $v !== null; } );

$b64 = base64_encode( json_encode( $payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) );
$sig = hash_hmac( 'sha256', $b64, $secret );

$url = GSX_CALLBACK . '?payload=' . rawurlencode( $b64 ) . '&sig=' . $sig;
if ( $back !== '' ) $url .= '&redirect=' . rawurlencode( $back );

header( 'Location: ' . $url, true, 302 );
exit;

Every version of this code signs the same way. The protocol reference lists each field and rule, including the optional customer details (plan, phone and more) you can add.

Check it works

  1. Save your sign-in link

    In Settings → Help center & portal → Portal sign-in, enter it under 3 · Sign-in link (for example https://yourwebsite.com/gsx-sign-in.php) and click Save. What is the sign-in link?

  2. Click Test sign-in

    Under Check it works. We open your link as a visitor who isn’t logged in. “Your sign-in link is live” means your link sent that visitor somewhere other than your homepage or the portal, and names the address. Check that address is your login page.

  3. Try it as a customer

    Log in to your website, then open your sign-in link. You should land in your portal, signed in. If not, Check it works lists the reason under recent failed sign-ins, and Troubleshooting says how to fix it.

Good to know

  • If GSX_SSO_SECRET is missing, logged-in customers see an error instead of being signed in. Test sign-in can’t see that (it isn’t logged in), so log in and open your sign-in link once yourself.
  • Keep GSX_SITE the same forever. It is part of every customer’s id.

Read next