Skip to main content

Posts

Showing posts from April, 2008

Designing a Simple “Quote of the Day” Script in PHP

“Quote of the Day” is quite an old feature, people put on their sites. And of course people like it; I mean who doesn’t like to know the sayings of great persons! In this post we’re going to design a complete quote of the day system to place on our web site. The title is a bit misleading actually, because most of these types of scripts show a new quote (randomly) each time the page is requested and not once a day. So if you incorporate this script on our web site, you’d see a different quote each time you reload the page. You may easily change it to “actually” display different quotes for each day only but people like that random one! The working of the quote scrip is pretty simple. We first store a good number of quote in an array (here we’ll just be storing a handful as an example). Generate a random number from 0 to NUM_ELEMENTS_IN_ARRAY-1 and just show the quote from the array with that index. Here is the script: <?php //quotes $quote [ 0 ]=

Arrays and Array Processing in PHP

Arrays are a set of variables of the same data type. If you’ve ever programmed before you should be knowing about it. but PHP takes arrays to the next level by providing so many useful in-built features to operate on them, let’s see some of them. Types of Arrays You should be familiar with the following type of array: $ar [ 0 ]= 1 ; $ar [ 1 ]= 10 ; It is an example of numerically indexed array since numerical values are used as indices to reference different elements of the array. One other type of array that PHP supports is those having string indices which sometimes may make indexing more meaningful. $temp [ 'jan' ]= 21 ; $temp [ 'feb' ]= 23 ; $temp [ 'mar' ]= 24 ; This type of array is also called Associative Arrays. As is obvious, the array above should most probably be having temperatures of different months. Had it been numerically indexed, it wouldn’t have made that much sense. So you can use these type

String Manipulation Functions in PHP

In the previous post Properties of String in PHP , we were discussing about the different properties of strings in PHP. String manipulation as you know, is an important part of web programming. PHP being a web programming language thus provides good set of string manipulation functions. In this post we’re going to discuss some of those which arte frequently needed. 1. trim() function Prototype: string trim  ( string str ); This function strips white spaces from the start and end of the string supplied returning the resulting string.. When we have to take user input via form, it’d be a good idea to “trim” the variables as extra white spaces sometimes creep in. $name = trim ( $_GET [ 'name' ]; 2. explode() function Prototype: array explode  ( string separator ,  string input ,  int limit ); The argument “limit” is optional. This function splits the string “input” on a specified “separator” and returns the different split strings as an arr

Properties of String in PHP

1. Strings in PHP can either be enclose in single quotes (‘) or double quotes (“). $str = ’Strings in PHP’ ; $str2 = ”PHP” ; both of the above are valid strings in PHP. 2. Two strings can be concatenated or joined together with the help of the “.” Dot operator. $str = ”I Like “ . ”PHP” ; $str2 = $str . ” a lot ! ” ; Now, $str will have the string “I Like PHP a lot!”. 3. Guess what the following code will print $str = ”String” ; echo  “This is $str” ; I bet many of you thought that it’d print “This is $str”. But actually it is going to print “This is String” because variables inside (“) double quotes are evaluated in PHP. If we had used (‘) single quotes for the string like: $str = ”String” ; echo  ‘This is $str’ ; It’d have printed “This is $str” as variable inside (“) single quotes are not evaluated. 4. What would you do if you have to output a string like below: He said “Wow!” Would you write?

How File Processing is done in PHP?

In the previous post’s ( Designing a Simple Order Form Application in PHP ) example we implemented the file I/O for storing order information without discussing about it. For those of you who were eager to know more about PHP File I/O read along, this post has it. File processing requires the following steps: Opening a file Doing the operation Closing the file Opening a file First of all we have to open the file before any operation (reading/writing) can be done. If you remember the previous post Designing a Simple Order Form Application in PHP , we had this line //open file $fp=fopen( "orders.txt" , "a" ); There we’re opening a file named “orders.txt” in the “append” file mode. File modes tell PHP what we want to do with the file. Some of the commonly used files modes along with what they mean is listed below: r For reading only, reading begins from the start of the file r+ Reading and writing, b

Designing a Simple Order Form Application in PHP

Ok guys, for this post we’re going to create a simple yet complete order form page. Order forms are used on many sites to take customers order online. Order forms should have the capability to take orders from visitors regarding what items they want to purchase and store the information for further processing. For this post’s example, we are going to create an order form for a Book Seller. The form will be designed to take order of five different items (books). Our order form application should be able to take order of five different items in any separate quantities tht user wants, it should also ask for shipping address and name of the customer. It should then store the information provided in a file along with the date and time order was placed. The application should also be able to take any number of orders and store them all linearly for further human processing. For this, we need a front end of a HTML form to which the user would interact a

How does CMS Create Dynamic Pages II

This is the continuation of the last post How does CMS Create Dynamic Pages . If you run the script given on that post (save it with the name “cms.php”), you’d see a site like below: As you can see, it’s a simple five page site of which all of the five pages are available (only 3 are shown in the image though). Isn’t it amazing for just one PHP script to create a five page site! If you look at the code and try to understand, you see that the script is designed to show the Homepage when no data is passed. It creates different pages from the data in the arrays when the respective page is asked for, by passing p=0 to p=4 to the script. We can create 10, 20, 100 or even a 1000 page site like this just from one script. In fact most CMS do that. Do remember however that the array storing the content was just to depict the database and real sites would store content in database. Did you notice the line $page=$_GET[‘p’]; As you know we have two methods of se

How does CMS Create Dynamic Pages

Content Management System (CMS) gives you an easier way to manage sites. It gives you a nice front-end to write, publish and manage content while hiding the technical details like FTP, HTML and other coding work. Most of the CMSs have a web based front-end that means you can manage the whole site from within the browser. Some examples of popular CMS are Joomla, Wordpress etc. One major characteristic of CMS is the fact that it creates the whole site dynamically, it means most (if not all) of the pages that the site has, are created dynamically. Conventional way of creating site was to create different HTML pages for each article (page) the site has. Contrary to that most CMS don’t create different files for pages. They store the content in the Database and create the pages from the Database. That means the content don’t reside in pages or files but rather in the Database. In this post we’re going to create a simple system that will help you understand

An Example of User Authentication System in PHP II

This is a short follow-up of the last post An Example of User Authentication System in PHP . In this post we’ll talk about the two methods of from sending GET and POST and how thy affect the way data sending From the previous posts example, when we provided the username and password and clicked on submit, we saw something like this: If you look at the address bar, you can see the data (username and password) being sent. Now, that’s not a good thing, if we are using a password box to hide the password being entered then what its use is if it can be seen this way! The good thing is that with very few modifications, the data passed can be made invisible (not to appear on the address bar). How? By using POST method of data sending for the HTML form. It can be done like below: <html > <head > <title >Simple Uesr Authentication System </title > </head > <body > <form name= "form1" method= &qu

An Example of User Authentication System in PHP

In this post we’re going to create a very simple user authentication system in PHP. It’d be like the one’s you see while logging in to various sites/services (emails, forums, social networking sites etc) User authentication is a way for sites to know who you are among the other registered users and showing you relevant content (may be confidential). For example it’s only you ho is authorized to see your emails because you only know your authentication information. In this post we’re going to create two files, a HTML page which will collect the username and password in a form. These information will then be send to a PHP script, which will verify and show the required information. Below is the PHP code: <?php //define some constants define( "USERNAME" , "goodjoe" ); define( "PASSWORD" , "123456" ); define( "REALNAME" , "Joe Burns" ); //have the data being passed $user=$_GET[ '

Taking User Inputs to Create Personalized Pages II

HTML Forms are a method of sending information from a page to a script. Forms in HTML have the following form: <form name= "form1" id= "form1" method= "get" action= "script.php" > </form > here, name=”form1” is the name of the form, may be anything id=”form1” is usually same as the name of the form method=”get” is the method by which sending of data will take place. It can also be “post”. action=”script.php” is the name of the script to which the data is to be passed to. A form can have many child elements such as input box, password box, check box etc. a form almost always has a submit button which on clicking sends the data entered in various elements of the form to the “action” script. We’d created the script in the previous post Taking User Inputs to Create Personalized Pages , so we need not work on that, here we’ll only create a HTML page having a form which will send the dat

Taking User Inputs to Create Personalized Pages

You might probably have seen this type of URLs: http://somepage.com/index.php?name=noname&age=18 It of course is a dynamic web page. As a matter of fact the string after the ‘?’, the data, is what makes it dynamic. The page is dynamic because most pages which take data this way, create the page according to the data passed. In this post we’re going to see how pages take data, we’ll also create a simple dynamic page which can be personalized to display user’s name. First, let’s understand what is in the URL http://somepage.com/index.php?name=noname&age=18 index.php is a PHP page and everything after the ‘?’ is the data being passed, which is name=noname&age=18 here name and age are the two variables having the values noname and 18 respectively which are being passed. Each variable is separated by a ‘&’ sign. That’s it; now let’s see how we can catch these variables from a PHP script. This method of sending data is known as GET metho

Variables, Type Casting and Constants in PHP

Let’s start by looking at the fundamental difference between variables in PHP and in C++: Variables in PHP need not be declared before using Variables can hold any type of values due to the fact that variable are not declared of any type, you can store any value in any variable no matter which type of value it is currently holding. So, in the previous post ( Conditional Statements if...else in PHP ) when we needed a variable to hold the integer (hour of the day) we just wrote $t=(int) date(“G”); and suppose we now want to have a string to be stored in $t variable, we’d just have to write $t=”PHP”; You see, in the first statement it was an integer variable but now it’s a string. Therefore we can conclude that the type of a variable is determined by the value currently assigned to it. PHP has the following types of data: Integer Float String Boolean Array Object Since PHP takes care of data types interna

Conditional Statements if...else in PHP

Look at the following code: <?php $t=( int ) date( "G" ); if ($t< 12 ) echo "Good Morning!" ; else if ($t< 17 ) echo "Good Afternoon!" ; else if ($t< 21 ) echo "Good Evening!" ; else echo "Good Night!" ; ?> If you are a reader of this blog (or know C++!) then the code would look familiar. As is obvious, that is a PHP code but looks similar to C++. You see, the if and else statements are all similar to that in C++. Talking about the code, it is pretty straightforward, we are taking the current time, type casting it to an integer in a variable $t (type casting is also done in the similar manner in C++). Then we are printing different messages according to what the current time is. That being clear, let’s us look at the working of date() function a bit. We’ve previously used date function in the post Writing and Running PHP Scripts . As I explained, it takes a string as

Configuring Apache Web Server and PHP

[Update: You may download the MSI package of PHP instead of the ZIP one which would do all the set-up and configuration itself and you wouldn’t have to do the following by yourself] This is the crucial part guys, first I start off with giving the locations of the configuration files of Apache and PHP. Apache: [X:\…]\Apache Software Foundation\Apache2.2\conf\httpd.conf PHP: [Y:]\PHP\php.ini-dist [renamed to php.ini later] Now let’s start guys: 1. Start by backing up both the configuration files, you’d need them in case you mess up the files. 2 . Rename the php.ini-dist file to php.ini 3. Look for a line like below doc_root and make it look like doc_root =[X:\…]\Apache Software Foundation\Apache2.2\htdocs be sure to change ’[X:\…]\’ with the location you installed Apache server in. Save the file. 4. Open the folder you installed PHP in. There you’ll see lots of DLL files, select them all and click copy. Now open your Windows System direc