Some of the TYPO3 sites that I’m responsible for maintaining, have login restricted pages. Normally, when a non-logged in user visits these pages, they’ll get shown a 404 page. That’s not at all helpful to them.
Bad, Bad, Bad UX – No Biscuit For You
Instead, do the simple act of showing the user a login page, that upon a successful login redirects them to the originally requested page.
Good UX – Do You Want Your Tummy Rubbed?
It’s easy to do this redirect action with a couple of edits to your typo3conf/localconf.php
and by creating a custom PHP script called pageNotFoundHandling.php
.
In typo3conf/localconf.php
place the following code.
$TYPO3_CONF_VARS['SYS']['curlUse'] = '1'; $TYPO3_CONF_VARS['FE']['pageNotFound_handling'] = 'USER_FUNCTION:fileadmin/scripts/pageNotFoundHandling.php:user_pageNotFound->pageNotFound';
curlUse
is required since our pageNotFoundHandling.php
script uses curl
. Otherwise, our whole show login and redirect effort won’t work.
Placing pageNotFoundHandling.php
in fileadmin/scripts isn’t required, but recommended if you don’t already have such a spot for custom scripts.
What’s in pageNotFoundHandling.php
?
<?php define(LOGIN_URL, 'http://www.example.com/login'); define(NOTFOUND_URL, 'http://www.example.com/page-not-found'); class user_pageNotFound { function pageNotFound($param, $ref) { if ($param['pageAccessFailureReasons']['fe_group'] != array(''=>0)) { header('HTTP/1.0 403 Forbidden'); $url = LOGIN_URL.'?redirect_url=' . $param['currentUrl']; } else { $url = NOTFOUND_URL; } session_start(); $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/'; session_write_close(); $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_COOKIE, $strCookie); $contents = curl_exec($c); curl_close($c); if ($contents) return $contents; else return FALSE; } } ?>
After creating your pageNotFoundHandling.php
script, make sure to edit the LOGIN_URL
and NOTFOUND_URL
definitions to your own login and 404 pages.
Bonus points…
What’s UX
? UX is short for user experience. A user experience leads to the way a person feels about using a product, system or service.
Therefore…
Bad UX ≠ Recommendations, Sales & Trust
Thank you FazzyX for your excellent code samples leading to this solution.
Have critiques, questions, trouble or thanks? Please leave a comment, I’ll respond.
Thanks a bunch!
I works nicely.
Hint: Do not use IDs for Loginurl: ?id=123 did not work