Tag Archive for 'php'

My Ubuntu Intrepid + LAMP

Ubuntu logo

Ubuntu logo

Until quite recently I was working on Windows XP, when I had this urge to shift to Ubuntu once again. I got hold of wubi for Intrepid and my system was running the gnome in another 5 minutes.

Now while I was in windows, I had a wamp server installed. The first thing I set myself to do was to install the LAMP, and reconfigure the system to include the wamp www directory as the default directory instead of the /var/www. Here is what I did -

  • Installed SSH client (so that I could connect remotely to my Ubuntu) : sudo apt-get install ssh
  • Install the mysql-server : sudo apt-get install mysql-server
  • Install the apache2 webserver : sudo apt-get install apache2
  • Install php5 : sudo apt-get install php5 libapache2-mod-php5
  • Install php5-mysql support : sudo apt-get install php5-mysql
  • Restart the apache2 webserver : sudo /etc/init.d/apache2 restart
  • Install phpmyadmin : sudo apt-get install phpmyadmin

And it was done. The LAMP installed. Not to configure my dafault directory

  • I navigated to/etc/apache2/sites-available
  • Made a backup of the default file : sudo cp default default.bk
  • Edited the default file : sudo vi default
  • I changed the default document root to the www directory created by my wamp server

Yay, my site was up and running. When I navigate to http://localhost I could see the wamp index.html. I clicked on phpmyadmin and it worked. The phpmyadmin that I installed on Ubuntu was presented. It’s almost like having installed wamp server on Ubuntu.

But I would like to make a few ponts over here :

  • I do not yet know how to add more than one directory to the default directory list. Will do that.
Reblog this post [with Zemanta]

Popularity: 1% [?]

php – Sessions

After a long long time I was back to coding in php. And by long time, I mean atleast 24 months ! Believe me, it wasn’t easy. More so because of the higher standard I have set for myself. My code this time shall be much neater and easier to read than it ever was.

The past four years of my life has been spent coding. And when you code so long, you modify your habits to get tuned to the proper way of coding. As it is said, a person spends more time reading a code than writing it. So this time I am taking special care of writing simpler and smaller functions so as to make the code more readable. Also, since I have been reading a lot of OOP these days, I am intent on providing an abstraction layer for the user, so that at the top most level coding should appear like writing a few english sentences rather than some cryptic mysqli_select_db things.

This is the first time I am trying to code in php following the OOP structure. Also, though this might seem strange, it is the first time I am using session variables in my design! Below is a trivia I had not known earlier, and spent 2 hours trying to understand what was wrong in my seemingly proper and correct code.

A session created with session_start will only be available to pages within the directory tree of the page that first created it.

i.e. If the page that first creates the session is /dir1/dir2/index.php and the user then goes to any page above dir2 (e.g. /dir1/index.php), session_start will create a new session rather than use the existing one.

Also I found the following class to be useful.

< ?php
class Session {
public static function Init() {
self::Commence();
}

public static function Set($fld, $val) {
self::Commence();
$_SESSION[$fld] = $val;
}

public static function un_set($fld) {
self::Commence();
unset($_SESSION[$fld]);
}

public static function Destroy() {
self::Commence();
unset($_SESSION);
session_destroy();
}

public static function Get($fld) {
self::Commence();
return $_SESSION[$fld];
}

public static function is_set($fld) {
self::Commence();
return isset($_SESSION[$fld]);
}

private function Commence() {
if(!isset($_SESSION['ready'])) {
session_start();
$_SESSION['ready'] = TRUE;
}
}
}
?>
Reblog this post [with Zemanta]

Popularity: 2% [?]

php5 OOP – Function Overloading

PHP

PHP

Starting php4, object oriented concepts have been made available to the programmer. Though not well developed, they paved the way for a more complete implementation of the OOP basics in the php5 version. However, one thing that remains lacking is the ability ot overload the functions.

Overloading of functions is quite an useful feature. In the simplest form, it gives you the freedom to declare a class with more than one constructors.

Not to get disheartened though, we have a simple enough way to work around this short coming. In php, when a call is made to a function that does not exist, then the control searches for the __call() function. We make use of this to carve out the over loading feature that we need.

Below, I am writing the code for a Register class which has two variables – name and pass. The constructor is being overloaded here.

class Register {
private $name;
private $pass;

public function Register() {
$num = func_num_args();
$args = func_get_args();

$funcName = 'Register'.$num;
$this->__call($funcName, $args);
}

private function Register0() {
$this->name = '';
$this->pass = '';
}

private function Register2($name, $pass) {
$this->name = $name;
$this->pass = $pass;
}

public function __call($funcName, $args) {
$this->call_user_func_array(array($this, $funcName), $args);
}

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

public function setPass($pass) {
$this->pass = $pass;
}
} // Register

// regA is using the Register2() constructor
$regA = new Register('Anu', '@#$#@$@$');

// regB is using the Register0() contructor
// and subsequently setting the values of name, pass
$regB = new Register();
$regB->setName('Anu');
$regB->setPass('@#$#@$@$');
Reblog this post [with Zemanta]

Popularity: 2% [?]