Archive for the 'Web' Category

Disable Selection With CSS

Jun 27 2011 Published by under Web

* {
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-o-user-select: none;
	user-select: none;
}

Comments Off

Short Ternary Operator

May 18 2011 Published by under PHP

With the release of PHP 5.3.0 there is an option to shorten the ternary operator.

So, if we start with variable $foo, which is true ($foo = true), instead of writing:

$bar = $foo ? true : false; // $bar = true

you can easily shorten it like this

$bar = $foo ?: false // $bar = true

The bad side of using a short ternary operator is that most of PHP IDEs are marking it as syntax error (well, at least for now)

Comments Off

PHP method chaining

Jul 31 2010 Published by under PHP

Method chaining is a technique that adds class methods one on top the other. It’s commonly used in jQuery and models in PHP frameworks.

The whole “magic” behind object chaining is made with a single line – “return $this” which is added in each method you wish to chain.

So, in order to get this behavior in PHP:

$human = new Human;
$human->setName('Haris')
      ->setAge(30)
      ->setSex('male')
      ->display();

you have to define class(es) like this:

class Human {

    protected $_name;
    protected $_age;
    protected $_sex;

    public function setName($name) {
        $this->_name = $name;
        return $this;
    }

    public function setAge($age) {
        $this->_age = $age;
        return $this;
    }

    public function setSex($sex) {
        $this->_sex = $sex;
        return $this;
    }

    public function display() {
        printf('Hi, I\'m %s. I\'m %d years old, and I\'m %s!',
            $this->_name,
            $this->_age,
            $this->_sex
        );
    }

}

class Man extends Human {
    protected $_sex = 'male';
}

class Woman extends Human {
    protected $_sex = 'female';
}

Now compare the methods called with method chaining and the old fashioned way

$woman = new Woman;
$woman->setName('Jane')
      ->setAge(27)
      ->display();

$woman = new Woman;
$woman->setName('Jane');
$woman->setAge(27);
$woman->display();

// Hi, I'm Jane. I'm 27 years old, and I'm female!

Comments Off

OpenDNS

Mar 28 2009 Published by under Ubuntu,Web

opendns

I needed some good content filtering service (anti-pr0n, anti-phishing, anti-….). So, I googled for “open” (which I do a lot lately when looking for software or services :)), and “dns”. Was taken to OpenDNS and I fell in love with it immediately.

Their site looks really good and displays the most crucial information on a single page, which i like a lot.

I signed up for the account, and this is how I set up my computer to use OpenDNS.

Add OpenDNS nameservers to resolv.conf file

$sudo nano /etc/resolv.conf
# OpenDNS
nameserver 208.67.222.222
nameserver 208.67.220.220

My ISP DNS names are always forced to resolv.conf when I connect to Internet, so I had to protect this file from being overwritten by network manager (i – immutable)

$sudo chattr +i /etc/resolv.conf

Install ddclient (updates IP addresses at dynamic DNS services) on Ubuntu

$sudo apt-get install ddclient

Configure ddclient

$sudo dpkg-reconfigure ddclient
Dynamic DNS service provider: other
Dynamic DNS server: updates.opendns.com
Dynamic DNS update protocol: dyndns2
DynDNS fully qualified domain names: $label //this is actually a network "label" set on the OpenDNS "networks" page
Username for dynamic DNS service: $username
Password for dynamic DNS service: $password
Interface used for dynamic DNS service: ppp0 // I use network manager to connect to DLS provider, which creates a ppp0 interface
Run ddclient on PPP connect: yes
Run ddclient as daemon: yes
ddclient update interval: 300

Check that ddclient daemon is up and running

ps aux | grep ddclient

Make sure to add “ssl” option to ddclient config file, otherwise OpenDNS will not be updated

sudo nano /etc/ddclient.conf
# /etc/ddclient.conf
ssl=yes

Restart ddclient

$sudo /etc/init.d/ddclient restart

This setup will check if my IP address has been changed every 5 minutes. If changed, my account on OpenDNS will be automatically updated.

I switched both home and office computers to OpenDNS, and so far all I have are words of praise.

Comments Off

Regular expression “e” modifier

Mar 16 2009 Published by under PHP,Web

I was reading regular expressions docs to find a solution for a problem that I had. And like all the greatest discoveries (that are found accidentally), I found this neath little “escaping” trick.

The big idea behind it is to grab the matching elements in patterns section and create an escape sequence with PHP code in the replacement part.

Example


$txt = 'This is a test';
print preg_replace("/(\>)([\w\s]+)(\<)/e", "'\\1'.strtoupper('\\2').'\\3'", $txt); 

Pattern explanation

 (\>) //matches the greater than sign of the opening div element and ads this element to a first subpattern
([\w\s]+) //second subpattern will grab all word characters and any white space
(\<) //third subpattern matches the less than sign of the closing div tag

Replacement explanation

‘\\1′ is used to access a first subpattern, ‘\\2′ for accessing second etc… These subpatterns are naturally available as strings. Since PHP uses a dot (.) as a string concatenation operator, we add dots before and after PHP code, in this example strtoupper(‘\\2′)

To a “strtoupper” function (which converts all characters to uppercase) we pass a second subpattern, and we close the replacement part with a third.

So, what we are doing here is actually matching any text between the div tags and convert that text to uppercase letters.

The end result of this small example would be THIS IS A TEST

Comments Off

The 140 character webapp challenge!

Feb 22 2009 Published by under PHP,Web

I read about this interesting challenge at http://f055.net/article/the-140-character-webapp-challenge/. The idea was to create a web application in 140 characters or less (one twitter message).

I was instantly hooked.

Most of the guys submitted variations of a micro twitter, so naturally, I decided to go for something else. I figured out it would be great to write an application that parses RSS/RDF feeds :)

So, after more then an hour I ended up writing this 135 characters web app:

<?php foreach(file($_GET[u])as$l)if(preg_match('/<(title|link)>([^<]+)/',$l,$m))echo$m[1]=='link'?" <a href=$m[2]>link</a><hr>":$m[2]?>

You can check out live demo here.

I am really glad that I managed to format the output the way I wanted, to have article links and separators, and not only to dump data to browser.

Coding this little app was pure fun. It really keeps you going when you try so squeeze so much stuff into only <= 140 chars. Any programmer out there should try to write one of these small apps.

Comments Off

Performance Optimization WordPress Plugins by W3 EDGE