PHP Projects for UG Students with Source Code
PHP Projects for UG Students with Source Code
Hey there, UG students! So, you’re diving into the world of PHP, huh? PHP, or Hypertext Preprocessor, is like that reliable friend who’s been around forever, powering websites like nobody’s business. If you’re a computer science student, IT enthusiast, or just someone curious about web development, PHP is a fantastic place to start. It’s easy to pick up, works seamlessly with HTML, and runs half the internet—think WordPress, Facebook, and tons of other sites. In this blog post, we’re going to explore a bunch of PHP projects that are perfect for undergraduate students like you. And guess what? We’re including source code for each one, so you can jump right in and start coding!
We’ll cover projects from beginner to intermediate levels, so whether you’re just starting or already know a bit of PHP, there’s something here for you. so buckle up for a mega-detailed ride through PHP projects that’ll help you learn, experiment, and maybe even impress your professors or land that dream internship!
Why PHP for UG Students?
Before we dive into the projects, let’s talk about why PHP is such a big deal. PHP is like the dal-chawal of web development—simple, versatile, and loved by everyone. Here’s why it’s perfect for undergrads:
- Easy to Learn: PHP’s syntax is straightforward, especially if you know a bit of C or Java. It’s embedded in HTML, so you’re already halfway there if you know some web basics.
- Huge Ecosystem: PHP powers over 70% of websites, and tools like WordPress, Laravel, and CodeIgniter make it super flexible.
- Job Opportunities: From startups in Bengaluru to MNCs in Gurugram, PHP developers are in demand. Learning PHP can open doors to web dev roles.
- Community Support: Stuck on a bug? The PHP community on Stack Overflow, X, or GitHub is always ready to help.
Alright, enough gyaan. Let’s get to the projects! We’ll start with beginner-friendly ones and move to intermediate ones. Each project comes with a description, why it’s useful, and the source code (properly encoded in <pre><code>
tags for best practices). Ready? Chalo, let’s start!
Beginner-Level PHP Projects
These projects are perfect if you’re new to PHP. They’ll help you understand the basics like variables, forms, loops, and database connections.
Project 1: Simple Login System
What’s This About?
A login system is like the gatekeeper of a website. It lets users log in with a username and password. This project teaches you how to handle forms, validate input, and connect to a database.
Why It’s Cool:
- You learn about form handling in PHP.
- It introduces MySQL database integration.
- It’s a core feature of most web apps.
Source Code:
index.php
<?php
session_start();
if (isset($_SESSION['user'])) {
header("Location: welcome.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login System</title>
</head>
<body>
<h2>Login</h2>
<form action="login.php" method="POST">
<label>Username:</label>
<input type="text" name="username" required><br><br>
<label>Password:</label>
<input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
login.php
<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "login_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
$_SESSION['user'] = $username;
header("Location: welcome.php");
} else {
echo "Invalid username or password!";
}
mysqli_close($conn);
?>
welcome.php
<?php
session_start();
if (!isset($_SESSION['user'])) {
header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome, <?php echo $_SESSION['user']; ?>!</h2>
<a href="logout.php">Logout</a>
</body>
</html>
logout.php
<?php
session_start();
session_destroy();
header("Location: index.php");
?>
Database Setup (SQL):
CREATE DATABASE login_db;
USE login_db;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL
);
INSERT INTO users (username, password) VALUES ('testuser', 'testpass');
How It Works:
This project creates a simple login system. The index.php
shows a login form, login.php
checks the credentials against a MySQL database, welcome.php
displays a welcome message, and logout.php
ends the session. You’ll need to set up a MySQL database and install a server like XAMPP.
What You Learn:
- Handling forms with
$_POST
. - Using sessions with
session_start()
. - Connecting to MySQL with
mysqli
.
Try This: Add password hashing with password_hash()
and password_verify()
for security. You could also add a registration form.
Note: Install XAMPP, start Apache and MySQL, and create the database using phpMyAdmin. Never store plain passwords in production!
Project 2: To-Do List Application
What’s This About?
A to-do list app lets users add, view, and delete tasks. It’s like a digital notebook for your assignments or project deadlines.
Why It’s Cool:
- You learn about arrays and form handling.
- It introduces CRUD (Create, Read, Update, Delete) operations.
- It’s a practical tool you can use daily.
Source Code:
index.php
<?php
$conn = mysqli_connect("localhost", "root", "", "todo_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['add_task'])) {
$task = mysqli_real_escape_string($conn, $_POST['task']);
$sql = "INSERT INTO tasks (task) VALUES ('$task')";
mysqli_query($conn, $sql);
}
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$sql = "DELETE FROM tasks WHERE id=$id";
mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>To-Do List</title>
</head>
<body>
<h2>To-Do List</h2>
<form action="" method="POST">
<input type="text" name="task" required>
<input type="submit" name="add_task" value="Add Task">
</form>
<h3>Tasks:</h3>
<ul>
<?php
$sql = "SELECT * FROM tasks";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<li>" . $row['task'] . " <a href='?delete=" . $row['id'] . "'>Delete</a></li>";
}
?>
</ul>
</body>
</html>
<?php mysqli_close($conn); ?>
Database Setup (SQL):
CREATE DATABASE todo_db;
USE todo_db;
CREATE TABLE tasks (
id INT AUTO_INCREMENT PRIMARY KEY,
task VARCHAR(255) NOT NULL
);
How It Works:
The app connects to a MySQL database to store tasks. Users can add tasks via a form, view them in a list, and delete tasks by clicking a link. The code uses mysqli
for database operations.
What You Learn:
- CRUD operations with MySQL.
- Form handling and URL parameters (
$_GET
). - Basic HTML integration with PHP.
Try This: Add an edit feature to update tasks. You could also add CSS to make it look nicer or a “mark as completed” option.
Note: Set up the database in phpMyAdmin and ensure XAMPP is running.
Project 3: Simple Contact Form
What’s This About?
A contact form lets users send messages to a website admin. This project stores form submissions in a database and displays a confirmation.
Why It’s Cool:
- You learn about form validation.
- It’s a common feature in real websites.
- It teaches you how to handle user input securely.
Source Code:
contact.php
<?php
$conn = mysqli_connect("localhost", "root", "", "contact_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['submit'])) {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
if (mysqli_query($conn, $sql)) {
echo "Message sent successfully!";
} else {
echo "Error: " . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<h2>Contact Us</h2>
<form action="" method="POST">
<label>Name:</label><br>
<input type="text" name="name" required><br><br>
<label>Email:</label><br>
<input type="email" name="email" required><br><br>
<label>Message:</label><br>
<textarea name="message" required></textarea><br><br>
<input type="submit" name="submit" value="Send">
</form>
</body>
</html>
<?php mysqli_close($conn); ?>
Database Setup (SQL):
CREATE DATABASE contact_db;
USE contact_db;
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
message TEXT NOT NULL
);
How It Works:
The form collects name, email, and message, stores them in a MySQL database, and shows a success message. The mysqli_real_escape_string
function prevents SQL injection.
What You Learn:
- Form validation and submission.
- Secure database queries.
- HTML forms with PHP backend.
Try This: Add email validation or send an email to the admin using mail()
. You could also create an admin page to view all messages.
Intermediate-Level PHP Projects
Now let’s level up with some intermediate projects that involve more complex logic, sessions, or APIs.
Project 4: Online Quiz System
What’s This About?
This project creates a quiz where users answer multiple-choice questions, and their score is calculated at the end. It’s like those online tests you take for placements.
Why It’s Cool:
- You learn about session management for tracking answers.
- It involves dynamic content generation.
- It’s a great portfolio project for web dev roles.
Source Code:
quiz.php
<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "quiz_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$questions = [
1 => ["question" => "What is the capital of India?", "options" => ["Delhi", "Mumbai", "Kolkata", "Chennai"], "answer" => "Delhi"],
2 => ["question" => "Which is the largest planet?", "options" => ["Earth", "Jupiter", "Mars", "Venus"], "answer" => "Jupiter"]
];
if (!isset($_SESSION['score'])) {
$_SESSION['score'] = 0;
$_SESSION['question'] = 1;
}
if (isset($_POST['answer'])) {
$qid = $_SESSION['question'];
$user_answer = $_POST['answer'];
if ($user_answer == $questions[$qid]['answer']) {
$_SESSION['score'] += 1;
}
$_SESSION['question'] += 1;
}
if ($_SESSION['question'] > count($questions)) {
echo "<h2>Quiz Over! Your Score: " . $_SESSION['score'] . "/" . count($questions) . "</h2>";
echo "<a href='quiz.php?reset=1'>Restart Quiz</a>";
session_destroy();
exit();
}
if (isset($_GET['reset'])) {
session_destroy();
header("Location: quiz.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Online Quiz</title>
</head>
<body>
<h2>Question <?php echo $_SESSION['question']; ?></h2>
<p><?php echo $questions[$_SESSION['question']]['question']; ?></p>
<form action="" method="POST">
<?php
foreach ($questions[$_SESSION['question']]['options'] as $option) {
echo "<input type='radio' name='answer' value='$option' required> $option<br>";
}
?>
<br><input type="submit" value="Submit">
</form>
</body>
</html>
<?php mysqli_close($conn); ?>
How It Works:
The quiz displays one question at a time, tracks the user’s score using sessions, and shows the final score when done. Questions are stored in a PHP array for simplicity, but you can move them to a database.
What You Learn:
- Session management for state tracking.
- Dynamic form generation.
- Basic quiz logic.
Try This: Store questions in a MySQL database. Add a timer for each question or a leaderboard for top scores.
Project 5: Simple Blog System
What’s This About?
A blog system lets users create, view, and delete posts. It’s like a mini WordPress, perfect for learning full-stack development.
Why It’s Cool:
- You learn about CRUD with a database.
- It involves user authentication.
- It’s a real-world project for portfolios.
Source Code:
index.php
<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "blog_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple Blog</title>
</head>
<body>
<h2>Blog Posts</h2>
<?php if (isset($_SESSION['user'])): ?>
<p>Welcome, <?php echo $_SESSION['user']; ?>! <a href="logout.php">Logout</a></p>
<form action="post.php" method="POST">
<label>Title:</label><br>
<input type="text" name="title" required><br><br>
<label>Content:</label><br>
<textarea name="content" required></textarea><br><br>
<input type="submit" value="Post">
</form>
<?php else: ?>
<p><a href="login.php">Login</a> to create posts.</p>
<?php endif; ?>
<h3>Recent Posts:</h3>
<?php
$sql = "SELECT * FROM posts ORDER BY created_at DESC";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<h4>" . $row['title'] . "</h4>";
echo "<p>" . $row['content'] . "</p>";
if (isset($_SESSION['user']) && $_SESSION['user'] == $row['author']) {
echo "<a href='delete.php?id=" . $row['id'] . "'>Delete</a>";
}
echo "<hr>";
}
?>
</body>
</html>
<?php mysqli_close($conn); ?>
login.php
<?php
session_start();
if (isset($_SESSION['user'])) {
header("Location: index.php");
exit();
}
$conn = mysqli_connect("localhost", "root", "", "blog_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['login'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
$_SESSION['user'] = $username;
header("Location: index.php");
} else {
echo "Invalid credentials!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="" method="POST">
<label>Username:</label><br>
<input type="text" name="username" required><br><br>
<label>Password:</label><br>
<input type="password" name="password" required><br><br>
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
<?php mysqli_close($conn); ?>
post.php
<?php
session_start();
if (!isset($_SESSION['user'])) {
header("Location: login.php");
exit();
}
$conn = mysqli_connect("localhost", "root", "", "blog_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$title = mysqli_real_escape_string($conn, $_POST['title']);
$content = mysqli_real_escape_string($conn, $_POST['content']);
$author = $_SESSION['user'];
$sql = "INSERT INTO posts (title, content, author) VALUES ('$title', '$content', '$author')";
mysqli_query($conn, $sql);
header("Location: index.php");
mysqli_close($conn);
?>
delete.php
<?php
session_start();
if (!isset($_SESSION['user'])) {
header("Location: login.php");
exit();
}
$conn = mysqli_connect("localhost", "root", "", "blog_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$id = $_GET['id'];
$sql = "DELETE FROM posts WHERE id=$id AND author='{$_SESSION['user']}'";
mysqli_query($conn, $sql);
header("Location: index.php");
mysqli_close($conn);
?>
logout.php
<?php
session_start();
session_destroy();
header("Location: login.php");
?>
Database Setup (SQL):
CREATE DATABASE blog_db;
USE blog_db;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL
);
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
author VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO users (username, password) VALUES ('admin', 'admin123');
How It Works:
Users must log in to create or delete posts. Posts are stored in a MySQL database and displayed on the homepage. Only the post author can delete their posts.
What You Learn:
- Full-stack development with PHP, MySQL, and HTML.
- User authentication and authorization.
- CRUD operations with multiple tables.
Try This: Add post editing, categories, or a rich text editor for content. You could also add CSS or Bootstrap for styling.
Project 6: Weather App Using API
What’s This About?
This project fetches real-time weather data for a city using an API like OpenWeatherMap. Users enter a city name and see the weather details.
Why It’s Cool:
- You learn about API integration with PHP.
- It introduces JSON parsing with
json_decode
. - It’s a practical, real-world project.
Source Code:
weather.php
<?php
if (isset($_POST['city'])) {
$api_key = "your_api_key_here"; // Replace with OpenWeatherMap API key
$city = urlencode($_POST['city']);
$url = "http://api.openweathermap.org/data/2.5/weather?q=$city&appid=$api_key&units=metric";
$response = file_get_contents($url);
$data = json_decode($response, true);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
</head>
<body>
<h2>Weather App</h2>
<form action="" method="POST">
<label>Enter City:</label>
<input type="text" name="city" required>
<input type="submit" value="Get Weather">
</form>
<?php
if (isset($data) && $data['cod'] == 200) {
echo "<h3>Weather in " . $data['name'] . "</h3>";
echo "<p>Description: " . $data['weather'][0]['description'] . "</p>";
echo "<p>Temperature: " . $data['main']['temp'] . "°C</p>";
echo "<p>Humidity: " . $data['main']['humidity'] . "%</p>";
} elseif (isset($data)) {
echo "<p>City not found!</p>";
}
?>
</body>
</html>
How It Works:
The form takes a city name, sends a request to the OpenWeatherMap API, and displays the weather details using file_get_contents
and json_decode
.
What You Learn:
- Making API requests in PHP.
- Parsing JSON data.
- Handling form input and errors.
Try This: Add more weather details like wind speed or a 5-day forecast. You could also use cURL instead of file_get_contents
for better control.
Note: Sign up for a free API key at OpenWeatherMap. Ensure allow_url_fopen
is enabled in your PHP configuration.
Wrapping Up
These six projects—Login System, To-Do List, Contact Form, Online Quiz, Blog System, and Weather App—cover a wide range of PHP skills, from basic form handling to API integration and full-stack development. Each project includes source code wrapped in <pre><code>
tags for best practices, ready for you to copy, run, and tweak.
Here’s what you can do next:
- Experiment: Add new features like styling with Bootstrap or security with prepared statements.
- Build a Portfolio: Host these projects on GitHub or a free hosting service like 000webhost to show off to recruiters.
- Learn More: Explore frameworks like Laravel or CodeIgniter for advanced PHP development.
PHP is like a trusty scooter—reliable, widely used, and gets you where you need to go. Keep coding, keep learning, and soon you’ll be building websites that’ll make your friends say, “Arre, yeh toh ekdum jhakaas hai!” If you need more project ideas or help with debugging, check out communities on X or Stack Overflow.
Happy coding, and don’t forget to grab some chai and samosas during your breaks!