Jump to content

Redirect non-logged in users to custom login page


samuelyip74

Recommended Posts

Hi,

I created a custom login page and added a hook to redirect non-logged in user to the custom login page.

Here the hook:

<?php
use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPage', 1, function($vars) {
    // Perform hook code here...
    $client = Menu::context('client');

       if (is_null($client)) {
            header("Location: https://www.mysite.com/clientarea/login.html");
       }

});

I tested and is working fine.

How can I exclude certain templatefile from redirecting to the custom login page? I need to exclude logout.php, pwreset.php from redirecting to the custom login page.

It seem the hook I created forces all non-logged in user that touch the clientareapage to redirect to the custom login page, including logout.php, pwreset.php etc.

Any advise?

Link to comment
Share on other sites

  • 4 weeks later...
  • 1 year later...

After making the update to WHMCS 8.04, I found that this hook causes excessive redirects and must be disabled.  Is there a modification that needs to be made to it for it to function properly?

Here is the code specified in the aforementioned posts
 


if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function hook_ForceEveryoneToLogin($vars) {

   $clientID = intval($_SESSION['uid']);
   $adminID = intval($_SESSION['adminid']);

   if ($adminID===0){
       if (!in_array($vars['filename'], array("login","dologin","clientarea","pwreset", "register")) && $clientID===0){
           header("Location: login.php");
           exit;
       }
   }

}
add_hook("ClientAreaPage", 1, "hook_ForceEveryoneToLogin");

 

Link to comment
Share on other sites

21 hours ago, javacodemonkey said:

After making the update to WHMCS 8.04, I found that this hook causes excessive redirects and must be disabled.  Is there a modification that needs to be made to it for it to function properly?

the issue is possibly caused by index.php being used far more than it was years ago and with index not being included in the whitelist of filenames - including it in the list could be a quick fix, but might then allow access to pages that you don't want non-clients to see.

ultimately, you might need to use a combination of filenames and template names to be more specific about what you want non-clients to be able to view.

Link to comment
Share on other sites

I ended up doing something similar by checking the URL and sending to the login page with an if/else statement.  I'll share it here so that others may use it as needed.
 

<?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function hook_ForceEveryoneToLogin($vars) {

   $clientID = intval($_SESSION['uid']);
   $adminID = intval($_SESSION['adminid']);
   
   // Create the link for the incoming URL.
    $link = "https";
    $link .= "://"; 
    $link .= $_SERVER['HTTP_HOST']; 
    $link .= $_SERVER['REQUEST_URI'];

   if ($adminID===0){
       if (!in_array($vars['filename'], array("index")) && $clientID===0){
            // Check if the link is accurate.
            if($link != "https://billing.dang-designs.com/index.php?rp=/login"){
                header("Location: login.php");
                exit;
            }
       }
       else if($clientID===0){
           // Check if the link is accurate.
           if($link != "https://billing.dang-designs.com/index.php?rp=/login"){
                header("Location: login.php");
                exit;
            }
       }
   }

}
add_hook("ClientAreaPage", 1, "hook_ForceEveryoneToLogin");

 

Link to comment
Share on other sites

  • 6 months later...

Is there away to have this but allow the direct store links to work or at least the direct to product link to work? 

 

Path I am looking for - Order page on my site (not whmcs) / click purchase / direct product link to purchase / once complete then access to the client area

Link to comment
Share on other sites

  • 5 months later...
  • 11 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated