What is Website Scripting?
|
|
So is HTML a scripting (or programming) language? Well no - HTML is a mark-up language that consists of a list of instructions that tell the web browser how to render the page (when to break to a new line, which font to use etc). The instructions are generally processed in the order in which they appear in the list and will produce the same result each time the page is loaded.
A scripting language, on the other hand, is a list of instructions that may not be processed in the order in which they appear and can make decision branches to other instructions in the list depending on the results of conditional statements. Therefore a script may not produce the same result each time the page is loaded and this is what makes scripting so powerful since it can produce different page content depending on how a user interacts with the website.
Client Side Scripting and Server Side ScriptingScripts can be divided into these two categories and the difference is that a client side script runs in the web browser (this is the client side) after the page is downloaded and a server side script runs on the web server (this is the server side) before the page is downloaded. There are different client side and server side scripting languages but the two most common are JavaScript (that runs client side) and PHP (that runs server side). JavaScript can actually be run server side but it's not that common and is normally considered as a client side language.Note that HTML, JavaScript and PHP are all actually written as simple text files but saved with different file extensions to allow the browser or web server to differentiate between them. |
How Web Pages are Served
To better understand how scripts run a quick look at how web pages are served up to the browser will help. If you click on a link in a web page or email or you type in a web address into your browser address bar your web browser constructs a request that contains the information in the link or address you typed in. The request holds a lot of information but the two pieces we're interested in are the name of the resource requested and where the resource is located.A resource is actually a file held on a remote server and may be an HTML page, a server side script, a graphic, a Flash movie or any of the other files types commonly used on the web. Where the resource is located is the name of the remote server - usually a web address that is actually an alias for a unique machine number.
When your ISP receives the request it looks up the domain name and converts it to its machine number (if it exists) and passes it on until it finally reaches the required web server. The web server "listens" on one of its ports for requests and when it receives one it processes it. It checks to see if it has a copy of the requested resource and if it doesn't it returns a not found error (a 404 error) to the sender of the request. If it does have the resource and it's an HTML file or any other type of resource that requires it to take no further action, it returns a copy of it to the sender of the request whereupon the web browser reads the file and renders the page according to the instructions contained in the file.
Serving HTML and PHP files
The web server knows which requests do not require further action and which do by reading the extension of the file requested. So for example if the requested file is called mypage.htm or graphic.gif it knows that .htm and .gif require no further action on its part. On the other hand if the requested file is called mypage.php it knows that this file requires further action before it is passed back.When the server encounters a PHP file extension it passes it to the PHP engine that runs on the server. A PHP file is a text file that contains PHP instructions and typically these instructions will be embedded within HTML instructions. The PHP engine ignores the HTML but carries out the PHP instructions (which may actually write HTML instructions directly into the page) then strips out all the PHP before returning the file to the web server. The server then treats the file as any other HTML file and returns it to the sender of the request.
JavaScript is also embedded within HTML instructions or may be written as a separate JavaScript file (with a .js file extension) for inclusion within an HTML page but the server regards these files as requiring no further action and passes them back for the browser to handle.
The Difference Between JavaScript and PHP
JavaScript and PHP are actually very similar in form and can perform similar functions and are both embedded within HTML instructions. However the crucial difference is that PHP instructions are run on the server and typically used to rewrite the HTML within the file before being stripped out before reaching the browser. JavaScript on the other hand is not run on the server but run in the browser and this makes it much less powerful and much less secure, since all instructions within the file are available to be read by a user and altered.So although it would be possible to write a shopping cart script using JavaScript, it wouldn't be very secure since a user could alter prices within a page for example then submit the altered page. A user on the other hand cannot alter PHP since the instructions are stripped out before the page is served.
Why Use Scripts?
Scripts add functionality to a web site in a way that HTML can't. Both JavaScript and PHP can do this but in different ways. To take a simple example - suppose we want an opening page that greets the visitor with a message that says, "Good Morning - it's Friday" (or an appropriate message depending on time and day).Both JavaScript and PHP can do this. With JavaScript we can read the time on the user's computer so figure out whether it's morning, afternoon or evening and which day it is. We can do the same with PHP but these variables will be read from the server not from the user's computer.
In the case of JavaScript all the possible messages that could be printed out must be written into the page and the browser must decide which one to output. In the case of PHP all possible messages don't need to be written into the page, only the required one, since the instructions that make the decision are run on the server then stripped out.
This example is trivial and it wouldn't make a great deal of difference whether JavaScript or PHP were used here. However, a more complex example might require that a completely different page be output depending on time and day. With JavaScript this wouldn't be practical since it would result in a huge page file. With PHP only the required page would be returned. In fact PHP can create unique pages assembled from prewritten content depending on user interaction. This is usually called creating pages "on the fly" and becomes particularly powerful if PHP is used to read from and write to on an online database such as MySQL server.
© 2000-2008 smallbizonline website design Tel: 01501 771106 Privacy Policy Terms & Conditions
