If blocks and readable code
0Writing readable code is as important as writing high performance working code. One of the things which can easily affect the readability of the code we write is improper structuring of if/else blocks. Consider the following example method (more…)
Finding the network interface used by a .NET socket
0Preventing SQL injection specially for PHP developers
0SQL injection happens when user provided input through forms or query string is directly used in an SQL query without any sanitation done to it. For example a badly coded login script would allow an attacker to login without knowing the username/password or login with full rights of admin users.
Though this can happen to any web page developed in any language, this issue seems to be affected more PHP pages than .NET applications. Perhaps simplicity of PHP programming and inexperience coders together with the fact that PHP is being used more and more are reasons for this. In any case, keeping following poins in mind while developing PHP sites should help avoid mistakes which can be prevented easily.
(more…)MySql replace pitfalls
0MySql has a replace into syntax using which we can insert a record and if the record already exists, replace it with new value. Cool! now we don’t have to check the existance of a record and decide whether to do insert or update.
Sending email using a gmail account in C#
0Sending emails using your gmail account is very simple in C#/VB.NET using .NET framework 2.0+. The below code snippet shows how.
Create datetime range with custom interval
0In this post I will show a method using which we can create datetime ranges. This method is not specific to the datetime, instead it can be used with any type including int, long etc. There is a new method in .net framework 3.5 using which we can easyly create integer ranges. For example.
foreach(int i in Enumerable.Range(1,10))Console.Write(i);
However, in this post I will show how we can create integer ranges and datetime ranges with custom intervals.
(more…)Displaying time in relative format
0
Many websites specially forum sites display time relative to the current time as "3 Hours 25 Minutes ago", "30 Seconds ago" etc. In this post I will show a method using which we can convert datetime into descriptive relative time string.
The method takes 2 parameters 1. the time to process, 2. the time to which the output will be relative. Usually it is DateTime.UtcNow. There is also an overload which only takes the time to process where the second parameter defaults to DateTime.UtcNow. Let us see some sample output of this function.
(more…)