Autologin link to WHMCS in email

Most of you probably knows about great WHMCS AutoAuth feature, which allows us to create links to send user to its client area without prompting username and password.

But what if you like to have such link sent in email?

That could be a challenge, because:

The timestamp must be within 15 minutes of the server time for the autoauth to be accepted, otherwise the link is considered to be expired

The problem is actually about not letting use same email link for autologin under different email.

So lets go to into admin -> Email Templates -> Your Template
First, we should setup secure link generation in email template. We simply add additional parameter. Lets call it hash.

Click below to login automatically:
{assign var='hash' value=$client_email|concat:"your_secret_key"}
http://yourdomain.com/autologin.php?email={$client_email}&hash={$hash|md5}

We will check this very special parameter on a server side.
autologin.php:

# Define WHMCS URL & AutoAuth Key
$whmcsurl = "http://yourdomain.com/dologin.php";
$autoauthkey = "auto_auth_key"; #AutoAuth Key, ATTENTION: should be defined in configuration.php aswell!
$secret_key = "your_secret_key"; #Should be same as you defined in email template

if (md5($_GET['email'].$secret_key) != $_GET['hash'])
die();//dying here because hash is not equal

$timestamp = time(); # Get current timestamp
$email = $_GET['email']; # Clients Email Address to Login
$goto = "clientarea.php"; # Here you can set default user page

$hash = sha1($email.$timestamp.$autoauthkey); # Generate Hash

# Generate AutoAuth URL & Redirect
$url = $whmcsurl."?email=$email&timestamp=$timestamp&hash=$hash&goto=".urlencode($goto);
header("Location: $url");
exit;

To make everything work you will need:
Smarty Concat Modifier (1696 downloads) Smarty Concat modifier (put it at /includes/smarty/plugins/)
Custom autologin.php (2047 downloads) (put it at root WHMCS directory)
– and dont forget to change you secret keys and define $autoauthkey in configuration.php

UPDATE

For 5.x users you don’t need to upload smarty concat plugin, cause it should be available already, it is called “cat”. So calling it in email template will look like:

Click below to login automatically:
{assign var='hash' value=$client_email|cat:"your_secret_key"}
http://yourdomain.com/autologin.php?email={$client_email}&hash={$hash|md5}

Little about me:

Im professional web developer. Love to work with PHP-driven projects. WHMCS seems to me the way to share my knowledges. Some of you may be interested in hiring me - drop me a line or take a look at my Odesk profile

17 Comments + Add Comment

  • Thanks very much !!!!

  • Hey – this really saved me some time. One small issue…smarty has updated so the concat function is no longer needed (it’s included in the current version of smarty shipping w/ whmcs AND the function is called via cat, not concat as in your example.

    Those small things aside, this was a lifesaver. Thank you!!

    Grant

    • Thanks, Grant!

      I’ve just updated the post with note for 5.x whmcs users.

  • Is there a way to set in the string on the email template to where the client go? clientarea, supporttickets, invoices etc? now the system only goes to clientarea.

    • sure, you can have additional parameter for autologin.php and pass it as $goto string.
      so in email it will look like: http://yourdomain.com/autologin.php?email={$client_email}&hash={$hash|md5}&goto=URLENCODED_LINK

      note no need to define absolute path, just yourpage.php and that’s it

    • Serg, congratulations for this great module!
      I made one modification in autologin.php. I changed the lines:
      $goto = “clientarea.php”;

      to:
      $goto = “viewinvoice.php?id=”.$_GET[‘invoice’];

      And changed my Email Template line from:
      http://yourdomain.com/autologin.php?email={$client_email}&hash={$hash|md5}

      to:
      {$whmcs_url}/autologin.php?email={$client_email}&invoice={$invoice_num}&hash={$hash|md5}

      Now, I don’t need to put the domain manually and clients can access their Invoices without login! Try It! 😉

      • Cool!
        Yes it does work, I implemented such thing some time ago with invoice and product details.

  • Hello!

    I’m using &goto=clientarea.php?action=changepw, but I’m being redirected to clientarea.php index

    Any ideas?

    Thanks

  • Hello, i don´t know why, everything was working nice, but now i´m getting a blank page when try to acess using the autologin, do you know what could be that is making this blank page? I already tried to enable the debug mode from whmcs, but nothing was showed.

    Thanks

  • i verified if i remove the line “die();//dying here because hash is not equal” after the get hash, the script works for me again, with this line not.

    What could be? there is any php variable disable/enable on php.ini?

    Thanks

  • […] acordo com nossas necessidades, mais é o que temos então vamos a luta né! Referências usadas: Whmcsjet Forum do Portal do Host Whmcs […]

  • Excellent script and great idea – thanks for sharing!

    I have followed the instructions carefully including the $autoauthkey and secret key and the url is generated nicely e.g.:

    http://domain.com/autologin.php?email[email protected]&hash=xxx41xx8xx98xx00xx204xx9800998xxx842

    but goes to blank page. using whmcs 5.2.

    Please help and thanks again for sharing.

  • I’ve been working on this module and updated some code on it to work with my system, you can view the project here https://github.com/zmjwong/whmcs-autoauth-invoice

    This is being on by me from http://www.zenithmedia.ca

    • Hi Jon, congratulations for your great module and thank you very much for citation to me! 😉
      “One is glad to be of service”
      Or, in portuguese version: “Isto fica feliz em ser útil!” (y)

  • I cant seem to get this or Jon’s script to work I must be missing something?
    Or is it because my main site is in wordpress?

    All I am getting is a 404 error.

    Any assistance would be appreciated.

    • Its possible wordpress could cause the problem, assuming you mean you’re main site is in WP you mean your WHMCS install is being bridged by a plugin then that could cause problems since i’ve not tested that install type.

      We have had the script working ever since we posted and it has not caused any problems for us.

  • Hi Jon, I’m McGuyver!
    How are You?

    Unfortunately now this module is not working on WHMCS v7, according to
    some friends. It seems that Smarty is not working with the function |md5 (from PHP).
    I saw that Smarty lets you use the PHP functions and that the function |md5 is not a standard modifier, as explained in the manual (http://www.smarty.net/docs/en/language.modifiers.tpl):

    Do you have any idea what to do?
    Thank yo a lot for your great help!

Leave a comment