views:

46

answers:

1

I've created a script called spiralurl (tiny url script). I'm still learning and making changes as I go. One thing I can't get fixed is to check that the user entered url already exists in the database, if it does exist I want output to the user the already shorted url previously created.

Script is at: http://redspiral.net/spiralurl

Any help is much appreciated, I'm still a learner so... :).

Thanks.

Here is the code which I would edit I think.

<?php

/*

      Filename:  class_short_url.php
      Creation   Date: 18-03-2010     
      Author:    RedSpiral / Yogesh Nagarur
      URL:       www.redspiral.net  
      Project:   SpiralURL V2

*/



class ShortUrl
{
    /** set initial vars **/
    var $is_error;
    var $error_message;

    /** this function crates a new short url **/
    function create_url($long_url, $api = false)
    {
        /** set globals **/
        global $url, $_LANG, $_CONFIG;

        /** start sessions **/
        @session_start();

        /** set url var **/
        $this->long_url = $long_url;

        /** check url has http:// or https:// **/
        if(substr($this->long_url, 0, 7) != "http://"){ $this->long_url = 'http://'.$long_url; }

        /** check that $url var is not empty **/
        if(empty($this->long_url) || $this->long_url == '')
        {
            $this->is_error = 1;
            $this->error_message = '<div id="error">Please enter a url to shorten.</div>';
        }else

        /** check url is valid **/
        if(is_valid_url($this->long_url) == false)
        {
            $this->is_error = 1;
            $this->error_message = '<div id="error">Please enter a valid url.</div>';
        }else

        /** no error? **/
        if($this->is_error != 1)
        {
            /** generate new url id **/
            $this->short_url_id = rand_string(6, 30);

            /** insert link in to database **/
            @mysql_query("INSERT INTO short_urls (long_url, url_id, url_hits, owner, screenshot ) VALUES ( '".$this->long_url."', '".$this->short_url_id."', '0', '".$_SESSION['userid']."', '".$this->screenshot."')");

            /** show new url **/
            echo '<center><input id="link" readonly="readonly" onClick="this.select();" value="'.$url->url_base.'/'.$this->short_url_id.'" type="text"></center><div id="buttons"> <a id="copy" class="button" href="javascript:;"><img src="./assets/images/copy-icon.png" alt=""> Copy</a><a class="button" href="javascript:;" onClick="return addthis_open(this, \'more\', \''.$url->url_base . '/' .$this->short_url_id.'\', \''.$url->url_base . '/' .$this->short_url_id.'\')"><img src="./assets/images/share-icon.png" alt=""> Share</a></div></div>';

            /** exit **/
            exit();
        }
    }


    /** this function deletes a url **/
    function delete_url($url_id, $admin = false)
    {
        /** set globals **/
        global $url;

        /** start sessions **/
        @session_start();

        /** do delete **/
        if($admin == true):
        mysql_query("DELETE FROM short_urls WHERE url_id = '".$url_id."' LIMIT 1");
        else:
        mysql_query("DELETE FROM short_urls WHERE url_id = '".$url_id."' AND owner = '".$_SESSION['userid']."' LIMIT 1");
        endif;

        /** redirect **/
        if($admin == true): redirect($url->url_base.'/admin?manage_urls'); else: redirect($url->url_base.'/account/home'); endif;

        /** exit **/
        exit;
    }
}

?>
+1  A: 

For Every URL you save, create an MD5 string (identifier) of that url and save it to the database. Next time a user enters the URL, you can check the md5 of entered URL against the MD5 in the database.

This will be faster than checking the url field of the database as it is a constant string length and you can make it a clustered index.

Stewie
Thanks will try this. This is a first time I will be creating a MD5 string, can you direct me to any docs please and how I can check etc?
yogesh
@yogesh: http://us.php.net/md5 -- to check to see if it's in the database as Stewie suggests, `SELECT COUNT(*) ... WHERE md5='$md5'` from the database first, if the count is greater than zero, it exists already.
Josh
Count is an aggregate function, there is no need to use it as it will take in few extra CPU cycles. What you can do is two birds with one stone. SELECT url FROM Table WHERE md5URL = 'md5($url)' This will return you the URL (if exists) or no results. If it returns url, you return url to the end user
Stewie