urlnew mehthod

CREATE DATABASE url_shortener;
USE url_shortener;

CREATE TABLE links (
id INT AUTO_INCREMENT PRIMARY KEY,
long_url VARCHAR(255) NOT NULL,
short_code VARCHAR(10) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
clicks INT DEFAULT 0
);

<?php
// Database configuration
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'url_shortener');

// Base URL of your shortener
define('BASE_URL', 'https://mytechager.xyz/');

// Connect to database
try {
    $pdo = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

// Handle URL redirection
if(isset($_GET['code'])) {
    $code = $_GET['code'];
    
    try {
        $stmt = $pdo->prepare("SELECT long_url FROM links WHERE short_code = ?");
        $stmt->execute([$code]);
        $result = $stmt->fetch();
        
        if($result) {
            // Update click count
            $pdo->prepare("UPDATE links SET clicks = clicks + 1 WHERE short_code = ?")->execute([$code]);
            
            // Redirect to original URL
            header("Location: " . $result['long_url']);
            exit();
        } else {
            die("Invalid short URL");
        }
    } catch(PDOException $e) {
        die("Database error: " . $e->getMessage());
    }
}

// Handle URL shortening
if(isset($_POST['submit'])) {
    $long_url = filter_var($_POST['url'], FILTER_SANITIZE_URL);
    
    if(filter_var($long_url, FILTER_VALIDATE_URL)) {
        // Generate random code
        $code = substr(md5(uniqid(rand(), true)), 0, 6);
        
        try {
            $stmt = $pdo->prepare("INSERT INTO links (long_url, short_code) VALUES (?, ?)");
            $stmt->execute([$long_url, $code]);
            
            $short_url = BASE_URL . $code;
        } catch(PDOException $e) {
            die("Database error: " . $e->getMessage());
        }
    } else {
        $error = "Invalid URL provided";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>URL Shortener</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            text-align: center;
        }
        .container {
            background: #f9f9f9;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        input[type="url"] {
            width: 80%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            background: #e9ffe9;
            border-radius: 4px;
        }
        .error {
            color: red;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>URL Shortener</h1>
        <p>Enter a long URL to make it shorter</p>
        
        <form method="post">
            <input type="url" name="url" placeholder="Enter your URL here" required>
            <button type="submit" name="submit">Shorten URL</button>
        </form>
        
        <?php if(isset($error)): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <?php if(isset($short_url)): ?>
            <div class="result">
                <p>Your shortened URL:</p>
                <a href="<?php echo $short_url; ?>" target="_blank"><?php echo $short_url; ?></a>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *