A Beginner’s Guide to Object Oriented Programming (OOP) in PHP5 – Classes, Objects and Methods

January 27th, 2009

***Guest Blogger***
The Personal Home Page (PHP) programming language provides a lightweight framework and foundation for Web application development. It runs on Apache (HTTP) Web server software amonst others, embedded into Hypertext Markup Language (HTML), passed through a Hypertext Preprocessor. As free, open source software – it has low implementation, maintenance and debugging costs. Its latest iteration called PHP5, has incorporated even more powerful object-oriented programming functionalities.

While PHP3 has rendered server-side scripts and PHP4 has established backward compatibility, PHP5 has improved object-oriented programming (OOP) as a cross-platform language. OOP bundles data and code into “objects”. PHP5 works better with object handles; passing or assigning by reference is not required.
Read the rest of this entry »

DNS based Load Balancing

January 22nd, 2009

There are two main options for DNS based load balancing. The first and most simple is the round robin option. We can use this for ‘A (alias) records’ and ‘MX (Mail-eXchanger) records’.

We can specify a priority for MX records. If we specify the same priority for multiple MX records, the querying client will toss a coin and ‘randomly’ decide which to use. The same applies to A records. This should provide with a reasonable split between your various records however provides no mechanism for server loads or using any kind of intelligence to route queries.

Another option is to return a record based on intelligence. Assume we are trying to balance load between web servers. The two popular methods we can use are to return a record based on knowledge of the load of the web servers, or alternatively return a record based on originating IP (location) of the requesting client.

This is all well and good however there are a number of considerations, specifically that DNS was not intended to be operated in this way.

  1. You can set your records expire time to as low as you like, it will still be cached in circumstances by the browser and/or the resolver. This method will not account for ‘downed’ or ‘overloaded’ servers, they will still receive traffic.
  2. Due to caching, should your browser or resolver hold on to the record, it will blindly access the same IP next time the host name is requested, without requerying the DNS server and ignorant of the changed network conditions.

How to reset forgotton MySQL root password

January 20th, 2009

As long as you have root access to your debian machine, you can do this as follows:

/etc/init.d/mysql stop #stop MySQL
/usr/bin/mysqld_safe –skip-grant-tables & #start MySQL with --skip-grant-tables
/usr/bin/mysql -u root mysql #connect to mysql as root, straight into the 'mysql' database. No password is required
UPDATE user SET password=PASSWORD(’newrootpassword’) WHERE user=’root’; #Do replace 'newrootpassword' with something that you'll remember.
FLUSH PRIVILEGES;
q #to quit
/etc/init.d/mysql stop #stop MySQL
/etc/init.d/mysql start #start MySQL

You can now test with mysql -u root -p you’ll be prompted for your password and your ‘newrootpassword’ should now work!

How to recover your Linux root password

January 20th, 2009

Here are two quick methods amongst many..

1. If you have a boot CD, use it!

2. Edit your grub boot line. Here’s an example:

Hit ‘e’ to edit, and go to edit the ‘kernel’ line. You’ll see something like:

 

/boot/vmlinuz-2.6.18-4-686 root=/dev/sda1

 

Edit this line and set init=/bin/sh i.e.

/boot/vmlinuz-2.6.18-4-686 root=/dev/sda1 init=/bin/sh

 

Then hit ‘b’ to boot. You should be dropped straight to a root prompt, whereby you can issue ‘passwd’ as before.

How to remove the tray icon from VNC (RealVNC) with XN Resource Editor

January 18th, 2009

I’ve been asked a few times if I knew a way to remove the tray icon from VNC, or simply prevent it’s behavior of switching from white to black when an admin was connected. This has a number of legitimate uses in terms of teaches wanting to monitor their pupils or employers wanting to monitor their employees especially remote workers without the user being aware of the exact time he is being monitored.

RealVNC is my preferred VNC Server/Client and this does not support the desired behavior – I’m not sure that any other VNC Server/Cliet does either. Here are the necessary steps to edit RealVNC’s Server executable (winvnc4.exe):
Read the rest of this entry »

Linux IP Address Configuration Static or DHCP

January 18th, 2009

It’s simple really..

You can define a static IP as follows:

ifconfig <interface> <ipaddress> netmask <mask> broadcast <broadcast>
e.g. ifconfig eth0 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255

You can then define a route out to the internet:

route add default gw <router>
Which has the same effect as: route add -net 0.0.0.0/0 <router>
i.e. route add default gw 192.168.1.1

Or if you’d like to acquire an address via a local DHCP server:

dhclient <interface>
e.g. dhclient eth0

That’s all there is to it! Now.. these settings won’t stick on reboot, you’ll have to refer to your distro’s startup files. On debian you want /etc/network/interfaces

A sample stanza for the above configuration:

auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1

All done!

A rudimentary PHP POP3 example

January 15th, 2009

In my last post I described the basics of PHP’s fsockopen. This script is far from perfect and only contains minimal error checking however it illustrates the basics of fsockopen in action communicating with a POP3 server. More information on the POP3 protocol can be found here

Read the rest of this entry »

PHP – fsockopen, TCP and UDP

January 15th, 2009

PHP comes preloaded with a good socket handling function set. Using fsockopen to make HTTP connections however is in most cases obsoleted as the php-curl and set of curl functions covers more HTTP related stuff than you could ever need! fsockopen is available in both PHP4 and PHP5

Regardless, to cover the most basic usage of fsockopen, to establish a TCP connection to “mailserver” on port 110 (POP3):
Read the rest of this entry »

PHP – Ternary Operator

January 15th, 2009

I’m going to try and focus some time on building out the PHP section of the site now – here’s a useful technique, in shortening and tidying various cases of if/then/else.
Read the rest of this entry »