HOWTO get the real host name of your server in PHP

In PHP, to get the name of the web server, you can print $_SERVER[“HTTP_HOST”]. However, if you use round-robin DNS or a load balancer, this will give you the name Apache thinks your machine is.

What if you want the real hostname — what you’d get from hostname or uname?
print gethostbyaddr("127.0.0.1");

That’s all. It sure beats the pants off:
print trim(`hostname`);

keywords: php hostname local computer server HTTP_HOST

JavaScript: how to unescape HTML entities

I was searching the web for JavaScript HTML entity unescaping code. I found lots of really bad ideas. Sound familiar? Some were quite complicated.

It looked like the scriptaculous guys had the right idea. However, all I could find was a cached page with no code. A quick peek inside prototype set me on the right track.

String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild);
    return result;
}

And the code in action:

var hello = "Hello José";
alert(hello.unescapeHtml());

How to create a self-signed SSL certificate for Dovecot on Debian

Here is how you create a self-signed SSL certificate for the Dovecot IMAP/POP server on Debian Linux:

openssl req -new -x509 -days 1000 -nodes -out "/etc/ssl/certs/dovecot.pem" -keyout "/etc/ssl/private/dovecot.pem"

The default is 365 days, but I upped it to 1000, so I don’t have to do this so often.

Dovecot does this on installation via /var/lib/dpkg/info/dovecot-common.postinst; you can force this script to re-run by issuing these two commands:
find /etc/ssl -name dovecot.* -exec rm {} \;
dpkg-reconfigure dovecot-common