Primm
Primm

Reputation: 1387

Shrink a URL in HTML

So, im making a file hosting site, and am using some form builders to start off with. However, these builders do NOT support PHP. Now, i would like to shrink some URLs, how can i do this in pure HTML, without adding in PHP methods. I am fine with goo[dot]gl, bit.ly, tinyurl.com, or whatever else!

Upvotes: 0

Views: 481

Answers (2)

Ozzy
Ozzy

Reputation: 8322

If the form builders don't support PHP you need to write it yourself. PHP is very easy to work with.

Here is an example for you. (Assuming you have PHP set up on your web host:)

Save the file with the extension .PHP (or whatever your web host uses - might be .PHP5 for php5) instead of .HTML

You can use the super-global $_GET to accept certain variables from the URL in the address bar ex.:

$short_url = $_GET["q"];

Since i'm getting a variable named 'q', if you access the page with a parameter named 'q' I will have that variable stored ex.:

http://your.site/?q=shorturl # Assumes your index file takes the 'q' variable

Now it is up to you what to do with that variable. The best thing would be to set up a MySQL database so that when you get a value like 'shorturl' you can do a quick SQL query to return the full address ex.:

# Make DB connection
$db = new PDO("mysql:host='X.X.X.X';dbname='mydb'", $user, $secret);

# Function to search database for URL
function getFullURL($short_url) {
    $result;
    $sql = "SELECT full_url FROM tbl_addresses WHERE short_url='?'";
    $query = $db->prepare($sql);
    $query->execute(array($short_url));
    $rows = $query->rowCount();
    if ($rows == 1)
        $result = $query->fetchAll();
    return $result;
}

There's really not much to it in PHP with MySQL.

Upvotes: 1

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

HTML is a Markup Language.

If you want to use some API, or anything more coding-oriented, you have to use a real programming language - you choose : for your purpose PHP would be the best choice.

Now, if you finally decide to use PHP, it's really easy.

Code (for TinyURL) :

<?php
    function createTinyUrl($strURL) {
        $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$strURL);
        return $tinyurl;
    }
?>

Usage :

<?php
     $myTinyUrl = createTinyUrl("http://www.yourdomain.com/some-long-url-here");
?>

And that's all! ;-)

Upvotes: 2

Related Questions