Category: Coding

  • URL Shortener AI-Generated RoadMap

    For sometime, I’ve wanted to write a URL Shortener. I need a little help getting started and I think ChatGPT can provide the direction I need.

    Because I also want to keep a Coding Journal, I should probably take a stab at solving my problem first before I ask ChatGPT or Bard to lay out a roadmap for me. I believe stating, in plain language, what I will want a script to do will help me understand the discrete steps that need to be taken.

    I will then compare what I think needs to be done with what ChatGPT suggests and lookup what I don’t understand in Flavio Copes PHP Handbook – Learn PHP for Beginners on FreeCodeCamp.com.

    I want to create a URL Shortener.

    So, in plain words, what should my URL Shortener do?

    1. The script should take a URL and create a shortened version of it,
    2. storing both the original URL and the shortened version.
    3. When the short URL is used, somehow, magically, some kind of lookup must be done
    4. to figure out what the short url stands for.
    5. I would also like the script to count the number of times a url shortener has been used.
    6. I think I’ll need a text file to read and write to.

    I queried ChatGPT for a starting point. At this point, I will learn what ChatGPT’s code instructions mean by reading Flavio

    What follows below is ChatGPT‘s response to my query: what are the steps to create a url shortener in php?

    ChatGPT-Generated URL Shortener Roadmap

    Here are the general steps to create a URL shortener in PHP:

    1. Create a MySQL database to store the original and shortened URLs.
    2. Create a PHP script to generate a shortened URL based on the original URL.
    3. Write a PHP script to insert the original and shortened URLs into the MySQL database.
    4. Create a PHP script to redirect the shortened URL to the original URL.
    5. Create a web form where users can enter the original URL and get the shortened URL.

    Here’s an example of how you can implement each step:

    Step 1: Create a MySQL database to store the original and shortened URLs.

    You can use phpMyAdmin or any other database management tool to create a database. Here’s an example SQL query to create a table to store the original and shortened URLs:

    CREATE TABLE urls (
        id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        original_url VARCHAR(255) NOT NULL,
        short_url VARCHAR(20) NOT NULL
    );

    Step 2: Create a PHP script to generate a shortened URL based on the original URL.

    Here’s an example PHP function to generate a shortened URL:

    function generateShortURL($originalURL) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $shortURL = "";
        for ($i = 0; $i < 7; $i++) {
            $randNum = rand(0, strlen($chars)-1);
            $shortURL .= $chars[$randNum];
        }
        return $shortURL;
    }

    This function generates a random string of 7 characters that can be used as a shortened URL.

    Step 3: Write a PHP script to insert the original and shortened URLs into the MySQL database.

    Here’s an example PHP function to insert the original and shortened URLs into the MySQL database:

    function insertURL($originalURL, $shortURL) {
        $conn = mysqli_connect("localhost", "username", "password", "dbname");
        $sql = "INSERT INTO urls (original_url, short_url) VALUES ('$originalURL', '$shortURL')";
        mysqli_query($conn, $sql);
        mysqli_close($conn);
    }

    This function uses the mysqli extension to connect to the MySQL database and insert the original and shortened URLs into the urls table.

    Step 4: Create a PHP script to redirect the shortened URL to the original URL.

    Here’s an example PHP script to redirect the shortened URL to the original URL:

    $shortURL = $_GET['url'];
    $conn = mysqli_connect("localhost", "username", "password", "dbname");
    $sql = "SELECT original_url FROM urls WHERE short_url='$shortURL'";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    mysqli_close($conn);
    header("Location: ".$row['original_url']);
    exit();

    This script retrieves the original URL associated with the shortened URL from the urls table and uses the header() function to redirect the user to the original URL.

    Step 5: Create a web form where users can enter the original URL and get the shortened URL.

    Here’s an example HTML form to allow users to enter a URL and get a shortened URL:

    <form action="shorten.php" method="post">
        <label for="url">Enter a URL:</label>
        <input type="text" name="url" id="url">
        <input type="submit" value="Shorten URL">
    </form>

    This form submits the original URL to a PHP script called shorten.php that generates a shortened URL and inserts the original and shortened URLs into the MySQL database. The script then displays the shortened URL to the user.


  • Toward a Working URL Shortener or, My Saga of Woe

    Maybe one day I’ll tell the story of how I got to the point of wanting to write a URL shortener but doing so right here and right now will only detract from actually getting down to writing a URL shortener in PHP. Instead, I’ll go off on a different tangent…

    Why I Haven’t Written a URL Shortener

    Most people would be happy to explain why they wrote a URL shortener. Many other people would probably want to know why anyone would bother to do something that has been done over and over again ad nauseum (think bit.ly).

    Yet ANOTHER URL Shortener?

    Instead, please allow me to tell you why I haven’t written a URL shortener, although I have wanted to for years.

    I am a Person Who Needs Her Routines

    When I was first struck by this idea, I thought I would use the Python programming language to write my dream URL shortener.

    But before I got started, I signed up for FreeCodeCamp’s Responsive Web Design course and the 100 Days of Code Challenge to brush up on my rusty late early 2000’s HTML and CSS skills (when you fly airplanes for a living, you don’t need HTML and CSS ).

    Halfway through Free Code Camp’s course, I was struck by the thought that I really needed to take that online CS50 course everyone was raving about. Surely that would put me on the path to programming greatness.

    I signed up, paid for the certification and dove in. No sooner had I started Harvard’s CS50 class when my laptop slid off my bed and onto the floor, cracking the LCD screen beyond repair. What a disaster.

    The CS50 class and the Free Code Camp course were extracurricular activities. They were something I wanted to do to get back into Web Development. The laptop meant I could learn and have fun anywhere.

    But my regular work activity depended on that laptop since I was away from my desktop most of the time.

    By the time I got another laptop and got situated again, I was trying to catch up with things that had fallen through the cracks and my routines were disrupted. CS50, Free Code Camp and 100 Days of Code fell by the wayside.

    Some people move in and out of routines easily. They appear to go with the flow when things get disrupted. For me, changes to routines feel like crises. I like plans and schedules. Knowing what I am going to do and when may seem boring, but I thrive in this space. Shifting gears unexpectedly is definitely not my preference.

    But wait, there’s more…

    Dependencies and Standards are the Bane of My Existence

    If ever you need someone to build you a house of cards, I’m your person. I create dependencies like noone else and, sometimes, those dependencies become roadblocks to doing what I want to do. It is as if I cannot move forward until that optimal dependency is in place.

    So, while I decided I would write my URL shortener in Python, somehow taking CS50 became a dependency. Fast forward three years (CS50 Course, Free Code Camp and 100 Days of Code unfinished) and I got it in my head that I needed to move my individual websites (in various states of disarray) into a WordPress MultiSite installation.

    “Okay, fine,” you say, “get on with it and set up a WP Multisite!”

    But wait! WPEngine, where I host some of my sites, does not support WordPress Multisite. So I have to find another host. Fear of the unknown sets in. So, which host is best for a multisite install?

    Now, it isn’t enough to find any ol’ MultiSite host – no, I have to find the right WordPress Multisite host. Down the rabbit hole I went and when I came back up a year had passed and still no host.

    Somehow, the dependency (find a WordPress MultiSite Host) became a standard with a mandate: find THE BEST WordPress Multisite Host and then, of course, learn all about everything before you do anything.

    Oh. My. GAWD.

    Dependencies, particularly needless dependencies waste time. I have to remember what my Dad used to say:

    When your standards are too high, lower your standards.”

    Finally, I am having issues with Focus

    Everything looks interesting. And apparently, I have a Fear of Missing Out on Learning.

    There is so much that I want to learn and do. So much that I actually start many new things but have a difficult time finishing them not because I lose interest. I don’t. I don’t finish projects because before one project is finished, I’ve started another oh-that-looks-like-fun, shiny object, interesting project.

    Over the past five years I learned enough about Eleventy to build a small website, I bought a few books and a bunch of glass and taught myself stained glass making. I took a two-part ceramics class and briefly joined a studio, learning enough to make small wedding favors for my daughter’s wedding. Along with my Husband, we gutted and remodeled our kitchen. I sold an aviation business and went to work for the new owner. We had a global pandemic. And, as if that wasn’t enough, I started a Real Estate course to become a licensed Real Estate Sales Associate in Florida.

    You know what this is called? Easily Distractiable. Too many irons in the fire! Not enough FOCUS!

    Frankly, one of the best things about flying airplanes was that my entire mind and body were engaged in that gravity-defying activity. There was no room for anything else because flying filled my head: FAA regulations, understanding weather, aircraft performance and limitations, airspace, instrument flying, weight and balance and teaching others how to fly.

    Perhaps because I am not flying much anymore, there is a void in my psyche that, in some weird way, I am trying to fill and excelling at that.

    But if I am ever going to get that URL Shortener written, I’m going to have to stop stuffing my head and to do list with tasks that don’t move me toward that goal.


  • Keeping a Programming Journal: Coding and Writing

    When I was learning to fly, and long after I earned my pilot certificates, there were occasions that demanded I process what I just learned. Writing is the best way for me to process. When I write, I can describe something I may not fully understand and hone in on its meaning or relevance by choosing my words carefully.

    When something isn’t working or when I know a procedure or behaviour is wrong (like not having a plan and landing long and fast on a short runway), it isn’t enough for me to say, “oh just don’t do that again.” No.

    I have to tease out in excruciating detail what was wrong, how it went south, what could have happened and what should have been done.

    After I’ve done so, I usually feel better. And usually because there was a lesson learned.

    Patterns and Positives

    Things don’t have to go wrong to make it into my journal though. Capturing positives like “aha” moments can refocus what seemed like a wasted effort into a more positive victory. Surely one good things happened, right?

    Things I need to know more about can be captured also. When you don’t know what you don’t know, well, write it down, describe it, give it form so you have a starting point.

    And so it will be with this Coding Journal.

    Programming is not my passion. Making stuff is my passion. 

    Pieter Levels, Maker of Nomad List

    Resources

    Programmer’s Pyramid: A work-at-your-own-pace overview of programming concepts.

    Patterns and Positives: If you can’t see a good reason to keep a coding journal but still want to another person’s opinion, Amy Haddad’s post about keeping a programming journal may help you see things differently.