Warning: Undefined variable $html in /customers/9/6/e/chocofant.dk/httpd.www/2010/php/MessageController.php on line 98

Obfuscate connection strings

Image not found

Sometimes you don't want your database connection strings in clear text in your config file.
Here is a simple way to obfuscate the connection strings. Remember this is not real encryption it just makes it a little hard to read the user ID and the password.

The obfuscation is based on base64 encoding.
private string EncryptConnectionString(string clearText)
{
Byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(clearText);
string encryptedConnectionString = Convert.ToBase64String(b);
return encryptedConnectionString;
}

Method to get the connection string back to clear text
private string DecryptConnectionString(string cipherText)
{
Byte[] b = Convert.FromBase64String(cipherText);
string decryptedConnectionString = System.Text.ASCIIEncoding.ASCII.GetString(b);
return decryptedConnectionString;
}


Refactor smelly if/switch statements with the strategy pattern

Normally the strategy pattern is used to select algorithms run-time.
Here I will show you how to use it for web site page navigation.

If you use some kind of page/front controller to handle requests it is possible you will have a big if/switch statement which determines what action to execute.
It could look something like this.
switch($_GET["page_id"])
{
case "1":
if($_GET["item_id"] == "1")
{
echo $ItemController->GetItemList();
}
elseif($_GET["item_id"] == "2")
{
echo $ItemController->GetItemTwoList();
}
elseif($_GET["item_id"] == "3")
{
$bool = $Check->Something();
$ItemController->Save($bool);
}
case "2":
if($_GET["item_id"] == "1")
{
echo $ItemController->SetItem();
}
elseif($_GET["item_id"] == "2")
{
echo $ItemController->GetConfig();
}
...

This will eventually become very hard to maintain. And without good documentation almost impossible to modify for another programmer.
Let's try to refactor this with the strategy pattern. First we need an interface with one function signature.
interface IStrategy
{
public function execute();
}

And we have concrete implementations of our strategies, these are the ones that actually do the job of read/writing data to and from the server.
I show just two here, it's good to use more informative class names. The class definitions can be put in seperate files or one common. If we later on want to add new page content to the site we just add a new class.
class Page1Item1Strategy implements IStrategy
{
public function execute()
{
echo $ItemController->GetItemList();
}
}

class Page2Item1Strategy implements IStrategy
{
public function execute()
{
echo $ItemController->SetItem();
}
}

And finally we have the consumer function.
The needed class is composed from the parameters in the URL string. Instead of the big if/switch statement we get a small consumer function and a more maintainable structure of our code.
public function GetContent()
{
// IStrategy interface
$iStrategy;

$page_id = $_GET['page_id'];
$item_id = $_GET['item_id'];
if(!empty($page_id) && !empty($item_id))
{
// Build concrete class name.
$strategyClass = "Page". $page_id . "Item" . $item_id . "Strategy";

// Really simple (nasty) PHP way to instantiate the concrete class
$iStrategy = new $strategyClass;

// Call specific method
$iStrategy->execute();
}
}

The urls would look like this.
http://host.com?page_id=1&item_id=1

Hmm.. I guess what I said in my previous about not showing any patterns examples didn't last long.


A few words about design patterns

Sometimes when I have implemented a solution to a problem, I just think this code is a mess but I can't figure out how to make the code cleaner. I sit staring at the code thinking "This is smelly".
If you find yourself in that situation it's time you look up design patterns on the internet or in a book.

Design patterns are like template solutions to common design problems. They also provide a uniform way to speak about design solutions between developers.
They variate in complexity from simple to hard and helps you solving wide variety of design problems. You might already use some of the patterns without knowing their names.
But like with all good thing over-use is misuse. They should make your code simpler not more complex.

Instead of showing examples here I will refer to dofactory.com, they have a very nice introduction to the subject with examples in C#.


Send a mail from your C# program

Add a reference to your program.
using System.Net.Mail;

This is basically how it is done. Beware: there could be some relay issues at the mailserver.
SmtpClient client = new SmtpClient("smtpServer");

MailAddress from = new MailAddress("sender@localhost", "senderName");
MailAddress to = new MailAddress("recipient@remotehost");
MailAddress cc = new MailAddress("cc@remotehost");

MailMessage message = new MailMessage(from, to);
message.CC.Add(cc);

message.Subject = "A new mail";
message.Body = "Message text";
message.BodyEncoding = Encoding.UTF8;

client.Send(message);


Don't exaggerate the use of AJAX

Initially I decided to use Ajax requests for all data loading on this layout. Big mistake.
The site navigation became a nightmare with limited possibility for redirecting to previous pages and javascripts that used the DOM failed everytime the DOM changed.
In the end I decided to refactor the site to use normal GET requests instead, which makes the navigation logic much easier.
Lesson learned: Be careful with the ajax requests, they change the site behavior a lot.



<- Previous page | Next page ->