Code Migration – Redirect User to Maintenance Page using .htaccess

One common approach of code migration. Redirect normal users to maintenance page while migration testers carry out production testing after completion. The solution – amend our .htaccess file to redirect all incoming request to maintenance except for request from testers’ office IP address. #.htaccess RewriteEngine on #Check if request is not server_down_message.html and ip is not from testers' ip address #Change xxx.xxx.xxx.xxx to your ip address, eg. 202.126.4.10 RewriteCond %{REQUEST_URI} !/server_down_message.html$ RewriteCond %{REMOTE_HOST} !^xxx.xxx.xxx.xxx #To allow more IP addresses, simply append "RewriteCond %{REMOTE_HOST} !^xxx.xxx.xxx.xxx" under this line #Both conditional are true #Not requesting for server_down_message.html and IP address not from tester. #Redirect to server_down_message.html RewriteRule $ /server_down_message.html [R=302,L] Hope it helps you and good luck with your...

ASP.Net List Check For Duplicates

To check for duplicates values/objects’ properties between 2 list or the list itself, we use the .Exist() function provided by the List collection type. //Code in C# List<string> lstListA = new List<string>(); List<string> lstListB = new List<string>(); lstListA.Add("ADuplicate"); lstListA.Add("Some"); lstListA.Add("Some Brown Cow"); lstListA.Add("Some Brown Cow"); lstListB.Add("ADuplicate"); lstListB.Add("This"); lstListB.Add("Don't"); lstListB.Add("Contain"); lstListB.Add("Duplicates"); //Check for duplicates between 2 list foreach(string strValue in lstListA) { if(lstListB.Exists(delegate (string match){ return match.ToLower().Trim() == strValue.ToLower().Trim(); })){ //duplicate found } } //Check for duplicates within the list //Create 2 list, one will hold values with no duplicates and 1 will hold duplicate values found List<string> lstNoDuplicate = new List<string>(); List<string> lstDuplicate = new List<string>(); foreach (string strValue in lstListA) { if (lstNoDuplicate.Exists(delegate(string match) { return match.ToLower().Trim() == strValue.ToLower().Trim(); })) { //duplicate found //we add to duplicate list lstDuplicate.Add(strValue); } else { //no duplicate found //we add to no duplicate list lstNoDuplicate.Add(strValue); } } //lstDuplicate will contain items that are duplicated in lstListA //lstNoDuplicate will contain unique items in...

ASP.Net Singapore NRIC Validation

This function may come in handy if you need to ensure user inputted Singapore NRIC is a valid one. Refer to wiki on the algorithm. The below function check for NRIC starting with prefix T or S. public static bool isValidSgFin(string strValueToCheck) { strValueToCheck = strValueToCheck.Trim(); Regex objRegex = new Regex("^(s|t)[0-9]{7}[a-jz]{1}$", RegexOptions.IgnoreCase); if (!objRegex.IsMatch(strValueToCheck)) { return false; } string strNums = strValueToCheck.Substring(1, 7); int intSum = 0; int checkDigit = 0; string checkChar = ""; intSum = Convert.ToUInt16(strNums.Substring(0, 1)) * 2; intSum = intSum + (Convert.ToUInt16(strNums.Substring(1, 1)) * 7); intSum = intSum + (Convert.ToUInt16(strNums.Substring(2, 1)) * 6); intSum = intSum + (Convert.ToUInt16(strNums.Substring(3, 1)) * 5); intSum = intSum + (Convert.ToUInt16(strNums.Substring(4, 1)) * 4); intSum = intSum + (Convert.ToUInt16(strNums.Substring(5, 1)) * 3); intSum = intSum + (Convert.ToUInt16(strNums.Substring(6, 1)) * 2); if (strValueToCheck.Substring(0, 1).ToLower() == "t") { //prefix T intSum = intSum + 4; } checkDigit = 11 - (intSum % 11); checkChar = strValueToCheck.Substring(8, 1).ToLower(); if (checkDigit == 1 && checkChar == "a") { return true; } else if (checkDigit == 2 && checkChar == "b") { return true; } else if (checkDigit == 3 && checkChar == "c") { return true; } else if (checkDigit == 4 && checkChar == "d") { return true; } else if (checkDigit == 5 && checkChar == "e") { return true; } else if (checkDigit == 6 && checkChar == "f") { return true; } else if (checkDigit == 7 && checkChar == "g") { return true; } else if (checkDigit == 8 && checkChar == "h") { return true; } else if (checkDigit == 9 && checkChar == "i") { return true;...

PHP Formatting Date

To display current date we use the date function in PHP. <?php //string date ( string $format [, int $timestamp ] ) echo date("Y-m-d H:i"); ?> Result: 2018-11-17 13:02 The date() function requires 1 parameter which is the string format to format the current date time to our liking. Refer to the php date documentation for more details on the formatting string. The 2nd optional parameter which takes in an integer value representing the date time comes in handly where in some situation, we need to display specific dates. We use mktime() function to create the specific date time. <?php //int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] ) //mktime(8,0,0,10,20,2010) creates a date time specific to 20th October 2010, 8:00 am echo date("Y-m-d H:i",mktime(8,0,0,10,20,2010)); ?> Result: 2010-10-20...

Introduction to PHP

PHP = Hypertext Preprocessor So what is PHP? Google “what is PHP?”, you will get loads of result on the definition. Here’s some: PHP – Wikipedia, the free encyclopedia PHP Introduction In this category you will find tutorials on common PHP codes, reusable classes for use in your projects. All codes released in this section are licensed under GPL, unless otherwise specified by the original...