Archive for the ‘Programming’ Category

Perl and Apache on Mac OS X

Tuesday, July 22nd, 2008

The other day while I was modifying a client’s site I saw that his contact form was in PERL. Recently I just got a Mac Book Pro. I’ve set up XAMPP and Eclipse on it to do my PHP programming, but I didn’t count on getting into any PERL. As a challenge I thought it would be fun to do some PERL programming.

I first installed XAMPP. Right out of the box I was able to do PHP programming and access MySQL databases which saved me some time configuring and trouble shooting issues. Or so I thought. I’ve written PERL scripts before, but never done anything that would run on a web server. When it comes to writing pages in PHP, just write the code and it works. That’s because the headers are sent for you. As a test I put the following into my script:

#!/usr/bin/perl

print(”Hello!”);

Expecting it to work. However I would get an error message saying Error 500. I looked at the log file /Applications/xampp and saw that the script was ‘exiting prematurely’ which did not make any sense at all. I could run the PERL script just fine on the command line.

After doing some digging around, about an hours worth I finally found what I was looking for. I needed to print the headers. In order to get my PERL script to work with Apache I needed to add the following:

use strict;
use CGI;

my $cgi = new CGI;

so now the script looks like:

#!/usr/bin/perl

use strict;
use CGI;

my $cgi = new CGI;

print $cgi->header . $cgi->start_html . “hi” . $cgi->end_html;

The key to this script is the $cgi->header function call. With out it, you will get an internal server error. There are other functions associate with the CGI class. The start_html and end_html are the beginning and closing HTML tags.

SimpleTest

Thursday, April 10th, 2008

A while back I was working on a site, and as I was writing the back end and making sure that it works properly I thought to myself “If .NET has nunit, and Java has junit, does PHP have phpunit, or something similar?” After some digging I found the answer. The answer I found was yes! In addition to finding unit frame works for PHP I also found some for other languages like ada, haskell and so on:aunit - for Ada
hunit - for Haskell
ant-junit - Apache Ant’s optional tasks depending on junit
dbunit - DBUnit is a JUnit extension targeted for database-driven pr
ojects.
tagunit - for testing custom JSP tags
xmlunit - for XML
and so on. If you want a complete list, on the command prompt, if you’re using Gentoo, just type:

(more…)

Eclipse, PHP, and XDebug

Thursday, April 10th, 2008

In the past I’ve written how to use Eclipse with phpdbg and ZendDebugger. The phpdbg was short lived as it was replaced with xdebug. According to Zend’s website, they say that there are no serious limitations of xdebug. However, there may be some limitations when working with other Zend modules.

This is not going to be a problem seeing as all I want to do use it to debug web pages. This will work on either Windows, Linux, and possibly MacOS X.

Since posting about Eclipse and Zend, it has gotten much easier to get Eclipse and configure it for PHP. Go to Eclipse’s homepage. On the front page there is a link to download the IDE for other languages; click on that and get the one for PHP.

(more…)

Java Doc

Thursday, April 3rd, 2008

Documentation is key when programming. There is the documentation in the code itself such as what the heck you’re trying to do there. There is also the documentation that describes the functions and objects and how to use them. Luckily there is a tool that helps the programmer create nice HTML documentation based on the comments within the code. This knocks out two birds with one stone. Javadoc is the tool to use for this task. There is also PHPdoc as well for, you guessed it PHP.

For this entry we’ll only be concerned about Javadoc.

(more…)

PHP User Authentication

Thursday, January 31st, 2008

The Tables

First we’re going to need a table to hold the user information. I’ve done a very basic table, you can expand this table to include the user’s name, e-mail address, street address, etc.

CREATE TABLE TblUsers(
user_id VARCHAR(18) NOT NULL PRIMARY KEY,
user_password VARCHAR(255) NOT NULL,
question TEXT NOT NULL,
q_answer TEXT NOT NULL,
CONSTRAINT user_id_fk FOREIGN KEY(user_id)
REFERENCES TblPermissions(user_id));

For the sake that I can, in the event that you want to limit users to perform certain tasks you can create another table for permissions:

CREATE TABLE TblPermissions(
user_id VARCHAR(18) NOT NULL PRIMARY KEY,
perm_one INT NOT NULL DEFAULT 0 CHECK( perm_one < 1 && perm_one > 0 ),
perm_two INT NOT NULL DEFAULT 0 CHECK( perm_two < 1 && perm_two > 0 )
);

I kept this table simple. Obviously, if you wanted to you could definitely add more permissions depending on how big your application or site is. I will explain this in a later entry. We’re going to need a place for the user to log in. In order for the user to login and remain logged in, we’re going to have to start a session:

session_start();
Now we must undo what magic quotes does. If magic quotes is enabled, this will undo everything that it did.

if( get_magic_quotes_gpc()) {
$_REQUEST = array_map(’stripslashes’, $_REQUEST);
$_COOKIE = array_map(’stripslashes’, $_COOKIE);
}

We’re going to need at least three files for the user to login, do some work, then log out. Again, you can definitely have more pages depending on what you’re trying to acomplish.

The login page is a very basic page where the person is going to put in their credentials. Behind the scenes the page will submit the data to itself and verify that the user is a valid user. After it determines that the person trying to gain access is valid, it will foreword the user to the second page, which is called main.php in this example.

When storing passwords in a database, it’s important to encrypt them. NEVER store them in plain text!! The query that we’d use in this case to create a user would be:

INSERT INTO TblUsers(user_id, user_password, question, q_answer) VALUES(’test’, PASSWORD(’password’), ‘Where was I born?’, ‘New York City’);

Now, if you wrote a page to add users, the query would look like:

$query = “INSERT INTO TblUsers(user_id, user_password, question, q_answer) VALUES(’” . mysql_real_escape_string($_REQUEST[”UserID”] . “‘, ‘” . mysql_real_escape_string($_REQUEST[”UserPWD”] . “‘, ‘” . mysql_real_escape_string($_REQUEST[”UserQuestion”] . “‘, ‘” . mysql_real_escape_string($_REQUEST[”UserAnswer”] . “‘)”;

The mysql_real_escape_string function is to ensure that the user isn’t putting any evil data into the field that would result in data being exposed.

That’s it! If you want, you can download the files that were used in this entry:

Unit Testing PHP

Saturday, December 29th, 2007

A while back I was working on a site, and as I was writing the back end and making sure that it works properly I thought to myself “If .NET has nunit, and Java has junit, does PHP have phpunit, or something similar?” After some digging I found the answer. The answer I found was yes! In addition to finding unit frame works for PHP I also found some for other languages like ada, haskell and so on:

aunit - for Ada
hunit - for Haskell
ant-junit - Apache Ant’s optional tasks depending on junit
dbunit - DBUnit is a JUnit extension targeted for database-driven pr
ojects.
tagunit - for testing custom JSP tags
xmlunit - for XML
and so on. If you want a complete list, on the command prompt, if you’re using Gentoo, just type:

# emerge –search unit | less

and have fun!

For PHP there are a few different ones:

dev-php4/phpunit
dev-php5/phpunit
dev-php/simpletest

At the time of writing this, dev-php4/phpunit is masked, but the version of phpunit for PHP 5 is not. I chose simpletest because I didn’t have to unmask any packages in order to install it.

# emerge simpletest

BAM! Done!

(more…)

Changing Images with Java Script

Monday, December 17th, 2007

While working on a site, I decided to add some Java Script to the menu bar so the site would be more interactive. I wanted the image to change so it gave the appearance as if the user is actually clicking on the button. Being a bit rusty on my Java Script I had to look up how one is supposed to reference an object on the page.

I came across a well written tutorial on how to change the images, but there was one minor problem. The script didn’t work. Perhaps it’s my web browser, or maybe I’m doing something wrong. When I look at the code itself it doesn’t make sense to me. What I kept finding over and over again was this:

document[image_name].src = …

The problem with the above statement, is that you’re trying to find the image by referencing the document array with the image name. That is an array of documents, not an array of images. I played around with this at first thinking that maybe Java Script is a bit funny. The error console in Firefox kept telling me that I was trying to set the property of an object that did not have that property. Further investigation revealed that I was correct in my thinking.

The correct way in setting the source of an image object is actually:

document.images[image_name].src = …

The best way to change the image when the cursor is over the image is to write a function that can be reused:

function(mouse_over(image_name, image_source)
{
document.images[image_name].src = image_source
}

After including the code in your page, you can use the function like so:

PHP, Zend Debugger, Eclipse

Sunday, November 25th, 2007

A while ago I wrote how you can configure Eclipse, PHP on a Linux machine to debug PHP pages using phpdbg. At first this worked very well but after a while break point wouldn’t work and I would have to restart the session. Another problem that I was having was that the session information was saved. I got around this by clearing out the session information in Firefox. This continued until one day when I tried to start a new debug session and all I got was this:

At that point I decided to dump phpdbg and start using the Zend Debugger. Getting the extension to work properly took some time. After many hours of searching the Internet I managed to find a decent site to work off of. Unfortunately, it was aimed at the Windows platform. Getting PHP and eclipse configured was half the battle that I faced.

(more…)

Debugging With Eclipse and PHPdbg in Linux

Monday, May 21st, 2007

Using a debugger to help find errors will save you a consider amount of time. Eclipse is a great IDE because not only is it platform independent, but you can use it to program in many different languages. The features and plugins seem endless which is an added bonus.

The main reason why I like it is that it works on Linux. ;)

(more…)