Archive

Archive for April, 2009

What is CGI and How Can It Be Useful?

April 17th, 2009 admin 2 comments

Well, to be honest, in the present day the CGI wrapper is used much less commonly with all the new web programming languages such as: PHP, Ruby, etc. However, CGI, to put it simply, is a kind of C wrapper for websites. The concept sounds confusing and the truth is, it is just like any other programming language.

Let’s begin discussing the uses of it. The most important use of it today is for hosts that are not PHP (or any other language) enabled. Most hosts allow CGI (even though if programmed wrong can make the system vulnerable) if they don’t allow PHP or some other advanced server-side scripting. Normally, these files need to be placed in the “cgi-bin” to execute. Hosts do this as a sort of precaution, trying to confine the areas of break-ins due to poor programming.

Actually using it is different now. If you’re writing a Perl script, there is no compilation needed, you write like normal. However, if you are a C programmer, such as myself, and wish to use C, compile the source as program.cgi. Now, I’ve dabbled with Perl and can program in it fairly decently, but I’d much rather use C. It’s much easier for me. So, I’ll provide source examples for both styles here!

Now, let’s look at an example script:
The first and most basic script to any sort of programming is “Hello World” so I’ll provide the source for each (and the compiled binaries in the package below for the C-version scripts)

hello_world.pl (This will run properly as is)

#!/usr/bin/perl
# Hello World CGI Script (Perl Version) by Dennis M.
#
# Remember now, comments in Perl must begin with the '#' character

## Our content type ##
print "Content-type: text/html\n\n";

## Now print the page! ##
print "<html>\n
<head><title>Hello World! :D </title></head>\n
<body>\n
<h1>Hello World!</h1>\n
Hello World Script!\n
</body>\n
</html>";

and here is the C source (which, again, needs to be compiled)
hello_world.c

/**
 * Hello World CGI Script (C Version) by Dennis M.
 *
 * I prefer this (although it needs to be compiled) because
 * I'm a C programmer ;)
 *
 * oh yeah, and because we're compiling it with GCC or whatever
 * you use, we can use normal comments ;)
 *
 */

#include <stdio.h>

int main(){
  // Our content type
  printf("Content-Type: text/html;charset=us-ascii\n\n");

  // Our page!
  // We're going to break it up line by line so you can
  // see what's going on here.
  printf("<html>\n");
  printf("<head><title>Hello World! :D </title></head>\n");
  printf("<body>\n");
  printf("<h1>Hello World!</h1>\n");
  printf("Hello world! C style ;) \n");
  printf("</body>\n");
  printf("</html>\n\n");

  // Let's terminate the program now :)
  return 0;
}

Now, we won’t go too deep into the code because simply, they are two completely different languages. But, it’s important to point out that both send headers (Content-type: text/html). You can Google content-type’s and figure what does what, but there are many different types. Another commonly used type is Content-type: text/plain. Notice, if this had been the case, no HTML would have been parsed by the server and everything would have been displayed as plain-text!

So that’s basically it :) CGI is just a wrapper to run Perl and C programming on the web!

Here you can download the sources AND binaries:
CGI Tutorial – Hello World

One more tutorial left in the series! Keep sending in the questions so I have a few more topics to write about! ;)

Regards,
Dennis M.

Categories: C/C++, Other Tags: , , , ,

What are PHP Classes and How to Use Them

April 16th, 2009 admin No comments

So some users have been e-mailing me asking me, “What are PHP classes? Why not just use functions?” Well, you could technically just use functions but it makes things messy and insecure. Let me explain. Classes, to oversimplify things a bit, are organizational units within PHP or any other Object Oriented Programming (OOP) language such as C++.

These organizational units, or Classes, hold common functions that you program. They can be used in numerous ways from defining __construct() for everything, or having a private function …() so that nothing outside of members within the class can access the function.

Well, you know what they say; good leading is leading by example. So we’re going to write a quick class and break it down!

<?php
/**
 * Simple class by Dennis M.
 *
 */

// Class definition
class Test
{
	// Define variables here if any.
	var $test = NULL;

	/**
	 * Now, we have two ways to define our constructor.
	 * we can use function __construct() OR we can use
	 * function Test(). If __construct() and Test() are both
	 * used, then the class will use __construct. If not, it
	 * will look for Test(). If neither are defined, we don't
	 * have a constructor XD
	 *
	 */
	function __construct(){
		/**
		 * Define actions in here that will
		 * take place for every function in the class!
		 * If you're not using a DB wrapper, then
		 * you can even define your mysql_connect()
		 * and mysql_select_db() in here so the functions
		 * don't have to do it later!
		 *
		 */
	}

	/**
	 * This next function allows us to print out information
	 * that is returned. Now, we're going to use two functions in here.
	 * The first function that is going to be defined is the public function
	 * for what we want to do. The second function, the check function,
	 * is going to be called within this function, but can only be called by a
	 * script like this because it's private and this function is within
	 * the class.
	 *
	 * It sounds much more confusing than it is in practice :)
	 * let's take a look.
	 *
	 * NOTICE: function text($param='val'). This defines an optional variable
	 *	   when calling the script later. E.g.: text("hello"); or $text();
	 *	   will both work. The first one will use "hello" and the second one
	 *	   will use the default value.
	 *
	 * @return string
	 *
	 */
	function text($say='test'){
		/**
	 	 * Let's run the check first.
		 * The check is a bool function so
		 * when we check for a response, we'll
		 * look for true or false.
		 *
	 	 * Note, because the function is within the
		 * class and it is separate from this one,
		 * we call it using the $this-> operator.
		 *
		 */
		if($this->check($say) != true){
			return "Could not find "".$say."" in the variable \$test.";
		}

		/**
		 * That was our only check. Now, let's
		 * return a success message if
		 * we got passed that!
		 * Realize that 'return' in a function is similar
		 * to an 'exit' in non-function programming.
		 *
		 * Also, return always returns with a value unless
		 * you are creating a VOID function, in which case
		 * you would just use return and no value would come back.
		 *
	 	 */
		return "Success! We found "".$say."" in the variable \$test!";
	}

	/**
	 * Here is the private function we were talking about.
	 *
	 * Private let's us know that no object outside the class "Test"
	 * can use this function.
	 *
	 * @return boolean
	 *
	 */
	private function check($say){
		/**
		 * We defined this variable earlier as
		 * null. Define it as string 'test'
		 * which is what the preceding function
		 * uses by default.
		 *
		 */
		$test = "test";
		if($say != $test){
			return false;
		}
		// That was the only check :)
		return true;
	}
}

// Now we have ended the class but in order to initiate it we
// must define an operator. This is how:
$testclass = new Test;

// Now, we must use pointers to call to functions!
// This will NOT work.
print "Test 1 should fail: ".$testclass->text('w00t!')."<br /><br />";

// This WILL work
print "Test 2 should succeed: ".$testclass->text()."<br />OR
<br />".$testclass->text('test')."<br /><br />";

// This WILL NOT work
print "Test 3 should fail: ";
print $testclass->check('test');

?>

Now I’ve pretty much explained most of what I’m going to cover within the code in the long comments. Now this tutorial is mainly an introduction to classes, but I’d like to leave you guys with a little something more. I’m going to leave this off at the beginning of the “intermediate” level. It’s very difficult to cram EVERYTHING about classes into one post because the possibilities are infinite. So, I’m going to get you started right now on learning more about them and the only way to begin achieving that infinite knowledge is to keep working with them!

You can add the following to the previous code (but make sure it is BEFORE test3 because the error will stop the rest of the script from processing from that point on)

class Test2 extends Test
{
	/**
	 * Note the "extends Test" defined after "class Test2"
	 * This makes Test2 an extension of "Test". In Test2
	 * you are now able to call all functions from Test
	 * using the $this-> operator except for the private
	 * functions. Those are still reserved for the Test class
	 * only.
	 *
	 */
	function w00t(){
		return $this->text();
	}
}

// Define second "test" class
$testclass2 = new Test2;

// This WILL work
print "Test 4 should succeed: ".$testclass2->w00t();

And that about wraps this one up! As usual, for those of you who would rather just download the code, it’s available here:
Classes Tutorial

2 more topics to cover, so get to e-mailing me on what you want to see after those!

Regards,
Dennis M.

Categories: PHP Tags: , , , , , ,

C++ Memory Management Tutorial

April 14th, 2009 admin 1 comment

This is the second article in the series that I’ll be working on! For those of you who do not already know the basics of C++ and how to compile, etc. I’ll go through a quick run through, but you should visit other sites (Google is a good resource) to find more information! The basics of compilation will be listed below this tutorial! As usual, there will also be a link below to download the whole script as well as compiled binaries!

Here is the main.cpp file:

/**
* Memory Management Tutorial by Dennis M.
* (C) 2009 Dennis M. All Rights Reserved.
*
* This is the basic file to making everything work!
* This isn’t a “basic” tutorial, so it will be less in
* depth than what many may be used to from me.
*
*/

#include <iostream>

using namespace std;

// We’re keeping this simple, so we’re going to keep everything
// in just one main() :)

int main(int argc, char* argv[])
{
/**
* Some memory allocation:
*
* float naturally uses more memory than int and is slower
* but in many cases is more effective from a personal view
* so we’ll use both here.
*
* This is how it works. We’re going to request memory allocation
* BEFORE we actually use it! We’ll allocate 10 bytes to the float var
* and 90 bytes max for the int!
*
*/
float* val1 = new float[10]; // Handle all numbers & initiate it as 0
int* val2   = new int(90); // Start it as 90

cout<< “Value 2 + 67 = “<< *val2+67 << endl << endl;

// Try to allocate too much :P
// the nothrow doesn’t allow this to be allocated if the system
// doesn’t have it..
int* nogo = new (nothrow) int[999999999];
if(!nogo){
cout<< “Your PC doesn’t have enough Memory Available! =-O *shocker*” << endl << endl;
}

/**
* Just some other simple work:
* Float Addition! :)
*
*/
*(val1+1) = 15.123097;
*(val1+8) = 1000.203998124;
cout<< “val1[1] (“<< *(val1+1) <<”) + val1[8] (“<< *(val1+8) <<”) = “<< *(val1+1)+*(val1+8) << endl;

// Free all the memory we’ve used
/**
* Delete the arrays: where we used allocation – e.g. delete [] VAR
* Delete the val2 normally :)
*
*/
delete [] val1;
delete val2;
delete [] nogo;

// Set everything to 0!
val1 = 0;
val2 = 0;
nogo = 0;
}

Now that’s all there is to it really. The code is pretty well documented and as always is much of the tutorial! Nothing I really need to explain in it seeing as this is a mainly intermediate tutorial.

As I promised, some compilation techniques:

For most *nix systems (must have gcc installed)

g++ main.cpp -Wall -o mem_management

And for Windows users, I suggest using either Visual C++ by M$ (which the compiler is visual and self-explanatory) or a personally I enjoy Ming (with MSYS) or CyGWIN. With either of those installed, run the command mentioned above for the *nix users and it should all run ok!

As usual, the download link is below:
Memory Management Tutorial Download

Keep an eye for the next few tutorials in the series listed a few posts ago! They should all be great ones!

Regards,
Dennis M.

The Basics to Working Within a Linux Terminal

April 13th, 2009 admin 6 comments

So I’ve had some break time before that project is finally due so I’m taking it to write a little something about the Linux terminal. (The first item I’m addressing off the list 2 posts below)

So what is a Linux terminal? Well, to keep it simple, it’s the computer’s interface. For those of you (probably the majority) who are Windows users, Linux does not come standard with a GUI, but rather you must download a 3rd party interface such as GNOME if you need one. Also, to keep it related to Windows, the Linux terminal is equivalent to Microsoft DOS’s command prompt. Now, I don’t want any of you to get confused and think that Windows and Linux are the same because they are not even close on any level. I think it is safe to say that their only certain comparison is that they are both Operating Systems. While Linux seems to be the system of preference, most users prefer Windows to use as their home PCs or what have you.

Now that we’ve got that out of the way, why is this tutorial just on the basics rather than everything with the terminal? Well, not to mention there is far too much to learn in just one post about the terminal, the basics will work across ALL Linux and *nix (and probably most BSD) operating system distributions (distro). Note that there are many distros such as the infamous RedHat Linux, Debian Linux, Ubuntu, and FreeBSD (a BSD system) all of which function fairly the same although each have arguable advantages over the other.

The Linux terminal is also very often referred to Shell Access (or SSH) by hosting companies. This can be a VERY useful tool for web developers who need to create things such a cron jobs or use system commands which they could not usually use with the standard FTP access.

With all that said, we’re ready to begin. This guide will more or less be a list of functions followed by their purpose and what some may do in an actual environment!

The format is as follows: COMMAND <required param> [optional param]

ls [directory] – Will list the files in a given directory
cd <directory> – Change to a different directory
ps aux – View processes
ps aux | grep <process_name> – Will find a given process by name if it’s running
find <directory> -name <file_name> – Find a file by name. For example, if you use find / -name w00t* it will return the paths of ALL files beginning with w00t
rm [-params] <file>- Will remove a given file or files. To remove a directory you must use rm -rf <directory> NOTE: Linux (without GUI) has NO trash bin, so anything you use rm on is removed FOREVER!
cp [-params] <file> <dest> – This will copy a given to file to a give destination
top – View processes and memory usage in realtime
df -h - See system partitions and usage (The -h parameter puts sizes in Megabytes instead of blocks)
uptime - Uptime information as well as system load averages
su - If you have root login information and are logged in as a regular user, use this command to grant root access (su = super user)
chmod <file_or_directory> <PERM_LEVEL> – Especially important for web developers. Different from windows, this is file access level. For web pages, 0644 is the standard and for directories it is 0755. 0777 for any file you want to give full permissions to (write access: e.g.: ability to upload files into the directory from a PHP upload script or something)
mkdir <dir_name> – Makes a directory

That’s a basic list and should be a more or less helpful guide to anyone who is starting to Linux! Enjoy and look out for the new articles to come out soon! ;)

Regards,
Dennis M.

Categories: Other Tags: , , , , ,

New Project (Almost Done and Can Start Articles!)

April 11th, 2009 admin No comments

Well I’ve been working on a new project which will cause the other things listed in the last post to be slightly postponed. This project is a very interesting one that I’m actually excited about. Until I’m allowed to talk about it, I won’t give exact details, but the project forces me to use my knowledge of Java Platform programming and PHP/MySQL. It’s exciting and the deadline is early next week, so once I turn that in to the client, I will proceed in writing the articles mentioned below!

Take care,
Dennis M.

Categories: News Tags: , , , ,