| Lifelong Learning | ||||||
| Reports Written In Pursuit Of Professional Diploma On IT | ||||||
|
Contents Report on Educational Technology |
A CGI C++ Learning Project For Linux I would like to tell you about a small project that you can do so you can experience first hand how to undertake systems development under a Unix platform. A hypothetical company, Dyna Telecommunications Inc. would like to develop a CGI application that will enable new subscribers to register with Dyna for a free service the company is offering and allow old subscribers to update their personal data. The application will be accessible through the Internet and will use C++ as the scripting language and mySQL as the database management system. The system must be designed to run on a Linux server running the Apache web server. I feel that this project can provide you with a truly meaningful learning opportunity that includes a lot of technologies that should work together seamlessly. But more than this, it will provide you with the chance to experience developing a system under the Unix platform. The project will entail a lot of work on your part but I believe all of them are worth it in terms of the amount of learning that you can get out of it. The first thing that you will need is a Unix-like computer so we will be dealing first with how to dual boot your existing Windows Millennium computer with the Red Hat Linux operating system. Actually, you will need two computers to be able to test the application as it will be actually running but don't worry, you can develop and test it using your dual boot Windows/Linux computer. You can install a dual boot system in one hard disk but I strongly suggest that you invest in a new hard disk for the Linux operating system you will be installing. You know that computers use the hard disk for creating additional memory for use in swapping data to and from random access memory and disk. You can get a good deal of performance boost when you place your swap space on a different disk. I also suggest that you invest in a hard disk partitioning tool like Partition Magic to make dual booting easier and safer. You can use Red Hat Linux for dual booting but it is a bit complex and can result in your Windows operating system not booting at all. Adding a new hard disk and buying a disk partitioning software cost a lot less than buying a new computer, installing a Linux server on it and networking it with your Windows computer. Connect your new hard disk as a master device on the secondary interface of your motherboard then install Partition Magic in your Windows Millennium computer. Make sure that you choose not to install BootMagic, Partition Magic's booting software yet. You will install it later after you have installed the Linux operating system. You should complete the rescue disk option when you install Partition Magic in your Windows Millennium computer. You can repartition your hard disk while running Windows Millennium but I suggest you do it using the rescue disk instead. This way, you avoid doing something that can affect your Windows Millennium operating system. Perform a disk clean up of your Windows C drive and defrag it. Reboot your computer using the Partition Magic rescue disk. You will be presented with a graphical interface running on DOS Repartition your old hard disk by shrinking your Windows C drive to a size just enough to fit all the contents with a small elbow room for the swap space. Create a small partition of about 50 megabytes immediately after your Windows boot partition and format it using the Linux file system. This will be used as your Linux boot partition. Create a 200-megabytes Linux swap partition on the remaining space in your old hard disk. Commit the changes and then move to the new hard disk. Divide the new hard disk into two partitions of equal size. Format the first partition as a Linux file system and the second partition as a Windows file system. You will use this partition later as your Linux root partition. Commit the changes and reboot to your Windows Millennium operating system. Check that the new D drive has been recognized by your Windows Millennium operating system. You can now move your personal files to the new drive that Windows recognized. This way, you separate your own files from the operating system files. If you haven't made a Windows Millennium start up disk, this is the time to make one. Go to your Windows control panel and make a new start up disk. Insert the Red Hat Linux install disk on your CD ROM drive and boot your computer using the Windows start up disk. Be sure to select boot the computer with CD ROM enabled option when booting with the Windows start up disk. In your DOS command prompt, type your CD ROM drive letter followed by a colon and hit the enter key. Go to the dosutils directory and run the autoboot program to start the Red Hat Linux installation in your computer. You will be presented with a nice GUI where you can enter your installation options. Linux will refer to your first hard disk as /dev/hda. It will reference the second one as either /dev/hdb or /dev/hdc depending on where your CD ROM drive is connected to. In the disk partitioning phase, connect the /boot folder to the second partition in your first hard disk and the Linux swap partition to its third partition. Connect the root folder / to the first partition of your second hard disk. During package selection, make sure that you enable Internet services to ensure that the Apache web server will be installed. Don't skip boot disk creation so you will have a boot diskette that you can use to start your Linux system when something goes wrong. Make sure that you install the boot loader in the Linux boot partition, not on the boot section of the first hard disk. Reboot your computer using your Linux boot diskette. Unless you selected otherwise, Linux will default to a nice GUI log on. Login as root and edit the Apache configuration file found at /etc/httpd/conf. Look for the ServerName directive and change it to the host name you gave the computer during the Linux installation. Save the file and reboot to your Windows Millennium operating system. Partition Magic comes with a free BootMagic software that you can use for booting your dual boot computer. You can install it now using the Partition Magic installation CD ROM. Just run the setup program in the BootMagic folder and then run the BootMagic configuration program found in the BootMagic Menu. Make sure that both Windows and Linux are correctly detected and if not manually configure BootMagic with the two operating systems in your computer. Choose the enable option and save the configuration. Reboot your computer and check if BootMagic is running correctly. Choose Linux when BootMagic presents the operating system selection menu. To install mySQL, login to your Red Hat Linux system as root and run the graphical package manager program (rpm). Click install and select databases. Check all mySQL related packages and click the install selected option. Run the text mode tool menu and select the services option. Select mySQL by hitting the space bar while the cursor is positioned at mySQL. Reboot your computer and check hat Linux starts mySQL by noting what services are run during the Linux start up. Logon to your Linux system as root and enter the mysql command in the console. Type the command show databases; and make sure both the mysql and test databases are shown. Save the Dyna.txt file as Dyna.sql in root's home directory. This file contains the SQL commands to create the Dyna database, create the Customer table and populate the table with sample data. In the mySQL console, type source Dyna.sql; and press the enter key. If successful, type use Dyna; and press the enter key. Type select * from Customer; and press the enter key. You should see the sample data in the Dyna database. If everything went smoothly, you can start developing the C++ CGI programs that will comprise the application. The CGI programs must first present two choices to the user: for a new customer to register for the free service and for a registered user to update her personal data in the database. The new user types a cell phone number which the CGI will check as to correct format (11 decimal digits) and whether the number had already been used, that is, already present in the database. You should note that the cell phone number is used as the primary key in the Customer table and the database might reject a number already present in the Customer table. The CGI should provide the user with a blank registration form if the proposed cell phone number is acceptable. Otherwise, the CGI informs the user of the problem and go back to the start of the transaction. The user fills out the application form and clicks the submit button after which the data are checked firstly if they are there and secondly if the date of birth is valid. If anything is not in order, the CGI returns the form to the user for corrections. If the data are valid, the CGI will insert the new record in the database and inform the user accordingly. The CGI presents old users with a prompt for her cell phone number and password. It checks if the cell phone number is correctly formatted and if not informs the user accordingly. The CGI then checks if the number is already present in the database and if not it informs the user of the problem. If the cell phone number is valid, the password is checked and if correct, the CGI presents a form that contains the data in the database. The user then makes the needed corrections and submits the form for database update. The data are then checked for completeness and the date examined for validity. The CGI returns the form to the user if anything is wrong informing her of the problem. Only when the data are correctly validated when the CGI updates the user's record in the database. The CGI then goes back to the start for a new customer to register or an old customer to update her record. The application contains two major items: CGI processing and database update. The application should be able to parse the data coming from the form for checking and preparation for database update. A good first step is to make test programs for accepting and displaying the data entered by the user. This will teach you how data is forwarded by the CGI and how to parse the data passed by the CGI. I suggest you use the GET method since it is simpler and will use only the QUERY_STRING environment variable. To make the data usable, you will need to replace the hexadecimal characters sent by the browser with their corresponding character forms. Then, you should split the data into name-value pairs and place them in variables to they can be processed. You should learn how to present the data using HTML and how to send the data so the browser will be able to correctly display them. You might need to refresh yourself with some HTML tags used in forms processing like type, name, size and maximum length. Note that if you will use character arrays to store string variables, you should provide an extra byte for the storage of the null character at the end of the string. Be aware that the CGI will get all data as characters so you might need to use C++'s conversion functions like atoi for integers and atof for floats. Check the table specification in the Dyna.sql file to see what variables need to be converted. You might want to note also that the variable date of birth is stored as character. You can use the readonly HTML tag to prevent the user from changing some items in the form but be aware that old versions of Netscape Navigator does not seem to support this tag. Thus, you might want to perform data checking even if the data item is tagged as readonly in the CGI form. Make sure that you type your password variable as password so asterisks will appear when the user types her password. Next, you can code test programs for checking the data submitted by the user. You can do this in the command line using cin and cout to get and display the information. You can very easily copy and paste the code to the actual program later once the codes are found to be working. You might want to approach your date checking by converting numeric characters into integers using the atoi function. You can save a little bit of code by doing arithmetic comparisons on year, month and date as numbers rather than characters. Note that you should check not only if the numbers in the date are valid but also the date was entered in the correct format expected by the database. Lastly, make test programs for accessing the mySQL database in the command line mode first and then copy these later to the actual programs. You need only code processes for inserting records and updating existing records. You will need to master the C++ mySQL application programming interface (API) since you will be using it to access the mySQL database. The API provides the commands and constructs for using C++ to access the mySQL database. You will be using the MYSQL structure, result set, and rows to hold the data intended for the database and the standard procedures for connecting and then reading and writing records to the database. You might need to familiarize yourself with some functions and constructs in the C++ mySQL API like the MYSQL MYSQL_RES, and MYSQL_ROW. You should note that the MYSQL_ROW is actually an array containing the values from the database fields. Pay particular attention to the parameters required by the functions and think how these values can be most efficiently passed to the functions. The mysql_real_connect function for example expects a lot of parameters and these values must agree with values held in the database. You can review these values by looking at the user table in the mysql database. Doing a select * on the user table will show you the users and the rights granted to them. Don't forget to use the su command first before typing mysql in your console. This will ensure that you can open the user table in the mysql database. Finally, combine your test codes to come up with a set of programs for implementing the application. I have included a few poorly designed but working CGI programs which you can test just to see how the application will work. But you should make it a point to design and code your own CGI programs because only by doing so can you really learn how to do this. Log in as an ordinary user in your Linux system and save the dynastart.txt, dynanew.txt, dynaold.txt, dynainsert.txt, and dynaupdate.txt with the .cc extension in your home directory. Next, open up a console, type su and press the enter key. Enter the password for root. Type and enter the following commands to compile the CGI programs and put the executables in the cgi-bin folder of your Apache web server. # g++ -o /var/www/cgi-bin/dynastart.cgi -lmysqlclient -L/usr/lib/mysql dynastart.cc # g++ -o /var/www/cgi-bin/dynanew.cgi -lmysqlclient -L/usr/lib/mysql dynanew.c # g++ -o /var/www/cgi-bin/dynaold.cgi -lmysqlclient -L/usr/lib/mysql dynaold.cc # g++ -o /var/www/cgi-bin/dynainsert.cgi -lmysqlclient -L/usr/lib/mysql dynainsert.cc # g++ -o /var/www/cgi-bin/dynaupdate.cgi -lmysqlclient -L/usr/lib/mysql dynaupdate.cc You can now start your Netscape Navigator and enter http://computername/cgi-bin/dynastart.cgi and press the enter key. Test the program by entering different invalid data particularly for mobile phone number and date. Play around the application and see the results. Try entering character data with quotes (') on them and see how they are reflected in the database. Imagine what is happening to the data and think about how you would make your own version. How would you revise the programs presented to make them object oriented? Send me en e-mail if you have more questions. |
|
||||
|
Copyright 2008 Aureo P. Castro Email: [email protected] |
||||||