Server-side scripting

From Higher Computing Science
Jump to: navigation, search

Key points

  • Server-side scripts are scripts that run on a server. The result of the scripts is sent from the server to the user. For example, a server side script could be used to locate information about your user account on a website, in order to send an HTML page containing a user's profile.
  • Server-side scripting allows web developers to deliver dynamic web pages. When a page is requested, the script can look up information, search a database, or respond to input to provide a different response each time.
  • Common server-side scripting languages include python, PHP, Ruby, Perl and ASP.

Information

To understand server-side scripting, it is important to understand how web pages are requested and delivered. Web pages can either be static or dynamic. When a static web page is requested, the same copy is sent for every request. When a dynamic web page is requested, the server will customise the webpage using server-side scripting, before sending the result back to the user.

Examples of server-side scripting

Example 1

The following code is a very simple example of server side scripting written in the language PHP. It displays a very simple web page that has a random number between 1 and 10 as the H1 header. When this web page is requested, the user will see a random number as the header. If they refreshed the page, the number could change.

<html>
   <body>
     <h1>
      <?php echo rand(1,10); ?>
     </h1>
   </body>
 </html>

It is important to remember that no code has been run in the browser. The browser has requested the page, and the server has run the PHP code, replacing <?php echo rand(1,10); ?> with a random number between 1 and 10. This means that the HTML the browser receives could be

<html>
   <body>
     <h1>
      7
     </h1>
   </body>
 </html>

or

<html>
   <body>
     <h1>
      3
     </h1>
   </body>
 </html>

The browser should never receive any of the PHP code, only the output.

Example 2

The following code example is a PHP script. It will check a shopping discount code and allow access to a sale page if the code is correct. The script will output one response if the code is correct, and another if it is wrong. The code was supplied by a form with an input box. Try out this example here

<html>
<head>
<title>Sale code access!</title>
</head>
<body>
<?php
if($_GET['code']=="MAGICDUCKS") {
?>Yay! Let's go shopping!
<a href="sale.php">Click here to go the secret sale</a>
<?
} else {
?>
You didn't have the code. Better luck next year!
<?
}
?>
</body>
</html>
A common setup for server-side scripting with PHP

Steps taken when a dynamic web page is requested

  1. The user activates a request by clicking a link, typing in a URL or submitting a form.
  2. The request is sent via the Internet to the HTTP server.
  3. The HTTP server will look for the requested file in its file directory.
    1. If the file is not a script:
      1. Send the file back to the browser to be used.
    2. If the file is a script:
      1. Evaluate the script, carrying out any database or file operations.
      2. Send the output of the script to the HTTP server.
    3. Send any response back to the browser via the Internet
  4. The browser renders the result on screen

Videos

Further information

Test yourself

Teaching resources