How to make your Own Tiny URL using PHP and MySQLi Database

Creating a short URL typically involves generating a unique identifier for the long URL and then mapping that identifier to the long URL in a database or some data structure. Here's a basic example of how you can create a simple PHP script to generate a short URL using PHP: 

<?php
// Database connection (You'll need to set up your own database)
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the long URL from the form submission
    $longURL = $_POST["long_url"];

    // Generate a unique identifier (you can use a library for this)
    $shortIdentifier = substr(md5(uniqid(rand(), true)), 0, 6);

    // Insert the mapping into the database
    $sql = "INSERT INTO short_urls (short_id, long_url) VALUES ('$shortIdentifier', '$longURL')";

    if ($conn->query($sql) === TRUE) {
        $shortURL = "http://your_domain/$shortIdentifier";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>URL Shortener</title>
</head>
<body>
    <h1>URL Shortener</h1>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="long_url">Enter a long URL:</label>
        <input type="text" name="long_url" id="long_url" required>
        <input type="submit" value="Shorten">
    </form>
    <?php
    if (isset($shortURL)) {
        echo "Short URL: <a href='$shortURL'>$shortURL</a>";
    }
    ?>
</body>
</html>

In this code:

  1. The form allows users to enter a long URL.

  2. When the form is submitted, the PHP code at the top of the file processes the form data.

  3. It generates a unique short identifier, inserts the mapping into the database, and then displays the resulting short URL.

Make sure to replace "your_server", "your_username", "your_password", "your_database", and "your_domain" with your actual database and domain information. Also, consider using a more robust method to generate short identifiers and handle errors more gracefully in a production environment.


To make the generated short URL redirect to the corresponding long URL, you need to implement a redirection mechanism. You can do this by creating a PHP script that reads the short identifier from the URL, looks up the corresponding long URL in the database, and performs the redirection.

Here's an example of how you can modify your code to enable URL redirection:

  1. Create a new PHP script (e.g., redirect.php) that will handle URL redirection:
<?php
// Database connection (You'll need to set up your own database)
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get the short identifier from the URL
$shortIdentifier = $_GET["short_id"];

// Look up the corresponding long URL in the database
$sql = "SELECT long_url FROM short_urls WHERE short_id = '$shortIdentifier'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $longURL = $row["long_url"];
    // Redirect to the long URL
    header("Location: $longURL");
    exit;
} else {
    echo "Short URL not found.";
}

$conn->close();
?>

Modify your previous code to display the short URL correctly with the redirect.php script as the action URL for the form:

<!DOCTYPE html>
<html>
<head>
    <title>URL Shortener</title>
</head>
<body>
    <h1>URL Shortener</h1>
    <form method="post" action="process.php"> <!-- Change "process.php" to the actual file name of your form processing script -->
        <label for="long_url">Enter a long URL:</label>
        <input type="text" name="long_url" id="long_url" required>
        <input type="submit" value="Shorten">
    </form>
    <?php
    if (isset($shortURL)) {
        echo "Short URL: <a href='$shortURL'>$shortURL</a>";
    }
    ?>
</body>
</html>
  1. When you create the short URL in your form, make sure to change the form action to point to your form processing script (e.g., process.php), and set the method attribute to "POST". Also, ensure that the form's action attribute matches the script name you've created for redirection (redirect.php).

Now, when you copy and paste the short URL, it will redirect to the corresponding long URL.


To redirect a short URL to its corresponding long URL, you can use the header function in PHP. Here's how you can create a simple PHP script to handle the redirection:

  1. Create a new PHP file, let's call it redirect.php, and place it on your server. This script will handle the redirection.
<?php
// Database connection (You'll need to set up your own database)
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get the short identifier from the URL
$shortIdentifier = $_GET["short_id"];

// Look up the corresponding long URL in the database
$sql = "SELECT long_url FROM short_urls WHERE short_id = '$shortIdentifier'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $longURL = $row["long_url"];
    // Redirect to the long URL
    header("Location: $longURL");
    exit;
} else {
    echo "Short URL not found.";
}

$conn->close();
?>
  1. Modify the way you create short URLs in your original form processing code. When you generate the short URL, you should make it link to your redirect.php script with the short identifier as a parameter. Here's an example:

In your original form processing code:

// After you generate the short URL, set it as a link
$shortURL = "http://your_domain/redirect.php?short_id=$shortIdentifier";

// Display the short URL as a link
echo "Short URL: <a href='$shortURL'>$shortURL</a>";

Now, when you click on the short URL, it will be redirected to the corresponding long URL.

Make sure to replace "your_server", "your_username", "your_password", "your_database", and "your_domain" with your actual database and domain information. Additionally, ensure that you have set up the database and have the necessary table (short_urls) to store the mappings.

 

 

Why are humans here on earth?

The question of why humans are here on Earth is a complex and multifaceted one, and it can be approached from various perspectives, including scientific, religious, philosophical, and existential viewpoints. Here are some of the key perspectives on this question:

  1. Scientific Perspective: From a scientific standpoint, humans are here on Earth as a result of natural processes, evolution, and the development of life on our planet. Life on Earth originated through a combination of physical and chemical processes, ultimately leading to the emergence of complex organisms, including humans. Evolutionary theory suggests that humans are the product of millions of years of natural selection, genetic mutation, and adaptation.

  2. Religious Perspective: Many religious traditions offer their own explanations for why humans are on Earth. For example, in Christianity, it is believed that humans are here to fulfill God's plan, to seek a relationship with God, and to live in accordance with His will. In other religions, there are different beliefs about the purpose of human existence, often tied to the pursuit of spiritual growth and enlightenment.

  3. Philosophical Perspective: Philosophers have debated the purpose of human existence for centuries. Some argue that the purpose of life and human existence is a subjective matter, and individuals must find or create their own meaning and purpose. Existentialist thinkers, for example, often emphasize the importance of creating one's own purpose in life.

  4. Existential Perspective: Existentialism posits that human existence is marked by a fundamental sense of "existential" or "ontological" angst or unease, often due to the apparent lack of inherent meaning in the universe. Existentialists contend that it is up to each individual to define their own meaning and purpose in life.

  5. Ecological Perspective: From an ecological standpoint, humans are considered a part of the interconnected web of life on Earth. They have a role and responsibility in maintaining the health of the planet and the diversity of life. This perspective emphasizes the importance of environmental stewardship and sustainability.

Ultimately, the question of why humans are on Earth is a deeply philosophical and existential one, and it may not have a single, universally accepted answer. Different individuals and cultures may interpret this question in their own ways based on their beliefs, values, and worldviews. It's a topic that continues to be explored, discussed, and debated by people from all walks of life.

Make your own Download SpeedTest in php code

To convert the download speed from KB/s to MB/s, you can divide the speed value by 1024 (since there are 1024 KB in 1 MB). Here's how you can modify your PHP code to display the download speed in megabytes per second (MB/s):

<?php
$start_time = microtime(true);

// URL of the file to download
$url = 'https://domain-name-here.com/downloadfiletest.zip';

// Initialize curl session
$ch = curl_init($url);

// Set curl options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Execute curl request and get the content length
$content = curl_exec($ch);
$content_length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

// Calculate download time and speed
$end_time = microtime(true);
$download_time = ($end_time - $start_time);
$download_speed_kb = ($content_length / $download_time) / 1024; // Download speed in KB/s
$download_speed_mb = $download_speed_kb / 1024; // Convert to MB/s

// Print the download speed in MB/s
echo "Download speed: " . round($download_speed_mb, 2) . " MB/s";

// Close the curl session
curl_close($ch);
?>

In this code, we first calculate the download speed in KB/s, and then we further divide it by 1024 to get the download speed in MB/s. The result is rounded to two decimal places before printing it.

To schedule one person for one week with hours in military time using PHP

To schedule one person for one week with hours in military time using PHP, you can create a simple code that generates a schedule for each day of the week. Here's an example code to get you started:

<?php
// Define the schedule parameters
$personName = "John Doe";
$startDate = new DateTime('2023-10-23'); // Start date for the week (adjust as needed)

// Create an array to represent the schedule for each day of the week
$schedule = array(
    'Monday' => array('08:00', '17:00'),
    'Tuesday' => array('08:00', '17:00'),
    'Wednesday' => array('08:00', '17:00'),
    'Thursday' => array('08:00', '17:00'),
    'Friday' => array('08:00', '17:00'),
    'Saturday' => array('08:00', '17:00'),
    'Sunday' => array('08:00', '17:00')
);

// Display the schedule
echo "Weekly Schedule for $personName:\n";
foreach ($schedule as $day => $hours) {
    $startTime = $hours[0];
    $endTime = $hours[1];
    echo "$day: $startTime - $endTime\n";
}

// You can use the $startDate to calculate specific dates for this week.
?>

In this code:

  1. We define the name of the person and the start date for the week.
  2. We create an array named $schedule where each day of the week (Monday to Sunday) has a start time and an end time in military time format (e.g., '08:00' for 8:00 AM and '17:00' for 5:00 PM).
  3. We use a foreach loop to iterate through the schedule and display it.

You can adjust the start date, person's name, and hours as needed. If you want to schedule multiple people or add more advanced features, you can expand upon this basic template.

- All From ChatGPT
PLG_GSPEECH_SPEECH_BLOCK_TITLE