Denny Labs

De Web Monster

PHP XMPP Google Talk send notification message to IPhone

The example below, I am going to show you how you can send notifications using PHP XMPP library. I am using CodeIgniter 2.0, a php framework, PHP XMPP library and MySQL database in the example. The receiving end, I am using an iPhone installed with an instant messaging app, setup with Google Talk account to receive push notifications. I chose IM+ free version. It’s up to you which app you prefer but I recommend at least one with push notifications on new messages so it served as some sort of “notification”. What you need? Latest version of PHP XMPP library. 2 x Google talk account, one for receiving and one for sending the notifications. PHP enabled server. Optional – MySql database for storing of notification queues. You may choose to read the notifications queue from a file, memory pipe and etc…. PHP server settings Important! Ensure PHP OpenSSL module is enabled. I encountered infinite loop when connecting to GTalk server while OpenSSL module is disabled. Click here if you need help. Firstly, install the PHP XMPP package in “/libraries/xmpp”. Create the model which reads the notification queue for new message to send. //M_notification_queue.php file ... function getUnsendNotifications(){ //Load the database $this->load->database(); //Build the sql query //Retrieve unsent messages only $this->db->where('status','unsent'); //Retrieve from table notification $query = $this->db->get('notification'); //return the active record handler return $query; } ... Implement the sending controller //notification.php controller file private $gtalk_username = "sender@gmail.com"; //fake email private $gtalk_password = "password"; //fake password private $check_notification_interval = 10; //frequency to check for unsent message ... function sendNotificationCronJob(){ //load required models $this->load->model('M_notification'); //initialize the xmpp library $conn = new... read more

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... read more

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... read more

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;... read more

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: 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)); ?>... read more

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... read more