Denny Labs

De Web Monster

Add File for Additional Swap Space

If you don’t have any additional disks, you can create a file somewhere on your filesystem, and use that file for swap space. The following dd command example creates a swap file with the name “swapfile” under /root directory with a size of 1024MB (1GB). # dd if=/dev/zero of=/root/swapfile bs=1M count=1024 1024+0 records in 1024+0 records out Change the permission of the swap file, only allow root access. # chmod 600 /root/swapfile Using mkswap command make this file as a swap file. # mkswap /root/swapfile Setting up swapspace version 1, size = 1048572 KiB no label, UUID=########-####-####-####-############ Enable the newly created swapfile. # swapon /root/swapfile Using your favourite text editor, append the following line to the /etc/fstab file, to make this swap file available as a swap area even after a reboot. # nano /etc/fstab /root/swapfile swap swap defaults 0 0 Verify the newly created swap area. # swapon -s Filename Type Size Used Priority /dev/xvdb partition 524284 0 -1 /root/swapfile file 1048572 0 -2 To verify whether the system takes all the swap space mentioned in the /etc/fstab without rebooting, you can do the following, which will disable and enable all the swap partition mentioned in the /etc/fstab #swapoff -a #swapon -a #swapon -s Filename Type Size Used Priority /dev/xvdb partition 524284 0 -1 /root/swapfile file 1048572 0... read more

Reset MySQL root password (ubuntu)

Oops! I’ve forgotten MySQL root password. 1)Stop MySQL instance (if it is running) sudo service mysql stop 2)Startup MySQL instance manually with option –skip-grant-tables and –skip-networking sudo mysqld_safe --skip-grant-tables --skip-networking 3)While MySQL is running from step 2, login to MySQL mysql --user=root mysql 4)Execute the following SQL commands to update root password Update user set Password=PASSWORD('new-password') where user='root'; flush privileges; exit; 5)Remember to stop MySQL instance executed from step 2 after you complete step 4. Lastly run MySQL normally. sudo service mysql... read more

php date time supporting date values before year 1970

“I need to store birthdate of my customers where it can fall before year 1970.” Php function date(), strtotime() uses UNIX timestamp which supports year 1970 to 2038. To work with date values beyond that, we need to use php datetime class which is available on after php version 5.2.0 (datetime manual). Create a date before 1970: $dt = new DateTime(); $dt->setDate(1945, 8, 8); // August 8 1945, end of world war 2 If you need to store time as well: //public DateTime DateTime::setTime ( int $hour , int $minute [, int $second = 0 ] ) //seconds argument is optional $dt->setTime(8,0,45); // 8:00:45 am To print the date: echo $dt->format('Y-m-d H:i:s'); //outputs //1945-08-08 08:00:45 For list of date format to use, refer to http://php.net/manual/en/function.date.php. Hope it helps.... read more

Apache web server – password protect your website / directory

You are developing a new site and wish to keep it secret till your new site is ready for launch? You got personal photos, confidential documents, project codes uploaded to your web server for future reference or download and you want to give access only to certain people? One of the solutions is to password protect your directory where the restricted stuffs were stored. “How to password protect the directory or entire website?” Requirements: – Hosted on apache server – Apache module mod_access is enabled. Step 1 Create password file “.htpasswd”. In the folder / site root you wish to password protect, create a .htpasswd file. The password file stores your valid users’ username and password. Step 2 Use online passwd generator to create user’s username and password entry for htpasswd. http://www.htaccesstools.com/htpasswd-generator/ Copy and paste the entries into “.htpasswd”. Example of an .htpasswd file denny:$apr1$7APPNnCz$M./pcs91u296uBP0VAD4J1 Step 3 In the same folder, create/edit .htaccess to include the following codes. AuthUserFile /absolute/path/to/.htpasswd AuthType Basic AuthName "Prompt Title" Require valid-user Note: Relative path to .htpasswd will not work. Final Step Launch your browser, navigate to the password protected folder, you will receive a authentication challenge if nothing goes wrong. =D That’s... read more

.net csharp store password as md5 hash into mssql database

Storing plain-text password into database is completely insecure and not recommended. Several methods to store encrypted password such as using LDAP server or hashing the password before saving to database. Below is an example how to hash the password using MD5 before writing to database and authenticate user. //In order to use MD5 class, we need to import System.Security.Cryptography; using System.Security.Cryptography; //MD5 Hashstring static method public static string MD5HashString(string input) { MD5 md5Hash = MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } //Create user login method public bool createUserAccount(string strUsername, string strPassword) { //validation //check if username is valid if(!UserEntity.isValidUsername(strUsername)){ //username invalid return false; } //check if password is valid if(!UserEntity.isValidPassword(strPassword)){ //password entered fail password policy validation return false; } //all validation passed //md5 hash user password string hashUserPassword = common.CommonFunction.MD5HashString(strPassword); //initialize data access layer object UserDA objUserDa = new UserDA(); //write to database bool insertResult = objUserDa.createNewUser(strUsername, hashUserPassword); return insertResult; } //Authenticate user public bool userLogin(string strUsername, string strPassword) { //check whether user login valid UserDA objUserDa = new UserDA(); //retrieve user id by username int userId = objUserDa.getUseridByUsername(strUsername); if(userId == -1) { //user records not found return false; } //convert user password to md5 string md5password = common.CommonFunction.MD5HashString(strPassword); //retrieve user... read more

java string explode and implode

Some nice java function to explode string into array or implode array into string as equivalent to PHP implode and explode function. Explode Convert string to array of tokens. Function #1 – Explode using org.apache.commons.lang.StringUtils import org.apache.commons.lang3.StringUtils; ... //explode function #1 //using org.apache.commons.lang package //api and jar download available @ http://commons.apache.org/ public static String[] explodeStringUsingStringUtils(String stringToExplode,String separator){ return StringUtils.splitPreserveAllTokens(stringToExplode, separator); } ... //usage String stringToExplode = "apple,pear,banana,,durian"; String separator = ","; String[] arrExploded = explodeStringUsingStringUtils(stringToExplode,separator); /* * String[] arrExploded will contain * 0 - "apple" * 1 - "pear" * 2 - "banana" * 3 - "" * 4 - "durian" */ Function #2 – Explode using core java.lang //explode function #2 //using core java lang public static String[] explodeStringUsingCoreJava(String stringToExplode,String separator){ return stringToExplode.split(separator); } ... //usage String stringToExplode = "apple,pear,banana,,durian"; String separator = ","; String[] arrExploded = explodeStringUsingCoreJava(stringToExplode,separator); /* * String[] arrExploded will contain * 0 - "apple" * 1 - "pear" * 2 - "banana" * 3 - "" * 4 - "durian" */ Implode Concatenate/merge array of string into a single continuous string. Function #1 – Implode using org.apache.commons.lang.StringUtils import org.apache.commons.lang3.StringUtils; ... //implode function #1 //using org.apache.commons.lang package //api and jar download available @ http://commons.apache.org/ public static String implodeArrayUsingStringUtils(String[] arrayToImplode,String separator){ return StringUtils.join(arrayToImplode, separator); } ... //usage String[] arrExploded = {"Denny","de","Web","Monster"}; String implodeString = implodeArrayUsingStringUtils(arrExploded,"+"); /* * String implodeString will contain * "Denny+de+Web+Monster" */ Function #2 – Implode using for loop //implode function #2 //using for loop public static String implodeArrayUsingForLoop(String[] arrayToImplode,String separator){ if (arrayToImplode.length == 0) { //empty array return empty string return ""; } if(arrayToImplode.length < 2){ //only 1 item return arrayToImplode[0]; } StringBuffer... read more

Prevent / Solve java.util.ConcurrentModificationException

Your program throws ConcurrentModificationException while running and how can you prevent / solve it It happens when your program removing an item in an ArrayList, Collection or List while looping (iterating) over it. Example: for (int i = 0; i < yourArrayList.size(); i++) { if (yourArrayList.get(i).compareToIgnoreCase("apple") == 0) { yourArrayList.remove(i); } } //Exception in thread "main" java.util.ConcurrentModificationException // at java.util.AbstractList$Itr.checkForComodification(Unknown Source) // at java.util.AbstractList$Itr.next(Unknown Source) To prevent this, there are several solutions depending on you program logic. Solution 1 – Using the java.util.Iterator: import java.util.Iterator; ... Iterator<String> iterator = yourArrayList.iterator(); while (iterator.hasNext()) { String string = (String) iterator.next(); if (string.compareToIgnoreCase("apple") == 0) { iterator.remove(); } } Solution 2 – Using an temporary ArrayList to store items for removal: //Initialize an temporary array list with similar type ArrayList<String> itemToRemove = new ArrayList<String>(); //Search for items matching string "apple" for (int i = 0; i < yourArrayList.size(); i++) { if (yourArrayList.get(i).compareToIgnoreCase("apple") == 0) { itemToRemove.add(yourArrayList.get(i)); } } //Remove items stored in itemToRemove ArrayList yourArrayList.removeAll(itemToRemove); Hope it... read more

Integrated Development Environment (IDE) for PHP / HTML / CSS / JavaScript

1. Aptana Studio 3 by Appcelerator, Inc Platform independent as the IDE runs on Java JRE and supported programming languages are HTML5, CSS3, JavaScript, Ruby, Rails, PHP and Python. Aptana Studio 3 is currently release under “GNU General Public License“.   2. Adobe Dreamweaver CS5.5 by Adobe Systems Favourite by large web development firms. Runs on mac osx and windows platform. Supported programming languages are HTML5, CSS3, JavaScript, PHP, ColdFusion Markup Language (CFML) , VBScript (for ASP), C# and Visual Basic (for ASP.NET) and JSP. Commercial license required to install and use.   3. Eclipse by Eclipse Foundation Open source community, whose projects are focused on building an open development platform comprised of extensible frameworks, tools and runtimes for building, deploying and managing software across the lifecycle. Platform independent as the IDE runs on Java JRE. Download the Eclipse PDT package for PHP support. Currently release under “Eclipse Public License (EPL)“.   4. Zend Studio 8 by Zend Technologies Ltd According to their FAQ, Zend Studio is a commercial IDE with rich a feature set including remote debugging, Zend Server integration, Zend Framework support, and more. Supported programming languages are HTML, CSS, JavaScript and PHP. If your presentation uses JavaScript libraries such as Dojo, jQuery, Prototype and Ext JS, then you may want to consider Zend Studio as it comes with content assist for the libraries. Zend Studio 8 runs on linux, mac osx and windows. Commercial license required for use after 30 days... read more

Add Google +1 Button To Your Site

Something similar to facebook like button. However +1 is also shown in google search result. Far more effective than other social plugins as it greatly extends your site exposure. “How do I implement the +1 button?” If you are using wordpress, download the google +1 social share button plugin for wordpress. Otherwise you may grab the code from google. There are also instructions on where to paste the 2 lines of code in your web... read more