This following trick will show you how to restrict login time in web applications that generated by PHPMaker. Let' say, you don't want your web application users login before 03.00 AM and after 11.00 PM (for example, since there are additional back-end process in the server after 11.00 PM and before 03.00 AM). If users are trying login after 11.00 PM and before 03.00 AM, then system will automatically reject them. Also, let system logs them to the Audit Trail.
Fortunately, PHPMaker has the server event for this, which is called with User_LoggingIn. We can implement it very easy. In other words, we will not modify any generated script files for this, as I always avoid it all the time. By using this trick, then you will learn how to check whether the current time is located between two different time in that day. If this criteria is fulfiled, then the current time is a valid time, so the users are able to login to your system. You will also learn how to concat the current time with the current date, so the result can be used to compare with the beginning and the end of your desired date and time.
I have implemented this trick in one of my many web applications (also for the same reason above), and it works perfectly.
[hidepost]
-
Insert the following code to the Server Events -> Global -> All Pages -> Global Code of PHPMaker application:
function VerifyMyTime($current_time) { $begin_time = "03:00:00"; $end_time = "23:00:00"; $current_date = DateTime::createFromFormat("H:i:s", $current_time); $begin_date = DateTime::createFromFormat("H:i:s", $begin_time); $end_date = DateTime::createFromFormat("H:i:s", $end_time); if ($current_date > $begin_date && $current_date < $end_date) { return true; } else { return false; } } [/code] </li> <li> The next step, insert the code below to your <strong>Server Events</strong> -> <strong>Other</strong> -> <strong>Login Page</strong> -> <strong>User_LoggingIn</strong> function, so you will have the complete code just like the following: [code lang="php"] // User Logging In event function User_LoggingIn($usr, &$pwd) { // Enter your code here // To cancel, set return value to FALSE $my_current_time = date("H:i:s"); if (VerifyMyTime($my_current_time)==false) { ew_WriteAuditTrail("log", ew_StdCurrentDateTime(), ew_ScriptName(), $usr, "login is not allowed at this time.", ew_CurrentUserIP(), "", "", "", ""); $this->setFailureMessage("Sorry, you are not allowed login at ".$my_current_time.".<br><br>You will be allowed after 03:00 AM."); return FALSE; } return TRUE; }
- Finally, re-generate your script files using PHPMaker, as always.
[/hidepost]
Leave a Reply
You must be logged in to post a comment.