PHPBuilder - Beginning PHP: One Goal at a Time



RSS Twitter
Articles Html

Beginning PHP: One Goal at a Time

by: Ali Al-Athary
|
August 31, 2006



A couple of months ago, I knew almost nothing about PHP, only that is was a programming language that I needed to learn. Today, my whole site relies upon PHP and I have gained the confidence that almost anything is possible using PHP. I accomplished this achievement using methods that I will describe in this article.

Set Small Goals That You Can Achieve

One of the most destructive things a programmer can do is to try to develop an extensive program or complicated website all at once. When I started to create my new site, I began with a simple goal--to make a members' area that users could log into and out from. I didn't decide what features the members area would have, I just wanted to successfully code the restricted area. I made a very simple form of registration--a database for users and the PHP functions that were required to transfer the data to the MySQL database. It was not a 'mission impossible' goal, but it was something that I knew I could accomplish and then I could move on to the next goal.
At that point I determined how to secure the data that was entered by users in the form. Keeping in mind that hackers might try to mess up my database by abusing the auto-registration or filling it with spam, I decided to make a simple security key using an image. The user is required to enter the text from the image in order to prevent spam/robotic registrations.
My next task was to decide how people can login and logout and how to validate the form to make sure someone did not enter garbage data in the email field. Then I needed to figure out how to send an email when the user finished the registration process. This was needed in order to make sure that the user entered a valid email.
After I worked out the features mentioned above, I started to think about my next task. I decided to allow members to post their own tutorials on the site and store them in the MySQL database. I wanted to be able to:
  • rate these tutorials
  • create a profile for each user
  • allow users to comment on several pages
  • allow users to search through these areas
  • integrate the JavaScript with the PHP code
The PHP code along with the JavaScript provided me with dynamic content, which enabled me to insert the new tutorial titles into a JavaScript drop down menu. I improved the method I used for rating to use "Asynchronous JavaScript and XML", commonly referred to as AJAX, so the page would not need to be reloaded if user rated a tutorial or profile.

The Internet Is a PHP Developer's Friend

Even if you don't have any PHP manuals or books, if you're reading this article, you're in luck! Internet search engines, discussion forums and the PHP manuals located at www.php.net can be your best tools. The first place I look for any PHP function syntax or implementation methods is at the PHP manual site mentioned above. Then I try out the PHP code myself, and if I haven't found what I need or am faced with a problem, I use search engines or forums to find the answer to my problem.
Keep in mind that by searching the discussion forums, I have also found some very complicated solutions to problems that I was able to solve in a much simpler manner. Nevertheless, the forum posts can provide you with an idea of what is going on with your code, or what you need to do to accomplish your goal.
One of the issues I faced that took a lot of time was when I made the security key for my site. An instance of PHP was mysteriously running twice, so the key that was shown as an image to the user was different from the actual key that was sent to the PHP form processing page. This issue was occuring only with the Mozilla browser. I didn't know at the initially that PHP was running twice and I didn't suspect that it was related to the browser until I searched online and found some posts in forums from folks that had similar problems. They explained that Mozilla requests HTML and images using separate requests. Using this info, I was able to come up with a working solution.

Everything You Want, You Must Code

While you do not want to use other developers' code, you can certainly learn from their work. If you depend on using other programmers' work, it will only take you longer to learn the tricks of the trade, so to speak. For example, if I had used the open source forums as a tool for creating my tutorial submission mechanism, I would have ended up not knowing how to store the caret in a form's textarea.

Hack Your Own Site

Security issues are very important for any developer, even for the most rudamentary applications. For instance, you do not want to wake up one morning to find your site's guestbook full of junk and porn advertisements.
If there is any possibility of an application not working properly, your site has a flaw. One of the best things about PHP is that it is a server side language, which means that users will not be able to see the code directly--things will work behind the scenes. Conversely, if you write bad code, you might inevitably allow your users to really mess things up on your site. For instance, you would never want to allow the transfer of a variable that is entered through a form directly to your database. You should use htmlspecialchars to avoid allowing someone to add malicious code to your database.

Interactive Site?

These days, to be topical, sites have to be flexible and interact with their visitors. The best way to create some interaction is to mix JavaScript and PHP and let these "teammates" work together. One of the most promising new technologies is AJAX, which does just that. I recently started to use AJAX on my site. It is easy, fast and saves users from waiting for your page to reload again just to post a comment (or rate a tutorial, in my case). Because PHP is a server-side language, you only need to reload the page to pass a variable. Using XmlHttpRequest or other Ajax methods with JavaScript, however, you can communicate with the server in the background and bring the results back to the user without having to reload anything.

Conclusion

Although I am still learning PHP, by using the techniques I mentioned above, I was able to create my own interactive website. I continue to add to my site, and my knowledge of PHP increases with each new feature I add. I encourage you to begin your own journey into PHP programming--you won't be sorry.


Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Ali Al-Athary

Comment:



Comment:

(Maximum characters: 1200). You have characters left.