Monthly Archive for August, 2008

McCain on Tires

PHP Inheritance

interface hasArea {
	public function getArea();
}

class Rectangle implements hasArea {
	static $w;
	static $l;
	public function __construct($w, $l) {
		$this->w = $w;
		$this->l = $l;
	}

	public function getArea() {
		return $this->w * $this->l;
	}
}

class Square extends Rectangle {
	public function setWidth($w) {
		parent::$w = $w;
	}

	public function setLength($l) {
		parent::$l = $l;
	}

	public function getArea() {
		return parent::$w * parent::$l;
	}

	public function getArea2() {
		return $this->w * $this->l;
	}
}

$rect = new Rectangle(10, 5);
print $rect->getArea();
print "";
$sqr = new Square(5, 5);
print $sqr->getArea();
print "";
print $sqr->getArea2();
print "";
$sqr->setWidth(10);
$sqr->setLength(10);
print $sqr->getArea();

/*
	Output:
	50
	0
	25
	100
*/

PHP supports inheritance and polymorphism. The above is one tiny example when I played around with it. If you see the output, you realize that setWidth and setLength would set the parent variable, in which is NOT set even I’ve instantiated Rectangle using $w and $l parameter on its constructor.

Matthew 17:20

20He replied, “Because you have so little faith. I tell you the truth, if you have faith as small as a mustard seed, you can say to this mountain, ‘Move from here to there’ and it will move. Nothing will be impossible for you.”

Isaiah 6:8

8 Then I heard the voice of the Lord saying, “Whom shall I send? And who will go for us?”
And I said, “Here am I. Send me!”

Part of Life

Trouble is part of your life, and if you don’t share it, you don’t give the person who loves you enough chance to love you enough.

~ Dinah Shore ~

Shared

Shared joy is a double joy; shared sorrow is half a sorrow.

~ Swedish Proverb ~

Spam from Coca-Cola..?

Click on the image to see the bigger version

I checked my email and I see there’s one emal from CocaCola.com, and the email stated that I’m a winner of something. I’ve already suspicious about this type of email.. But alright, I never done anything special with Coca-Cole and/or sodas in general, and in fact, I don’t like to drink sodas..!!

I opened the email, and I read it through, it says that they do some drawing from UK yada yada.. bla bla.. I have the confirmation number, bla bla, and as usual, they’re asking me to send my information to some email, and the funny things is that they want me to send these information to some free email provider accounts.

Anyways, that’s not interesting, so what’s more interesting? How could they got the cocacola.com email account they’re spams? So I take a closer look to the email, and it says that it’s from promotion@cocacola.com , ourcoke03@gmail.com, this is the smart thing that they did, did you just see a space there? Yeah, there’s a space that they put right after the .com!!! Isn’t that pretty smart? :)

Be aware to this type of email, it’s obvious that it’s a spam!

PHP Paging

config.php

$username = 'username';
$password = 'password';
$database = 'dbname';
$table = 'dbtable';
$host = 'localhost';

$rowsPerPage = 10;
$pageNum = 1;

opendb.php

include('config.php');
$link = mysql_connect($host, $username, $password);
if (!$link) {
	die('Could not connect: ' . mysql_error());
}
mysql_select_db($database);

closedb.php

include('config.php');
mysql_close($link);

index.php

include('opendb.php');

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page'])) {
    $pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage ;

// selecting $rowPerPage, starting from $offset
$query = "SELECT * FROM " . $table . " LIMIT " . $offset . " , " . $rowsPerPage;
$result = mysql_query($query) or die ('Error, query failed');
$totalRows = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {
   echo $row['fname'] . '';
}

// how many rows we have in database
$query   = "SELECT COUNT(*) AS numRows FROM " . $table;
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numRows = $row['numRows'];

// how many pages we have when using paging?
$maxPage = ceil($numRows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav  = '';

for($page = 1; $page <= $maxPage; $page++) {
   if ($page == $pageNum) {
      $nav .= " " . $page . " "; // no need to create a link to current page
   } else {
      $nav .= " " . $page . " ";
   }
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1) {
   $page  = $pageNum - 1;
   $prev  = " [Prev] ";

   $first = " [First Page] ";
} else {
   $prev  = ''; // we're on page one, don't print previous link
   $first = ''; // nor the first page link
}

if ($pageNum < $maxPage) {
   $page = $pageNum + 1;
   $next = " [Next] ";

   $last = " [Last Page] ";
} else {
   $next = ''; // we're on the last page, don't print next link
   $last = ''; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

include('closedb.php');

I think the code is pretty straight forward other than understanding on how does the $offset works. Also, I just knew that in PHP you could do something like:

$var = 1;
print("some $var");

That means you could combine both variable and string. PHP takes care to differentiate between the real string and variable. So, you don’t have to do all of the complicated stuff by adding dots as you see in my code. However, I always like to make it explicit.. :).. As you see in this code that I’m still using dots all around, well, it does get more complicated when you’re dealing with more variables, but that way my IDE would be able to differentiate between the real variable and string. That means it adds my visibility when looking at the codes.

Download the code

Colossians 2:3

3in whom are hidden all the treasures of wisdom and knowledge.

How to Secure a Folder using Basic HTTP Authentication?

You should read on if you are not going to secure data that is very sensitive such as Social Security Number. I’m not security expert, and I’m not responsible for any loss or damages.

You have control over a web server, and you want to secure a folder within the web server using basic HTTP authentication. What is that? If you’ve seen a pop up dialog box when you’re about to enter a web page on a site like:

OR

They’re both a basic HTTP authentication.

There are many ways to implement the basic HTTP authentication. In PHP for example, you could easily implement this in few lines of codes:

if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "

Hello {$_SERVER['PHP_AUTH_USER']}.

";
    echo "

You entered {$_SERVER['PHP_AUTH_PW']} as your password.

";
}

However, note that in PHP, the authentication process applies to those who’s trying to access the PHP page that contains the above code. But what if you want to secure the whole folder that contains many files, and list them when the user is authenticated, otherwise, then they can’t see and/or download the files within the folder.

Note that the web server that I’m using in this tutorial is Apache. I’m using XAMPP, but it doesn’t matter whether you’re using XAMPP or not, because we still need to use the htpasswd that comes with the Apache. In XAMPP, you’ll find this in xampp/apache/bin

Once you create a folder in the web server, then make sure that you know the full path from the root  to the folder that you want to secure, and then create .htaccess file that contains the following lines:

AuthUserFile “/www/www/full/path/to/your/folder/.htpasswd”
AuthName “Message to go on user’s login screen”
AuthType Basic
Allow from all
Require valid-user
Options +Indexes

After that, you have to create .htpasswd file, which would be created by the htpasswd that is located in the bin folder of the Apache.

Usage:
htpasswd [-cmdpsD] passwordfile username
htpasswd -b[cmdpsD] passwordfile username password

htpasswd -n[mdps] username
htpasswd -nb[mdps] username password
-c  Create a new file.
-n  Don’t update file; display results on stdout.
-m  Force MD5 encryption of the password (default).
-d  Force CRYPT encryption of the password.
-p  Do not encrypt the password (plaintext).
-s  Force SHA encryption of the password.
-b  Use the password from the command line rather than prompting for it.
-D  Delete the specified user.
On Windows, NetWare and TPF systems the ‘-m’ flag is used by default.
On all other systems, the ‘-p’ flag will probably not work.

Now, type htpasswd -c username username, Apache will then ask you for a password to be encrypted. If you don’t use SHA encryption, the default encryption would be the MD5. There will be username file that is created in the bin directory, and when you open it, you’ll see something like:

username:$apr1$L1f68a53$8Hq14C3aFwqZCAosvxtW60

Now just rename the file to be .htpasswd, then put in the folder where .htaccess is:

/www/www/full/path/to/your/folder/.htpasswd
/www/www/full/path/to/your/folder/.htaccess

Done, then just open up browser, and go to http://localhost/folder or try download a file within the folder http://localhost/folder/file.pdf.

Note that each line of the .htpasswd file represents one username and password, and you could always add multiple user accesses by appending additional username followed by the colon “:” and then the encrypted password to the .htpasswd.