A modern, full-featured URL shortener built with PHP, MySQL, and Bootstrap 5. Create short links, track clicks, and manage your URLs through a user-friendly dashboard.
- π Quick Shortening - Shorten any URL in seconds
- π€ User Accounts - Register and login to manage your links
- π Dashboard - View stats: total links, total clicks, top link, and quota
- π Link Management - View, copy, and delete your shortened URLs
- π Click Tracking - Track how many times your links are clicked
- βοΈ Settings - Change your username and password
- π Secure - Password hashing with PHP's password_hash()
- βοΈ Email Verification - OTP-based email verification for registration
- π± Responsive Design - Works on desktop and mobile devices
- π Clean URLs - No .php extensions in URLs
- Backend: PHP 8.1+
- Database: MySQL
- Frontend: HTML5, Bootstrap 5, JavaScript
- Server: Apache with mod_rewrite
url-shortener/
βββ .htaccess # URL rewriting rules
βββ index.php # Root redirect
βββ README.md # This file
βββ api/
β βββ shorten.php # URL shortening API
β βββ delete_url.php # URL deletion API
βββ assets/
β βββ css/style.css # Custom styles
β βββ js/script.js # Frontend JavaScript
βββ auth/
β βββ login.php # User login
β βββ logout.php # User logout
β βββ register.php # User registration
β βββ forgot-password.php # Password recovery request
β βββ reset-password.php # Password reset with OTP
β βββ verify-email.php # Email verification
βββ config/
β βββ db.php # Database connection
βββ includes/
β βββ navbar.php # Shared navigation
βββ pages/
β βββ index.php # Home page (public shortener)
β βββ redirect.php # Short URL redirect handler
β βββ terms.php # Terms & Privacy Policy
βββ users/
βββ dashboard.php # User dashboard
βββ settings.php # User settings
- PHP 8.1 or higher
- MySQL 5.7+ or MariaDB 10+
- Apache with mod_rewrite enabled
- Composer (optional, for dependencies)
git clone https://github.com/yourusername/url-shortener.git
cd url-shortenerCreate a MySQL database and import the schema:
# Login to MySQL
mysql -u root -p
# Create database
CREATE DATABASE url;
# Use the database
USE url;
# Create tables (run the SQL from db.sql or manually)-- Users table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
otp VARCHAR(6) DEFAULT NULL,
otp_expire DATETIME DEFAULT NULL,
is_verified TINYINT(1) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- URLs table
CREATE TABLE urls (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT DEFAULT NULL,
long_url TEXT NOT NULL,
short_code VARCHAR(10) NOT NULL UNIQUE,
clicks INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Create index for faster lookups
CREATE INDEX idx_short_code ON urls(short_code);
CREATE INDEX idx_user_id ON urls(user_id);Edit config/db.php with your database credentials:
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'your_password'; // Your MySQL password
$dbName = 'url';Update BASE_URL in config/db.php to match your domain:
define('BASE_URL', 'https://yourdomain.com/');For email verification, configure SMTP settings in your PHP configuration or use a library like PHPMailer. Currently, the system uses a basic mail() function - for production, consider using a transactional email service.
# Ensure config directory is writable for any dynamic configs
chmod 755 config/For local development:
# Using PHP built-in server
php -S localhost:8000
# Or using XAMPP/WAMP, place in htdocs folderVisit http://localhost:8000 (or your configured URL)
- Visit the home page
- Paste your long URL
- Click "Shorten"
- Copy your shortened URL
- Register - Create an account with email verification
- Login - Access your personalized dashboard
- Shorten Links - Use the quick shorten form on your dashboard
- View Stats - See total links, clicks, and top-performing links
- Manage Links - Copy or delete your shortened URLs
- Settings - Update username or change password
The application uses Apache's mod_rewrite for clean URLs:
| URL | Maps To |
|---|---|
/ |
Home page |
/login |
Login page |
/register |
Registration page |
/dashboard |
User dashboard |
/settings |
User settings |
/terms |
Terms & Privacy page |
/abc123 |
Redirect to original URL |
POST /api/shorten.php
Content-Type: application/x-www-form-urlencoded
long_url=https://example.com/very/long/url
Response:
{
"status": "success",
"short_url": "https://localhost/abc123",
"code": "abc123"
}POST /api/delete_url.php
Content-Type: application/x-www-form-urlencoded
id=1
Response:
{
"status": "success",
"message": "URL deleted successfully"
}In api/shorten.php, modify the $length variable:
$length = 6; // Default 6 charactersIn users/dashboard.php, update the quota check:
// Currently set to 100 links
<span class="badge <?php echo $link_count >= 100 ? 'bg-danger' : 'bg-success'; ?>"><?php echo $link_count; ?>/100</span>Edit assets/css/style.css for custom styles. The main customizations include:
- Hero section background
- Card styling
- Shortener box styling
- Button styles
- Passwords are hashed using
password_hash()with PASSWORD_DEFAULT - Input sanitization with
htmlspecialchars()to prevent XSS - Prepared statements for all database queries to prevent SQL injection
- Session-based authentication
- Email verification required for new accounts
- OTP expiration for password reset
- Enable HTTPS - Always use SSL/TLS in production
- Update BASE_URL - Set to your production domain
- Secure Database - Use strong database credentials
- Email Configuration - Set up proper SMTP for emails
- Error Handling - Disable display_errors in production
- File Permissions - Restrict write permissions appropriately
Example production php.ini settings:
display_errors = Off
log_errors = On
error_log = /var/log/php/errors.log- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Bootstrap 5 for the UI framework
- Bootstrap Icons for icons
- PHP for the backend
- MySQL for the database
Made with β€οΈ for the community
