UseStrict Consulting

Professional IT Solutions & Training

Inexpensive and reliable web hosting

Back in 2002 I decided to host my own sites and those of my clients and friends. To do so, I hired a very expensive internet connection, moved into the living room so that my bedroom could serve as a Data Center, and put together some old hardware to serve as a makeshift web server. Setting up the software was relatively easy, but it wasn’t long until I had to give up my idea due to faulty hardware and excessive maintenance needs. I had to move back to a hosting company, my dream shattered due to limited server access (no root, limited ssh, and so on).

Last year I had a second chance at that dream when I found out about RimuHosting.com. I hired a VPS for my sites (this one included) and now I have everything I had back in 2002 (including my bedroom back!) and no worries regarding hardware failure. I am extremely satisfied with my services, especially since I even got a free memory upgrade. And all that for a fraction of the cost of my 2002 server and cheaper than my old Godaddy bill when hosting more than 2 domains. If you don’t want to share your resources with other people, they also offer Dedicated Server plans.

If you’re looking for reliable and inexpensive web hosting and know how to set up your own software, I strongly suggest RimuHosting.com. If you don’t know how to set up your own software, then I still strongly suggest RimuHosting.com, and leave the System Administration stuff to UseStrict Consulting! Contact us for more information on how we can assist you with your SA needs!

Why Invest In Your Website?

It’s not that rare to hear someone claim that the look of your website isn’t the focal point of having one. While this is true – content is crucial, as even the worst-looking websites in the world can thrive if the content is good enough.

The catch? Most of them can’t.

Your website is your identity, and if it looks horrendous when it loads, the likelihood is that someone will make a snap judgement about your content, your services and your willingness to put effort into your online presence. This sounds harsh, but realistically, companies should have been investing in high-quality websites ten years ago. In the second decade into the new millennium, there’s no excuse.

So what makes a good website, and how do you invest in one? For starters, you’ll find that user experiences are going to improve when everything is neat, clean, well-organised and easy to use. Whether you’re running Amazon or o2.co.uk, your users need to be able to do whatever they want (within the bounds of the site’s purpose and their user privileges). If they can’t, they’ll simply go somewhere they can.

Investing in your website is simple – hire good web developers, make sure your servers are high-quality and never prone to more than .1% downtime, and hire some staff to run the site, if you’re a large enough company. A good team of staff will be comprised of a web administrator, someone on content, someone promoting the site (you can combine these last two roles, if you wish), and someone working on helping users who may have any queries.

Lastly, think of your website as the modern equivalent of a shop front. If it looks like something years out of date, without a door handle and with all the products lacking price tags, then you’re going to run into trouble. So keep everything clean and appealing, and it’s going to be a lot easier to generate a client/consumer base that appreciates and is loyal to your dedication to your and their online presence.

Launching CronBlocks.com – Dynamic content for your website

It’s been a while since my last post, but it’s been for a good cause. This week I launched CronBlocks.com, an app that allows you to insert content on a web-page according to a given schedule or visitor’s geo-location.

Here’s how it works:

  1. Go to www.cronblocks.com and click on Plans to sign up – there’s a free plan if you just want to see how it works. Check out the premium plans once you’re satisfied it’s what you need.
  2. After signing up, and activating your account (you’ll get an activation email), click on the Channels link. The Channels allow you to control where on your page you want the content to appear.
  3. Configure your channel to run by server (EDT) or client time – I recommend client time since you’ll probably be receiving people from across several different time zones.
  4. You can choose to copy the JS snippet at this time, or leave it for later – it’s the JS snippet that you’ll use on your website to show the content
  5. Once you have the channel set up, you can create your scheduled blocks for that channel. Blocks are the actual content that’ll show on your page. You can create as many schedules for a block as you want/need – either by months/days or by day-of-week. And you get to set the time you want them to show, too.
  6. If you have multiple blocks to show that overlap a given time slot, you can set them to have different priorities (weight). Higher weights trump lower ones. Think of it as stack, with heavier items being served first. Overlapping items with same weight will be picked randomly.

And that’s all there is to it. Your site will show whatever block is set up for that given timestamp.

What uses do you have for this? Well, for starters, you can segment your ads by time – sell your “prime time” for a higher price than other times (for those of you who don’t use ad networks). You can also include some JS in your block to make it refresh every 15 seconds or so. Basically, your imagination is the limit.

I’ll create new posts in the future with more examples. In the meantime, here’s a test block for you.

Cheers,
Vinny

Safari shows a blank page

I just ran into something odd and found all sorts of complex solutions (none of which worked for me), so I decided to post this case.

I’m creating a new version of an interface at work and tested with FireFox during development. When I opened it in Safari, all I got was the background image. No content at all. I Googled and several people reported having the same problem and said that clearing the cache would do the trick. It did not.

In the end, the issue was a missing ‘–>’ inside a script tag.

<script type="text/javascript"> <!-- // open a comment for older browsers - is this actually required still??
/* lots of JS here */

// There should be a close comment (-->)  here!
</script>

Safari, not finding the closing comment tag, simply considered all of the HTML to be commented out.

I hope this helps!

 

Book Suggestions:

 

Introduction to Ajax

In this article, I provide an explanation of Ajax with a historical introduction. If you are eager to start seeing the code, please scroll down.

 

What’s Ajax?

 

Ajax stands for Asynchronous Javascript And XML. It’s a way to call back-end scripts asynchronously – that is, without impacting user experience/flow. Basically, you don’t even see the cursor become an hourglass or whatever other “waiting/processing” icon your system uses. It’s not a language, but a technique. As for back-end scripts, you can use whatever you feel more comfortable with: PHP, Perl, Java, JSP, Shell, C, etc. The way to choose which technology to use as back-end is not outside the scope of this article.

 

Historical approach for calling back-end apps

 

Throughout web-development history, the very first way used to achieve back-end processing followed by front-end display was to create a form and set the back-end script as the action to that form. Upon submission, the form fields would be sent to the back-end script as a series of special environment variables, which would then be handled by the programmed logic. The back-end output would be displayed on the screen (either the same page/frame or a different page/frame, depending on the target attribute of the form).

The catch 22 of this approach is that if you want to present another form after processing, that form must be produced by the back-end script, which leads to maintenance mayhem – to add or remove a field, you have to do so in both the original HTML and in the HTML of the back-end script.

Continue reading

PHP+PDO+MySQL: How I’m doing it and a question…

I recently decided to rewrite an application that I created for an English school where I used to work, and this time I want to do it right. I chose to write it in PHP with Object Orientation in mind, and use PDO+MySQL for the DAO portion.

The reason for using PDO is that it’s the closest thing to the DBI that I found for PHP – and the reason for PHP is that it’s just plain simpler than using pure Perl, and I don’t have the time to learn Catalyst or some other Perl Web framework.

Anyway, here’s what I’m doing for the PDO portion.

First I created a Db class which is just a DB connection wrapper.

<?php
class Db{

	public $dbh  = null;

	public function Db($db_type,$db_host,$db_user,
			         $db_pass,$db_base,$tbl_prefix){

		if ($db_type == 'mysql') { // in case I want to add oracle in the future

			$mysql_DSN = "mysql:host=".$db_host.";dbname=".$this->db_base;

			try {
				$this->dbh = new PDO($mysql_DSN, $db_user, $db_pass,
							   array(PDO::ATTR_PERSISTENT => true, PDO::ERRMODE_WARNING => true,
							         PDO::ATTR_ERRMODE => true));

			}
			catch(PDOException $e){
				$rc[msg] = $e->getMessage();
				die($rc[msg] . "  " . __LINE__);
			}

			return $this->dbh;

		}

	}

}
?>

Secondly, I created the DAO classes which extend the DB. They are responsible for prepare‘ing and execute‘ing the queries, and returning the data. Here’s an example one (I stripped some bells and whistles in order to not confuse anyone):

<?php
class StudentsDao extends Db {

	public $sth = array();

	// Constructor
	public function StudentsDao($db_type=NULL,$db_host=NULL,$db_user=NULL,
				  	       $db_pass=NULL,$db_base=NULL,$tbl_prefix=NULL) { // These params are to be passed to Db class

		// Connect to DB
		$this->Db($db_type,$db_host,$db_user,
			      $db_pass,$db_base,$tbl_prefix);

		// Prepares queries
		$this->sth = $this->prepare($tbl_prefix); // Look at prepare() further down

	} // end of function StudentsDao

	// Function that prepares queries
	public function prepare() {

		$sth = null; // will hold our temporary array

		// get all students
		$string = sprintf('select * from %s.%sstudents where id = ?',DB_BASE,TBL_PREFIX);	// I have these constants defined in the main page
		$sth['getStudentById'] = $this->dbh->prepare($string);

		// update Student by Id
		$string = sprintf('update %s.%sstudents
				   set field1 = ?, field2 = ?, field3 = ?
				   where id = ?',DB_BASE,TBL_PREFIX);	// Note that this table has 3 control fields (id, sys_creation_date, and sys_update_date)  prior to field1, field2, and field3 - I don't want them to be touched

		$sth['updStudentById'] = $this->dbh->prepare($string);

		return $sth;

	} // end of prepare()

	// Here we have the handlers for the queries prepared above.

	// Fetches all students
	public function getStudentById($id) {

		$s = $this->sth['getStudents'];
		$s->execute(array($id)) || die(print_r($s->errorInfo,1));
		$result = $s->fetchAll(PDO::FETCH_ASSOC);

		if (sizeof($result) == 0) {
			// no data found
			$out[err_cd] = 1;
		}
		else {
			// data was found
			$out[err_cd] = 0;
			$out[data] = $result;
		}

		return $out;

	}

	// Updates student by Id - this is a tricky one
	public function updateStudentById($data,$id){ // $data can have data regarding one or more fields

		// Populate current fields
		$fields = $this->getStudentById($id); // get current values

		// Remove unwanted fields
		$fields = array_slice($fields[data][0],3); // remove the 3 initial control fields

		// Replace with new fields
		foreach($data as $k=>$v){
			$fields[strtolower($k)] = $v;
		}
		$fields['id'] = $id;

		$s = $this->sth['updStudentById'];
		$s->execute(array_values($fields)) || die(print_r($s->errorInfo,1));
		$count = $s->rowCount();

		if ($count == 1) {
			$out[err_cd] = 0;
		}
		else {
			$out[err_cd] = -1;
		}
		$out[err_msg] = $this->status[$out[err_cd]];
		return $out;

	}
?>

In the example above, you will probably frown on the fact that I’m calling getStudentById() at the beginning of updateStudentById(). It’s OK – I frown on it, too. Let me explain why I did that:

I wanted to be able to pass the function an associative array containing only the fields I want to update – be it only field1, or field1 and field2 and so on. But I wanted to do that against my already prepared query, and not touching any additional fields. In my application, students table has a BUNCH of fields. In other words, update any given field or set of fields using the single prepared statement, without having to set ALL the fields in $data

My original idea was to basically, to emulate something like this:

update students set field1=field1, field2='some new value ', field3=field3 where id = 1

That would set field1 to whatever value field1 already had.

Here’s the question part:
Unfortunately, I couldn’t figure out how to do that using the PDO (how to bind a field name instead of a value against the ?-mark). If you know how to do that, do leave a comment explaining how! :D

The technique shown in the example will basically pull all values from the record and populate them in the $fields array. The array_slice() is used to remove the control fields that I have in my table, and the rest gets compared against the $data array and if any matching fields are found in $data, those fields get their values assigned over the existing $fields values.

Ajax: Form fields into URL query string

Here’s a quickie for you Ajax coders who need a function to parse your form fields into a URL query string. It handles input fields (text, hidden, whatever), radio and checkbox elements, and single/multiple select menus. Just make sure that all your form fields have IDs. Multiple choice SELECTs get grouped by pipes (|). Update:Modified multiple-choice SELECTs to work like real GETs.

function build_post_string(frm) {

	var poststr;
	var poststr_array = [];

	if (!frm.id) {
		// assume it's a string. get the form object
		frm = document.getElementById(frm);
	}

	for (i=0;i<frm.elements.length;i++){

		var elem = frm.elements[i];

		if (!elem.id) {
			// skip any fields that don't have IDs
			continue;
		}

		if (elem.type == 'radio' || elem.type == 'checkbox') {
			// only grab radio buttons and checkboxes that are checked
			if (!elem.checked) {
				continue;
			}
		}

		// get select values
		if (elem.nodeName.match(/SELECT/i) && elem.multiple) {
			//var sel = elem;
			var sel_array = new Array();

			for (var o=0;o<elem.options.length;o++) {

				if (elem.options[o].selected) {
					sel_array[sel_array.length] = elem.id+"="+elem.options[o].value;
				}
			}

			var sel_str = sel_array.join('&');

			// build key/value pairs for SELECTs
			poststr_array[poststr_array.length] = sel_str;
		}
		else if (elem.nodeName.match(/SELECT/i)) {
			poststr_array[poststr_array.length] = elem.id+'='+elem.options[elem.selectedIndex].value;
		}
		else {
			// build key/value pairs for everything else
			poststr_array[poststr_array.length] = elem.id+"="+elem.value;
		}

	}

	// build poststr
	poststr = poststr_array.join("&");
	return poststr;

}

 

Book Suggestions:

 

Perl: Installing DBD::Oracle + Oracle Instant Client on Ubuntu 9.04

A couple of weeks ago I finally got a new laptop at work – which meant of course that I had to reinstall everything. Although we use Windows XP, there’s one app that I have to run through Linux. The solution was Sun’s VirtualBox running Ubuntu 9.04. When I tried to run the app, I realized that I still needed to install DBI and DBD::Oracle – which brings me to this article. Nothing better than a reinstall to generate article material :)

DBD::Oracle is usually a pain to install if you haven’t already done it a gazillion times. After that, it’s just an annoying itch. In this article I’ll cover installing DBD::Oracle using Oracle Instant Client, Ubuntu 9.04, and Perl 5.10. Since we’re using the Instant Client, you’ll need some Oracle DB you can connect to in order to do the testing. You can choose to skip testing altogether, but you might be into a surprise later. Continue reading

Didn’t find what you came for?

The cool thing about blogging software is that it shows the blogger all sorts of interesting information, such as the number of visitors, or the search string that led them to the blog.

I see lots of interesting search strings, but I keep wondering if the users actually found what they were looking for. That said, please be kind and leave a message telling me if you found what you wanted, and if you didn’t, please say what it was and I’ll try to cover that in my posts.

You can leave your message either as a comment here, or send me an email through the contact form.

Thanks!!