AgriTech Web Application Documentation 🌱
This documentation details the main code files of the AgriTech web application. It covers file responsibilities, code flow, user interactions, and underlying database connections. The project aims to provide a modern agricultural e-commerce and knowledge-sharing platform.
about.php
This file renders the "About" page, introducing AgriTech's mission and vision. It includes the navigation bar and utilizes the global styles.
Key Features:
- Static informational content about AgriTech.
- Link to required navigation sections (Home, About, Articles, Shop, Contact).
- Utilizes the shared CSS for consistent styling.
<?php require_once __DIR__ . '/../config/db.php'; ?>
<!-- HTML content for About page follows -->
Purpose:
- Educate visitors about the platform.
- Allow easy navigation to other site sections.
article.php
Displays a single article based on the provided article ID in the URL.
Key Features:
- Fetches article data from the database using the
idGET parameter. - Redirects to articles index if the article is not found.
- Escapes all outputs for security.
- Shows article title, publish date, and content.
$id = intval($_GET['id']);
$sql = "SELECT * FROM articles WHERE id=$id";
$result = $conn->query($sql);
// Output article or redirect if not found
Purpose:
- Present detailed information for a specific article in a clean format.
index.php (Articles Directory)
Lists all agricultural articles as cards with previews.
Key Features:
- Pulls all articles from the database, ordered by creation date.
- Shows a thumbnail, truncated content, and a "Read More" link.
- Handles empty article lists gracefully.
$sql = "SELECT * FROM articles ORDER BY created_at DESC";
$result = $conn->query($sql);
// Loop through articles and render each as a card
Purpose:
- Serve as the articles home/index page for users to discover content.
style.css
This is the global stylesheet that maintains consistent design and layout across the application.
Highlights:
- Responsive containers and navigation bar.
- Button styles for primary, secondary, and small variants.
- Grid layouts for products and articles.
- Form styling for authentication and contact.
- Special visual cues for errors and empty states.
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #ffffff;
}
.navbar { background: #27ae60; color: white; }
.btn, .btn-sm { background: #27ae60; color: white; }
...
Purpose:
- Enforce brand identity and professional appearance.
- Enhance user experience with visually distinctive elements.
login.php
This file presents the login form to users.
Key Features:
- Collects email and password via a POST form.
- Redirects submission to
login_process.php. - Provides a link to the sign-up page.
<form method="POST" action="login_process.php">
<input type="email" name="email" ...>
<input type="password" name="password" ...>
<button type="submit" class="btn">Login</button>
</form>
Purpose:
- Authenticate users and provide access to user-specific features.
Login Processing Endpoint (login_process.php)
Handles user authentication. Validates email, checks password, and manages session state.
API: User Login
{
"title": "User Login",
"description": "Authenticates a user with email and password.",
"method": "POST",
"baseUrl": "https://agritech.example.com",
"endpoint": "/auth/login_process.php",
"headers": [],
"queryParams": [],
"pathParams": [],
"bodyType": "form",
"requestBody": "",
"formData": [
{
"key": "email",
"value": "User's email address",
"required": true
},
{
"key": "password",
"value": "User's password",
"required": true
}
],
"rawBody": "",
"responses": {
"200": {
"description": "Login successful, redirects to home",
"body": "Redirect to /index.php"
},
"400": {
"description": "Invalid credentials",
"body": "<p class='error'>Invalid password!</p>"
},
"404": {
"description": "User not found",
"body": "<p class='error'>User not found!</p>"
}
}
}
Purpose:
- Authenticate users and initiate sessions.
- Redirect on success, show error on failure.
logout.php
Logs out the current user by destroying the session and redirecting to the homepage.
session_destroy();
header("Location: ../index.php");
exit();
Purpose:
- Enforce secure logout and session cleanup.
signup.php
Displays the registration form for new users.
Key Features:
- Collects username, email, password, and password confirmation.
- Submits data to
signup_process.php. - Offers a link to the login page.
<form method="POST" action="signup_process.php">
<!-- Username, Email, Password, Confirm Password -->
</form>
Purpose:
- Grow the user base by allowing account creation.
Signup Processing Endpoint (signup_process.php)
Handles user registration, password hashing, error checking, and redirects.
API: User Registration
{
"title": "User Registration",
"description": "Registers a new user account.",
"method": "POST",
"baseUrl": "https://agritech.example.com",
"endpoint": "/auth/signup_process.php",
"headers": [],
"queryParams": [],
"pathParams": [],
"bodyType": "form",
"requestBody": "",
"formData": [
{
"key": "username",
"value": "Desired username",
"required": true
},
{
"key": "email",
"value": "User's email address",
"required": true
},
{
"key": "password",
"value": "User's password",
"required": true
},
{
"key": "confirm_password",
"value": "Password confirmation",
"required": true
}
],
"rawBody": "",
"responses": {
"200": {
"description": "Signup successful, redirect to login",
"body": "Redirect to /auth/login.php?success=1"
},
"400": {
"description": "Passwords do not match",
"body": "<p class='error'>Passwords do not match!</p>"
},
"409": {
"description": "Email already exists",
"body": "<p class='error'>Email already exists!</p>"
}
}
}
Purpose:
- Securely create new user accounts and handle common errors.
db.php
Centralized database connection and session management.
Key Features:
- Defines DB connection constants (host, user, pass, db name).
- Initializes mysqli connection and sets UTF-8 charset.
- Ensures a PHP session is started.
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'agritech_db');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// session_start();
Purpose:
- Provide a reusable, consistent DB connection across all backend scripts.
Database Schema Overview
erDiagram
USERS {
int id
string username
string email
string password
}
ARTICLES {
int id
string title
string content
date created_at
}
PRODUCTS {
int id
string name
string description
string image_url
decimal price
int stock
}
ORDERS {
int id
int user_id
decimal total_amount
string status
}
ORDER_ITEMS {
int id
int order_id
int product_id
int quantity
decimal price
}
CONTACTS {
int id
string name
string email
string message
}
USERS ||--o{ ORDERS : places
ORDERS ||--|{ ORDER_ITEMS : contains
PRODUCTS ||--|{ ORDER_ITEMS : included_in
contact.php
Displays a contact form and saves messages in the contacts table.
API: Contact Submission
{
"title": "Contact Submission",
"description": "Submits a contact message.",
"method": "POST",
"baseUrl": "https://agritech.example.com",
"endpoint": "/contact/contact.php",
"headers": [],
"queryParams": [],
"pathParams": [],
"bodyType": "form",
"requestBody": "",
"formData": [
{
"key": "name",
"value": "Sender's name",
"required": true
},
{
"key": "email",
"value": "Sender's email",
"required": true
},
{
"key": "message",
"value": "Message content",
"required": true
}
],
"rawBody": "",
"responses": {
"200": {
"description": "Message sent successfully",
"body": "<p class='success'>Message sent successfully!</p>"
}
}
}
Purpose:
- Allow users to get in touch with the AgriTech team.
cart.php
Shows the current user's shopping cart and enables product removal.
Key Features:
- Lists products, prices, quantities, totals.
- Allows item removal (via GET parameter).
- Displays overall cart total.
- Provides a checkout button.
foreach ($_SESSION['cart'] as $product_id => $quantity) {
// Fetch product, calculate subtotal
}
if (isset($_GET['remove'])) {
unset($_SESSION['cart'][$remove_id]);
header("Location: cart.php");
exit();
}
Purpose:
- Facilitate shopping cart management.
checkout.php
Processes order placement and collects shipping/payment info.
Key Features:
- Checks if user is logged in and cart is not empty.
- On POST, creates an order and order items in the database.
- Clears cart and shows success message with order ID.
API: Checkout (Order Placement)
{
"title": "Order Placement",
"description": "Submits a new order and order items.",
"method": "POST",
"baseUrl": "https://agritech.example.com",
"endpoint": "/shop/checkout.php",
"headers": [],
"queryParams": [],
"pathParams": [],
"bodyType": "form",
"requestBody": "",
"formData": [
{
"key": "address",
"value": "Shipping address",
"required": true
},
{
"key": "city",
"value": "Shipping city",
"required": true
},
{
"key": "postal_code",
"value": "Postal code",
"required": true
},
{
"key": "card_number",
"value": "Credit card number",
"required": true
},
{
"key": "card_holder",
"value": "Card holder name",
"required": true
}
],
"rawBody": "",
"responses": {
"200": {
"description": "Order placed successfully",
"body": "<p class='success'>Order placed successfully! Order ID: ...</p>"
},
"400": {
"description": "Invalid data or not logged in",
"body": "Redirect to signup/login"
}
}
}
Purpose:
- Complete the purchase workflow and persist order data.
index.php (Shop Directory)
Displays all available products in a grid, with links to product details.
Key Features:
- Fetches products from the database.
- Renders cards with images, names, descriptions, and prices.
- Provides "View Details" links for each product.
$sql = "SELECT * FROM products";
$result = $conn->query($sql);
// Loop through and display each product
Purpose:
- Serve as the main shop page for product discovery.
product.php
Shows a single product's details and allows adding it to the cart.
Key Features:
- Fetches product by ID from the database.
- Displays name, description, image, price, and stock.
- Provides a form for adding a specific quantity to the cart.
- Redirects to cart after successful addition.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$quantity = intval($_POST['quantity']);
if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); }
$_SESSION['cart'][$id] = $quantity;
header("Location: cart.php");
exit();
}
Purpose:
- Allow detailed product exploration and seamless cart addition.
index.php (Main Site Homepage)
Acts as the homepage, introducing AgriTech and featuring articles and navigation.
Key Features:
- Welcomes visitors and introduces the platform's mission.
- Showcases featured articles (latest 3, with titles and snippets).
- Provides navigation for signing up, logging in, and site exploration.
Homepage Architecture Flow
flowchart TD
A[Visitor lands on index.php]
B[Navigation bar renders]
C[Hero section displays welcome]
D[Featured articles are queried from DB]
E[Loop: Each featured article is shown]
F[Footer renders]
A --> B --> C --> D --> E --> F
Purpose:
- Serve as the entry point for all users and introduce key site features.
{
"title": "Security Best Practices",
"content": "Always validate and escape user input to prevent SQL injection and XSS attacks throughout the application."
}
{
"title": "Session Management",
"content": "Sessions must be started before accessing session variables like the cart or user_id."
}
Summary Table
| File | Responsibility | Output Type |
|---|---|---|
| about.php | About page (info) | HTML |
| article.php | Single article display | HTML |
| articles/index.php | Article listing | HTML |
| style.css | Application-wide styles | CSS |
| login.php | Login form | HTML |
| login_process.php | Login processing/auth | HTML/Redirect |
| logout.php | Session logout | Redirect |
| signup.php | Registration form | HTML |
| signup_process.php | Registration processing | HTML/Redirect |
| db.php | DB connection & session setup | PHP |
| contact.php | Contact form & submission | HTML |
| shop/cart.php | Shopping cart | HTML |
| shop/checkout.php | Checkout & order placement | HTML |
| shop/index.php | Product listing | HTML |
| shop/product.php | Product details & add to cart | HTML |
| index.php | Main homepage | HTML |
This documentation should help developers, maintainers, and stakeholders quickly understand the intent, flow, and responsibilities of each core file in the AgriTech platform.