php whmcs hook redirected you too many times

Solution for php whmcs hook redirected you too many times
is Given Below:

<?php
    
        //redirect clients to invoice page if they are overdue. Nothing else.
        //provided by https://www.whmcs.guru
        use IlluminateDatabaseCapsuleManager as Capsule;
    
        function check_client_access($vars)
        {
        //what time is it?
        $time = time();
        //are we logged in? If not, then return
        $uid  = $_SESSION['uid'];
        if (empty($uid))
        {
            return;
        }
        $filename = $vars['filename'];
        $displayTitle = $vars['displayTitle'];
        //do we have any unpaid invoices?
        $rows = Capsule::table('tblinvoices')->select('id') ->WHERE ('userid', '<=', $uid) ->where('status', '=', 'Unpaid') ->count();
    
        if (empty($rows))
        {
    
            //we have no unpaid invoices. Do not proceed any further
            return;
        }
    
        if (!empty($rows))
        {
            //we have unpaid invoices. Are they overdue?
    
            foreach (Capsule::table('tblinvoices')->select('duedate') ->WHERE ('userid', '=', $uid) ->where('status', '=', 'Unpaid')->get() as $unpaidinvoice) {
    
                $invdue = $unpaidinvoice->duedate;
    
                if (strtotime($invdue) < $time) {
    
                        //all those checks, we're finally where we need to be
                        header('Location: clientarea.php?action=invoices'); 
                        
                        exit();
                    }
    
            }
    
        }
    
        }
        add_hook('ClientAreaPage', 1, "check_client_access");