hookwhmcsrestrict

force require login to show cart & store pages in whmcs 8.1


i want restrict two page for if user want to see this page, redirect to login page.

I create hook file (includes/hooks/disao.php) with this code

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

function hook_ForceEveryoneToLogin($vars) {

  $client = Menu::context('client');
  $validpages = array("login","dologin","clientarea","pwreset","contact","index");
  if (!$client && !in_array($vars['filename'],$validpages)) {
    header("Location: login.php");
    exit;
  }
}
add_hook("ClientAreaPage", 1, "hook_ForceEveryoneToLogin");

but this code not work on whmcs 8.1 and work only on whmcs 7.10. Noice : want restrict pages : Cart and Store


Solution

  • The hook's $vars array parameter has loggedin key to check if the current user is logged in, code updated:

    <?php
    if (!defined("WHMCS")) {
       die("This file cannot be accessed directly");
    }
    
    function hook_ForceEveryoneToLogin($vars) {
    
        $validPages = ["login", "dologin", "clientarea", "pwreset", "contact", "index"];
        if (!$vars['loggedin'] && !in_array($vars['filename'],  $validPages)) {
            header("Location: login.php");
            exit();
        }
    }
    add_hook("ClientAreaPage", 1, "hook_ForceEveryoneToLogin");