Jump to content

Redirect Issue using hooks


WHMCSModuleNetworks

Recommended Posts

Hello,

 

Currently i am created a hook to redirect to some module page but it gives me error (Image attached) but it is something else like google url then it works.

 

Hook Code is:

 

use Illuminate\Database\Capsule\Manager as Capsule;
function hook_forceuserredirect($vars){
$cstatus = Capsule::table('tblclients')->select('status')->where('id', $_SESSION['uid'])->get(); 

global $CONFIG;
   # Add Redirection URL Here
   $URL = $CONFIG['SystemURL'].'/index.php?m=AccountClosed';

   if ($cstatus[0]->status != 'Active'){ 
//header('Location: index.php?m=AccountClosed');   // If used this gives error
//header('Location: https://www.google.co.in');  //It works
   echo '<meta http-equiv="refresh" content="0; url='.$URL.'">'; // if used this Gives Error
   exit;
}

}

add_hook('ClientAreaPage', 1, "hook_forceuserredirect");

 

Rather than above i mean if i am not using hooks and accessing the page it gives the output of the page

whmcserror.png

Link to comment
Share on other sites

there are a few issues with the above hook - the biggest one being that it will constantly redirect to itself... :roll:

 

e.g let's say i'm not logged in as a client and visit the homepage... the hook won't be able to detect the client status, so the $cstatus value won't equal active and will redirect to index.php?m=accountclosed - which if the module doesn't exist will redirect back to index.php and the loop starts all over again... if you're linking to an external URL, then you won't trigger this loop issue.

 

therefore, you need some way to break the loop... I suspect the following hook would be closer to the solution, though might need the module present to work...

 

<?php

function hook_forceuserredirect($vars){

   global $CONFIG;    
   $client = Menu::context('client'); 
   $cstatus = $client->status;

   # Add Redirection URL Here
   $URL = $CONFIG['SystemURL'].'/index.php?m=AccountClosed';

   if ($client and $cstatus != 'Active' and !isset($_REQUEST['m'])){ 
//header('Location: index.php?m=AccountClosed');   // If used this gives error
//header('Location: https://www.google.co.in');  //It works
   echo '<meta http-equiv="refresh" content="0; url='.$URL.'">'; // if used this Gives Error
   exit;
   }
}

add_hook('ClientAreaPage', 1, "hook_forceuserredirect");

as you can see, I got rid of Capsule as you don't need to query the database to get the client status value (the query was slightly wrong anyway) - you can get that info using the Class method... also it now detects if the person is logged in (a client) and so only if they're logged in, their status is not "Active" and the url doesn't pass a value for m does it redirect.... you could do the same by checking $cstatus equals specific values (inactive or closed) rather than not equalling 'active'.

Link to comment
Share on other sites

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