Friday, April 13, 2007

What is a Domain Name?

A domain name is basically the name of your website such as google.com or cbc.ca or computercops.biz. These are all domain names which are registered by official registrars with various domain name authorities. These authorities make up part of an international database or “domain name system” overseen by the Internet Corporation for Assigned Names and Numbers (ICANN).

The domain name that you select basically consists of two parts:

  • Second level domain - The actual name of your website, e.g., ironspider
  • Top level domain (TLD) - The 2 or 3 letter suffix at the end of the domain name, e.g. .com, .org, .ca, .uk, etcetera.

Choosing the Right Domain Name

Second level domain names may not be repeated within each top-level domain but may be repeated across top level domains. For example, as of this writing, there exists ironspider.ca, ironspider.net and ironspider.com. But within the .ca top level domain, there is only one ironspider (yours truly). If you find that the domain name you want is already taken then try changing the top-level domain before changing the second level domain.

As for which second level domain name to choose (the actual name of your website), you could try to work in some keywords related to your niche to help gain higher rankings in seach engines. However be advised that there is much disagreement among the experts as to whether this is actually works.

For example, you may want to choose the domain name blue-widgets.com if your company sells ‘blue widgets’. Personally, I think this has little effect on your rankings and you can prove this by just doing a search in Google for keywords related to your niche and see how many of the top ten results have those keywords in their domain names.

Also I’m a firm believer in using your domain name for branding purposes. I think it’s much better to create highly memorable domain name than a blasé domain name that simply contains keywords related to your business.

For example, which name are you more likely to remember?

purplemonkeyplaytime.com OR best-toys-for-tots.com

Right?

To register a domain name, it is simply a matter of performing a domain name ‘look up’ to find a name that isn’t already taken (since all domain names must be unique), then finding an official registrar, laying the cash on the barrel and presto, you are the owner of an internet domain name.

Choosing the Right Registrar

The cheapest domain name registration (from a reputable provider) is currently hovering around $8 - $9 per year.

Here’s some info on what to look for in a domain name registrar:

  • ICANN-accredited domain name registrar — You can change web hosts from one month to the next but when it comes to your domain name, you’re pretty much stuck with it for an entire year. This is something you don’t want to mess around with and you definitely want to avoid petty hassles such as who actually owns your domain name once it’s registered. Using a properly accredited registrar will ensure that you are doing business with a reputable company that has met a required set of standards as set out by the Internet Corporation for Assigned Names and Numbers (ICANN) which is the official body that oversees domain name registration.
  • Easy domain name management interface — You won’t be accessing this very often so you’ll want to have your domain name managed by a registrar that provides an easy-to-use domain name management interface that speaks plain English. Some of the cheapest domain name registrars won’t put much thought into this which will ultimately have you wasting a lot of time trying to plow past a bunch of technobabble like ‘CNAME’ and ‘MX Record’ when all you want to do is simply update your contact information. Typically you’ll be accessing your domain name management screens to renew your domain name, update contact information or change your nameservers (switch web hosts).
  • 24/7 technical support — This is self-explanatory. Like web hosting, domain name registration is now a highly competitive business. If your registrar can’t offer you 24/7 tech support then they simply aren’t in the game.

Using Subdomains

Third level domain names, a.k.a., host names or subdomains, are often used by free web hosts to create multiple free hosting accounts to allow users to have a little piece of the web without actually having to register a ‘real’ (second level) domain name. Subdomains usually look like this:

username.freewebhostname.com

If you sign up with a free web host and you don’t have your own domain name then they will assign you a subdomain name of this sort. As mentioned earlier, in this situation, it will be painfully obvious that you’re using a free web hosting service. If you’re serious about your website (and you want others to thinks so too) then you should definitely spend the few bucks a year it takes to have your own domain name.

Wildcard DNS and Sub Domains

What follows is what I consider to be best practice for my personal sites and a guide for those who wish to do the same. Months ago I dropped the www. prefix from my domain in part because I think it’s redundant and also because I wanted to experiment with how Google treated valid HTTP redirect codes. The experiment has been a great success. Google seems to fully respect 301 Permanent Redirects and the change has taken my previously split PageRank has been combined and now I am at 7. There are other factors that have contributed to this, of course, and people still continue to link to my site and posts with a www. (or worse) in front of it, but overall it just feels so much cleaner to have one URI for one resource, all the time. I’m sure that’s the wrong way to say that, but the feeling is there nonetheless.

Now for the meat. What’s a good way to do this? Let’s look at our goals:

  • No links should break.
  • Visitors should be redirected using a permanent redirect, HTTP code 301, meaning that the address bar should update and intelligent user agents may change a stored URI
  • It should be transparent to the user.
  • It should also work for mistyped “sub domains” such as ww. or wwww. (I still get hits from Carrie’s bad link)

So we need a little magic in DNS and in our web server. In my case these are Bind and Apache. I am writing about this because at some point the code I put in to catch any subdomain stopped working and while I reimplemented it I decided to write about what I was doing. This method also works with virtual hosts on shared IPs where my previous method did not.

In Bind you need to set up a wildcard entry to catch anything that a misguided user or bad typist might enter in front of your domain name. Just like when searching or using regular expressions you use an asterisk (or splat) to match any number of any characters the same thing applies in Bind. So at the end of my zone DB file (/var/named/photomatt.net.db) I added the following line:

*.photomatt.net. 14400 IN A 64.246.62.114

Note the period after my domain. The IP is my shared IP address. That’s all you need, now restart bind. (For me /etc/init.d/named restart.)

Now you need to set up Apache to respond to requests on any hostname under photomatt.net. Before I just used the convinence of having a dedicated IP for this site and having the redirect VirtualHost entry occur first in my httpd.conf file. That works, but I have a better solution now. So we want to tell Apache to respond to any request on any subdomain (that does not already have an existing subdomain entry) and redirect it to photomatt.net. Here’s what I have:


DocumentRoot /home/photomat/public_html
BytesLog domlogs/photomatt.net-bytes_log
User photomat
Group photomat
ServerAlias *.photomatt.net
ServerName www.photomatt.net
CustomLog domlogs/photomatt.net combined
RedirectMatch 301 (.*) http://photomatt.net$1

The two magic lines are the ServerAlias directive which is self explanitory and the RedirectMatch line which redirects all requests to photomatt.net in a permanent manner.

There is a catch though. The redirecting VirtualHost entry must come after any valid subdomain VirtualHost entries you may have, for example I have one for cvs.photomatt.net and I had to move that entry up in the httpd.conf because Apache just moves down that file and uses the first one it comes to that matches, so the wildcard should be last.

Simple Optimization for PHP and MySQL

Here is a list of a few very simple tips for optimizing your php/mysql applications. Keep these in mind while developing.
MySQL

  • MySQL is interpreted from right to left so you should put the most significant limiters as far to the right as possible.
  • Only select fields you need, instead of selecting * (everything).
  • Don’t put things that changes very rarely in the database, instead put it in a global array in some include file.
  • Use indexes on the columns in the WHERE clause and on the columns you want to ORDER BY.
  • Indexes are great if you search the table alot, but it slows down insertion.
  • Use the EXPLAIN command to analyze your indexes.
  • If you only want one line as a result from the database you should always use LIMIT 1. This way mysql stops searching when it finds the first line instead of continuing through the whole database, only to find that there weren’t any more lines that matched the query.
  • If you use $line = mysql_fetch_array($result) you’ll get two ways of accessing the columns, $line[0] and $line[’columnname’]. If you only use the $line[’columnname’] you should use $line = mysql_fetch_assoc($result) instead, then there will not be any $line[int index] array.
  • Sometimes mysql_free_result() end up wasting more memory than it saves. Check the difference with memory_get_usage().
  • Don’t ask the database for the same stuff over and over again, save the result.
  • Use NOT NULL as default value as much as you can, it speeds up execution and saves one bit.
  • Use datatypes that fits your data, not too large. For example, INT can hold values up to 4294967295 unsigned, which is often unnecessarily big. Use MEDIUMINT or SMALLINT where applicable.
  • Make use of the default values, only insert values that differs from the default values to speed up the insertion.

PHP:

  • Many code blocks might slow down the interpretation a little bit.
      ...
    ...
    ...
    ?>

    is faster than



  • Don’t concatenate when you don’t need to.
    "SELECT id FROM tabell WHERE id = $_SESSION[id] LIMIT 1"

    is faster than:

    "SELECT id FROM tabell WHERE id = ".$_SESSION['id']." LIMIT 1"
  • Surrounding your string by ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string.
  • The previous item makes it all boil down to
    'SELECT id FROM tabell WHERE id =
    '.$_SESSION['id'].' LIMIT 1'

    as the fastest way of concatenating querys.

  • When echoing strings it’s faster to separate them by comma instead of dot.
    echo "echoing ",$variable," something";

    Note: This only works with echo, which is a function that can take several strings as arguments.

  • echo is faster than print.
  • Set the maxvalue for your for-loops before and not in the loop.
    $maxvalue = 100/10;
    for($i=0; $i<$maxvalue; $i++){
    // Some code
    }

    is faster than:

    for($i=0; $i<100/10; $i++){
    // Some code
    }

    because the value is calculated once instead of ten times.

  • Unset your variables to free memory, especially large arrays.

If possible it’s of course always better to generate static html pages every time something is updated or as often as an update might be relevant instead of querying the database every time.

Further reading:

10 things if you didn’t know about PHP

PHP is simultaneously the most infuriating and joyful languages I’ve ever worked with. I say “infuriating” primarily because the function names are so inconsistant. Despite the fact that I use it almost everyday, I still have to think to myself “Is it str_pos or strpos? str_split or strsplit?” On the other hand, occasionally I’ll stumble across a gem that perfectly solves the problem at hand with a single line of code.

Here’s a short list of cool features that might have slipped under your radar as well:

  1. Use ip2long() and long2ip() to store IP addresses as integers instead of strings in a database. This will reduce the storage space by almost a factor of four (15 bytes for char(15) vs. 4 bytes for the integer), make it easier to calculate whether a certain address falls within a range, and speed-up searches and sorts (sometimes by quite a bit).
  2. Partially validate email addresses by checking that the domain name exists with checkdnsrr(). This built-in function checks to ensure that a specified domain name resolves to an IP address. A simple user-defined function that builds on checkdnsrr() to partially valid email addresses can be found in the user comments section in the PHP docs. This is handy for catching those occasional folks who think their email address is ‘joeuser@wwwphp.net’ instead of ‘joeuser@php.net’.
  3. If you’re using PHP 5 with MySQL 4.1 or above, consider ditching the mysql_* functions for the improved mysqli_* functions. One nice feature is that you can use prepared statements, which may speed up queries if you maintain a database-intensive website. Some benchmarks.
  4. Learn to love the ternary operator.
  5. If you get the feeling that you might be reinventing the wheel during a project, check PEAR before you write another line. PEAR is a great resource that many PHP developers are aware of, yet many more are not. It’s an online repository containing over 400 reusable snippets that can be dropped right into your PHP application. Unless your project is trully unique, you ought to be able to find a PEAR package that saves at least a little time. (Also see PECL)
  6. Automatically print a nicely formatted copy of a page’s source code with highlight_file().This function is handy for when you need to ask for some assistance with a script in a messageboard, IRC, etc. Obviously, some care must be taken not to accidently show your source when it contains DB connection information, passwords, etc.
  7. Prevent potentially sensitive error messages from being shown to users with the error_reporting(0) function. Ideally error reporting should be completely disabled on a production server from within php.ini. However if you’re on a shared webhost and you aren’t given your own php.ini, then your best bet is to add error_reporting(0); as the first line in each of your scripts (or use it with require_once().) This will prevent potentially sensitive SQL queries and path names from being displayed if things go awry.
  8. Use gzcompress() and gzuncompress() to transparently compress/decompress large strings before storing them in a database. These built-in functions use the gzip algorithm and can compress plaintext up to 90%. I use these functions almost everytime I read/write to a BLOB field within PHP. The only exception is when I need full text indexing capabilities.
  9. Return multiple values from a function with “by reference” parameters. Like the ternary operator, most PHP developers who come from a more formalized programming background already know this one. However, those who’s background is more HTML than Pascal, probably have wondered at one time “how do I get multiple values back from a function I wrote, even though I can only use one return value?” The answer is that you precede a variable with “&” and use it “by reference” instead of “by value”.
  10. Fully understand “magic quotes” and the dangers of SQL injection. I’m hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it’s absolutely critical to understand. If you’ve never heard the term before, spend the entire rest of the day googling and reading.

Cheat Sheet Roundup - Over 30 Cheatsheets for developers

Lets face it, unless you have a photographic memory, no developer can remember all the different functions, options, tags, etc. that exist. Documentation can be cumbersome at times, thats why I like cheat sheets. They are quick references that feature the most commonly forgotten things on a specific topic. You can print them out and hang them on your wall, or just keep them handy in your bookmarks for quick reference.

I have rounded up over 30 cheatsheets that developers might find useful.

Web Development Cheat Sheets

Databases / SQL Cheat Sheets

Language Cheat Sheets

Version Control Cheat Sheets

Other

Commercially Printed CheatSheets

Am I missing any good ones? Feel free to post any others in the comments.

Updates:

Related Entries

PNG OVERLAY : USEFUL CSS TECHNIQUE FOR MASKING IMAGE ON YOUR WEBSITE

When designing a webpage webdesigner have some idea, something to beautify an image shape so it’s not common and classic style with just box style. But other reason that must be a consideration is if that image is part of dynamic content, that mean every time post a news/dynamic content we must editing the image shape, it’s very wasting time. In this case, I have some problem too :) , so I browse on the net to find solution, and lucky me I found some interesting solution for this :) , thanks to Nathan Smith for the “PNG Overlay” solution.

Related article about PNG Overlay and Transparency :

PNG overlay using one single Image element
How to create simplemasks

Anton Subagja