Maintenance mode / disable login

Tips submitted by PHPMaker users
Post Reply
sticcino
User
Posts: 1043

Re: Maintenance mode / disable login

Post by sticcino »

this is what we use for maintenance...

	// User Logging In event
	function User_LoggingIn($usr, &$pwd) {

            if(_getPreferences('MAINTENANCE_MODE')) {
                if( strtolower($usr) == strtolower(Config("ADMIN_USER_NAME"))) return true; // check if an administrator.. let them in

                $this->terminate(_getPreferences("MAINTENANCE_MODE_REDIRECT"));
                return FALSE;
            }

OR

	// Page_Load event
	function Page_Load() {

            if(_getPreferences('MAINTENANCE_MODE')) {
                $this->terminate(_getPreferences("MAINTENANCE_MODE_REDIRECT_URL"));
                return FALSE;
            }

you could do this on the Page_Load event, but then there would be no check to see if its an admin trying to get into the system to disable the maintenance mode or do other tasks.... (unless you have a backdoor or direct access to the DB to change the maintenance mode flag -- then you can put it in page_load).

_getPreferences() is just a global function we use to get values from a table for various app settings, substitute with your logic
MAINTENANCE_MODE_REDIRECT = a web page or site that you want to display, page gets loaded and the function is set to fail the login and end...

just my 2 cents


philmills
User
Posts: 535

Post by philmills »

Here's a method which works great in v2022 (and probably v2023 though I haven't tested yet):

  1. in Global Code add the following var:

    function IsMaint(){
      //hardcoded version
      $getMaint='0'  //hardcoded -  set 0 for off, 1 for on
      //database table version (create a table 'Settings' with Value and Setting columns)
      //$getMaint=ExecuteScalar("SELECT Value FROM Settings WHERE Setting='Maintenance'", "DB"); 
      return $getMaint;
    }
  2. In Gobal Events under Page_Rendering add this:

       if (CurrentUserLevel() != '-1') {     // make sure superadmin can still login
    	if(IsMaint()=='1'){  // if maintenance mode is on
    		if (IsLoggedIn()){
            		CurrentPage()->terminate("logout");  // logout user
    		}
    	} 
        }
  3. In Server Events > Other > Login > Page_DataRendered add either option a) or b)
    Option a) Uses text from your language file or Language_Load

        $footer = "";
        if(IsMaint()=='1'){
           $db =& DbHelper(); 
           $footer .= "<div style='margin: auto; margin-bottom: 20px; width: 300px; text-align: center;' class='text-danger h5'>".$Language->phrase("Maintenance_Mode")."</div>";  
           //  replace the variable name $Language->phrase("Maintenance_Mode") with your own, or add it to Language_Load 
        }

Option b) Uses simple text

    $footer = "";
    if(IsMaint()=='1'){
       $footer .= "<div style='margin: auto; width: 300px; text-align: center;' class='text-danger h5'>Site is under Maintenance, please try again later</div>";
    }

Hope someone finds this useful :)


Post Reply