Is PHP Right For Your Website?by: Dan BallFull Version Article published Saturday, 27th September 2003 Is PHP Right For Your Website? As a small prologue to this column, I have to say that I, like most people, am a big fan of the old cliche "good price for a good product". To that end, along with many self-proclaimed computer geeks, I am also a big fan of the Open-Source movement in web development. For those few that have not yet heard of Open-Source, let me elaborate... The Open-Source community is a group of geeks that collaborate on development of products, and, with the release of these products, the source code is also given. This in-turn allows the user to recode and customize the application to their needs or add enhancements and release it back to the public. This method of development has resulted in things like Linux, Apache Web Server, and more recently PHP. Not only that, but it keeps the products advancing due to the seemingly endless development cycle. Now back to our regularly scheduled column... There are a whole lot of "p" acronyms going on today in web development today, we have ASP, JSP now there is PHP. All of these are scripting languages that run on the server instead of in the visitors browser. This method has many advantages, by running on the server you avoid browser compatibility problems and you also have the advantage of an easier interface with a database that resides on the server. ASP stand for "Active Server Pages", a Microsoft product, which is, therefore, only runs at it's best on a Microsoft platform. Some companies have made ASP packages for other server platforms, but they never perform like the true MS implementation. JSP is "Java Server Pages". I have worked with JSP and it is very nice and it costs nothing to download and install on your server (at the time of this writing). The major drawback I find with JSP however, is that the developer had better know a little Java Syntax before trying anything to sophisticated with it. Anything really interactive will require some serious coding, if not have a Java "Bean" or "Servlet" (both are applications that need installation on the server, and are significantly more complex code than typical web programming) developed for it. Enter PHP... "Hypertext Preprocessor" to the true geek, is very cool, easy to learn and use and free to implement server-side scripting language. In all honesty, I have just begun to use PHP in the last couple months while rebuilding my home business site, http://www.dbmasters.net/, and moving it to a new host. I saw this host supported PHP and figured I would give it a try. This was one of the better decisions I have made this year so far:-) For starters, PHP can run on many different servers, and the PHP engine is a free download. It runs very smoothly under Linux, which is also free and open-source. The point is, that except for your hardware costs, you can set up a PHP server for NOTHING, ZILCH, ZIP, ZERO MONEY! OK, putting the money issue (or, the no money issue) aside, lets dig into how PHP works. PHP is scripting that is embedded into standard HTML, to make the PHP code do it's thing, however, filename must end in the .php extension (or, .php3 for using features from version 3 PHP if that is how you configure the server). All the PHP code opens with "" (without the quotes, of course). So the code to include a file in your document would look like this: <?php include "/home/path/to/filename.php";?> There, you just got the first taste of PHP...you can now do server-side includes in PHP. The only other advice I can give about using includes is that if they are not in the same directory as the page including them, path the include right from the root of the server, relative paths can make problems, which doesn't really bother me personally because I code everything from the root anyway. In a previous column I discussed building email forms in PHP, so I won't go into great detail here. If you want to read that it is at http://www.allwebarticles.com/articles/1phpmail.html for your reading pleasure. Suffice it to say for this column, it is the easiest way to build an email form I have ever used. The real strength of PHP lies in it's database interactivity. The site I built runs on a MySQL database (a free relational database). It was the easiest database connectivity I have ever worked with. This is my database connection: <?php $database_login="login-name"; $database_pass="login-password"; $database_host="host-name"; $database_name="database-name"; $Err_Mail = "your email address"; $db=mysql_connect($database_host,$database_login,$database_pass); mysql_select_db($database_name,$db); ?> With that (replacing the obvious values with the proper username, password, etc.) I am connected to MySQL, for other databases the "mysql_connect" and "mysql_select_db" are just replaced with database specific codes that can be found at php.net. Then, inside the above php tags insert: Function MySQLConnect($errmsg, $msg="") { $success= mysql_connect($GLOBALS["database_host"], $GLOBALS["database_login"], $GLOBALS["database_pass"]); if (!$success) { mail($GLOBALS["Err_Mail"], "MySQL Connect Failure: $errmsg", mysql_errno(). ": ".mysql_error(), "From: AutoError"); die(); } } Function MySQLQuery($query, $errmsg, $msg="") if (!$success)
error_reporting(0); MySQLConnect("There was an error connecting to MySQL", "Sorry, this service is temporarely unavailable."); echo "<P class=\"bodysm\"> Related Stories: » Protecting your PHP and HTML Source Code » Publishing Newsletters Using PHP & MySQL - 4 » Publishing Newsletters Using PHP & MySQL - 3 » Publishing Newsletter Using PHP & MySQL - 2 » Publishing Newsletters Using PHP & MySQL » Unix Webserver Crontab Basics |