Skip to content

Server-Side PHP

Server-side programming refers to code that runs on the web server rather than in the user’s browser. When a user visits a website, their browser (client) sends a request to the web server, which processes the request using server-side code and sends back a response.

Client-SideServer-Side
Runs in the browserRuns on the web server
Languages: HTML, CSS, JavaScriptLanguages: PHP, Python, Java, Node.js, etc.
Visible to usersHidden from users
Limited securityMore secure
Can’t access databases directlyDirect database access

PHP (PHP: Hypertext Preprocessor) is one of the most popular server-side scripting languages. Here’s how it works:

User Browser → Web Server → PHP Engine → Database (if needed) → Response → User Browser
  1. User requests a PHP page (e.g., example.com/page.php)
  2. Web server locates the PHP file
  3. PHP engine executes the PHP code
  4. PHP generates HTML output
  5. Server sends HTML to user’s browser
  6. Browser displays the final webpage
  • Dynamic Content: PHP can generate different content based on user input, time, database data, etc.
  • Server Processing: All PHP code is processed before reaching the user
  • Security: Source code remains hidden from users
  • Database Integration: Seamless connection to MySQL, PostgreSQL, and other databases

<?php
$username = "John";
$currentTime = date("Y-m-d H:i:s");
echo "<h1>Welcome, $username!</h1>";
echo "<p>Current time: $currentTime</p>";
?>
  • Connect to databases (MySQL, PostgreSQL, SQLite)
  • Perform CRUD operations (Create, Read, Update, Delete)
  • Manage user accounts and data
  • Read and write files on the server
  • Upload and process user files
  • Generate reports and documents
  • Track user login status
  • Store temporary data across page visits
  • Manage shopping carts and user preferences
  • Validate user input
  • Sanitize data for security
  • Send emails and notifications
  • Connect to external services
  • Process payments
  • Integrate with social media platforms

  • Source code is never exposed to users
  • Sensitive operations happen on the server
  • Better protection against client-side attacks
  • Powerful server hardware for processing
  • Optimized for handling multiple requests
  • Efficient database connections
  • Full access to server resources
  • Can perform complex calculations
  • Unlimited processing capabilities
  • Direct database access
  • Server-side data validation
  • Centralized data processing

<?php
// Process user login
if ($_POST['username'] && $_POST['password']) {
// Validate credentials against database
$user = authenticateUser($_POST['username'], $_POST['password']);
if ($user) {
// Start user session
session_start();
$_SESSION['user_id'] = $user['id'];
header('Location: dashboard.php');
}
}
?>
  • WordPress, Drupal use PHP for content management
  • Dynamic page generation based on database content
  • User role management and permissions
  • Form submissions and validation
  • File uploads and processing
  • Report generation and analytics