OCR H446, section 1.3.4 (d)
When you use a website, some of the work is done on the web server, and some is done in your browser. Knowing where code runs matters for speed, security and what is possible.
Client-side processing
Client-side code is sent to the browser and runs on the user's own device.
- The usual language is JavaScript (with HTML and CSS controlling structure and style, which the browser also processes).
- The browser downloads the code and runs it, without going back to the server for each action.
Typical uses:
- Validating form input before it is sent (is the email address in the right form? is the field empty?).
- Making the page interactive: menus, image sliders, pop-ups, animations.
- Updating part of the page without reloading it.
- Simple calculations and games running in the page.
- Reacting to mouse clicks and key presses.
Advantages
- Fast response, since there's no network delay for each action.
- Reduces the load on the server, which has less to do, so it can handle more users.
- Less data is sent over the network.
- Works even with a slow connection once the page has loaded (and sometimes offline).
Disadvantages
- The code and data are visible to the user, who can read and change it, so it can't be trusted for anything important.
- Not secure: sensitive logic or secrets must never rely on it.
- It depends on the user's device and browser: it may behave differently, and the user can turn JavaScript off.
- Can't access the server's database directly.
- Powerful processing is limited by the power of the user's device.
Server-side processing
Server-side code runs on the web server. The user's browser sends a request, the server runs a program, and sends back the result, usually an HTML page. The user never sees the code itself.
- Languages include PHP, Python, Java, C# and JavaScript running on a server (Node.js).
Typical uses:
- Accessing and updating a database (looking up products, saving orders).
- User authentication: checking passwords and logins.
- Generating pages from data: for example a page that is different for each user.
- Processing payments and other sensitive operations.
- Sending emails, and handling file uploads.
- Re-validating the data that was sent.
Advantages
- More secure: the code and sensitive data (such as the database and passwords) stay on the server, and are hidden from the user.
- Can access databases and other server resources.
- It runs the same way for everyone, whatever the user's device or browser.
- Can do heavy processing with the server's greater power.
Disadvantages
- Slower response: each action needs a round trip across the network.
- Puts load on the server, so a busy site needs more or stronger servers, which costs more.
- Needs a network connection.
- More data is sent across the network.
Comparison
| Client-side | Server-side | |
|---|---|---|
| Runs on | The user's device (browser) | The web server |
| Typical language | JavaScript | PHP, Python, Java, C#, Node.js |
| Code visible to the user? | Yes | No |
| Speed of response | Fast (no network trip) | Slower (needs a request) |
| Server load | Low | Higher |
| Security | Weak, can't be trusted | Strong |
| Database access | No (directly) | Yes |
| Depends on the user's browser? | Yes | No |
Using both together
Most websites combine them, using each for what it does best.
Example: a registration form.
- Client-side JavaScript checks the form as the user types: a presence check and a format check on the email. The user gets instant feedback, and the server isn't bothered by obvious errors.
- When submitted, the data is sent to the server.
- The server-side code checks (validates) it again, because the client-side check can be bypassed. It checks whether the username is free in the database, stores the password as a hash, and saves the new account.
- The server sends back a confirmation page.
Never rely on client-side validation alone for security. A user can switch off JavaScript or send data directly, so the server must always re-check, which also stops malicious input (such as SQL injection) reaching the database.
Worked example
Task: for each task, say whether client-side or server-side processing is better, and why.
- Showing an error as soon as a user leaves an empty "name" box. Client-side: immediate feedback, with no wait for the server.
- Checking a customer's password against the stored data. Server-side: the password data must be kept secret on the server, and can't be left to the browser.
- Displaying a photo slideshow that changes every few seconds. Client-side: the browser can do it without any network traffic, reducing the load on the server.
- Searching 1 million products stored in a database for a keyword. Server-side: the data is on the server, which can search it efficiently.
- Sending the HTML of a page when it is requested. Server-side: the page's HTML is stored on (or generated by) the server, which sends it to the browser.
- Applying the CSS styles to a page. Client-side: the browser reads the CSS and works out how each element should look.
Exam tip: state where the code runs, then give a reason based on speed, security or access to data. Remember that client-side checks can't be trusted, so sensitive actions and validation must be done again on the server.
Check yourself
What is the difference between client-side and server-side processing?
Client-side code runs in the user's browser on their own device. Server-side code runs on the web server, and only the result is sent to the user.
Give two advantages of client-side processing
Any two of: fast response because there is no network delay; reduces the load on the server; less data sent over the network.
Why must data validated on the client also be validated on the server?
The client-side code is visible and can be changed or bypassed by the user, so it can't be trusted. The server must check again to stay secure.
Give one task that must be done server-side
Any one of: accessing a database; checking passwords; processing payments; storing user data securely.