Hello! Thanks for stopping by. In this tutorial we will be looking at using server-side scripting in your websites. Don’t worry if that means nothing to you at the moment – you’ll understand it perfectly by the end of the post. To get you excited though, once you understand this concept, you will be ready to go with true interactivity on your sites – ie login functions, membership sites, e-commerce, booking forms, database interaction etc. In other words, once you start on scripting you are well on your way to becoming a true web developer.
This first post on scripting will be mainly theoretical as I think it’s important to know a bit about the concept before diving in with code. We will probably start to do some set-up in the next tutorial. Don’t worry though, this hopefully won’t be a dry lecture – as always I’ll try and keep it short and to the point, and only give you the things you need to get going, and pointers if you wish to study more in depth concepts on your own. So lets go :)
What is Server Side Scripting?
So far in our sites we have kept things simple. We have pointed the browser at an HTML file on our computer, and the browser has displayed the page by reading the HTML we have pointed it at. Pretty straight forward stuff.
Now, this is fine for small static web pages that are never going to change and aren’t going to do anything fancy. But we lost interest in that kind of site 20 years ago. What if we want to be able to change the content dependant on any number of variables – what if we want to make the content dynamic? Well, we can do that, quite easily. Instead of having one static HTML file that the browser finds and reads, we can give the browser pages that are made on the fly. Instead of there being any number of static pages, we serve pages to the browser that are built using a server.
So here’s our new flow so far:
Ok, to sum up what we know so far:
- The browser requests a page
- The request is received by the server
- The server builds the page
- The server responds by sending the built page to the browser
- The browser displays the page
If that all makes sense to you, you may be asking how the server builds the page?
PHP – Your Friendly Server-side Scripting Language
It doesn’t take much reading around web development to hear PHP mentioned. PHP is a scripting language – this means it is used to write short pieces of code called ‘scripts’ that carry out distinct tasks. If you want to find out more about the language, start with the Wikipedia entry.
In the context of serving servering web pages, PHP is the language of choice for many developers. Millions of web pages around the web are written in PHP, mainly due to it’s ease of use, it’s ability to be picked up easily by beginners, it’s fantastic documentation, and massive user base. All of these points have made PHP the leading language in use around the web today. Of course, something this popular receives a lot of stick from ‘real coders’, but don’t worry about that. You will find most real coders ( note the lack of apostrophes ) will agree that PHP is very good at what it does.
So how exactly is PHP involved in the building of a web page? Where is its grand entrance, so to speak? By way of the server.
The ‘Apache Server‘ is another term synonomous with web development. An Apache server is one ‘flavour’ of web server in use today. It’s probably not the best, but it’s very capable of doing a great job ( like PHP ), is used in millions of web pages ( like PHP ), is easy to learn ( like PHP ), and has a great user and support base ( like PHP ). You begin to see the pattern? These technologies go hand in hand as being a great entry point for beginners, and the way I got into web development. So it’s only natural that this friendly combo is the way I will be going in my tutorials :)
Oh, and I forgot to mention that using an Apache server and PHP are both entirely free, like all great things on the web.
How’s it work?
Right, let’s see how all these things work together. Let’s go through it step by step so it’s perfectly clear for everyone.
First of all, someone, somewhere in the world, types in a web address in to their browser. They wait patiently as this request is sent by their browser to the server that looks after, or ‘hosts’ this web page. The server receives the request and as long as it’s a valid request ( the page exists, the person has the right authority to view the page they are asking for, etc. ), the server will go and fetch the page. This is where things are a bit different from the simple browser/html file model that we are familiar with. In this case, we are fetching a PHP file.
The PHP file will have the job of building an HTML document for the browser to read. Now, PHP is a language, and so it has the ability to write the page dynamically, to make decisions depending on the request it receives from the browser. For example, say our user wants to log into a member’s area. The browser will send the username and password to the server. The server then calls up the relevant PHP file, or script, lets say ‘login.php’.
Login.php will contain lines of PHP code that will decide if the username and password are a match, maybe by checking them against a database of the users. Once the script has decided whether the user can login or not, the response is written out and served to the browser in the form of an HTML document that the browser then renders and displays to the website user. The browser doesn’t know or care what happened at the server, it just wants a valid response from the server, and this is what the server gives it – an HTML document that has been written dynamically using PHP scripts.
So, in a bit more detail than before, here’s our flow now:
- The user types in a page to the browser
- The browser sends the request for the page to the server
- The server checks to see if it is a valid request
- The server fetches the requested PHP script
- The PHP script is processed
- This results in an HTML response to be sent to the browser
- The response is sent
- The browser displays the new page
Next time: Installing an Apache Server
So there you go! This is, believe it or not, still a very high level picture of what is going on, but it is all you really need to know for now if you are wanting to get going with PHP. If all of this still seems a bit cloudy to you, don’t worry, we will be getting our hands dirty with some code in the next few tutorials, and you will eventually get it. In the very next tutorial, we will be installing an Apache server and configuring it. This is first step to getting our own server set-up going. See you then.