SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: url shortening algorithm

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question url shortening algorithm

    Hello Intelligent,

    Please help me with this.

    what can be the simple logic to generate 6 characters short url ( Should contain a-z, A-Z, 0-9 - Total 62 Chars) for a long url without any repeated result.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you anthony

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,560
    Mentioned
    35 Post(s)
    Tagged
    0 Thread(s)
    Why not just base-62-encode str_replace('.','',microtime())?

  5. #5
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks from me too, i'd been looking for something like this.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  6. #6
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been thinking about this since yesterday.

    I guess if you enter domain.com/xunrfg you're going to get a 404 right? So presumably you need to redirect all 404s to the index.php file and have that echo a location in the header to redirect the user.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all for helping

  8. #8
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tangoforce View Post
    I've been thinking about this since yesterday.

    I guess if you enter domain.com/xunrfg you're going to get a 404 right? So presumably you need to redirect all 404s to the index.php file and have that echo a location in the header to redirect the user.
    Actually you'll use mod_rewrite to redirect all requests to the domain (except perhaps js, images, css, and other media) to index.php in which a function should resolve the shortened URL to the full url. Now you don't have to redirect to the full url. Instead, you can set index.php to load specific php files, function, classes, and templates (or controller and action) depending on the values provided in the full url.

  9. #9
    SitePoint Enthusiast webdesignhouston's Avatar
    Join Date
    Dec 2010
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gokulgg View Post
    Hello Intelligent,

    Please help me with this.

    what can be the simple logic to generate 6 characters short url ( Should contain a-z, A-Z, 0-9 - Total 62 Chars) for a long url without any repeated result.
    You can use any random string generation function (search on google). After generating the string, be sure to check your database to see if the code is already in use. some may say 62 ^ 6 = 56 800 235 584 is not random enough.

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,560
    Mentioned
    35 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webdesignhouston View Post
    You can use any random string generation function (search on google). After generating the string, be sure to check your database to see if the code is already in use. some may say 62 ^ 6 = 56 800 235 584 is not random enough.
    And if your string already exists, you're wasting time.
    Better to use something that will never be the same (time/microtime) twice. Yes, it's not actually 'random'. Note that the OP didnt ask for random. He asked for a UUID, ala uniqid

  11. #11
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with uniqid StarLion, is that its too long. Anthonys method is still unique because its based on the insert id of an auto incrementing table and therefore the number of the latest row will always be different to the last row and hence the code will be too.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Thanks friends

    A simple solution can be an "auto incrementing" id or "microtime".

    But one more thing I have to ask if above said methods used. If I provide a custom url recommendation

    Eg :
    Long url : http://www.url.com/123/abc/
    Short url (Auto) : http://abc.xy/eGe323
    Short url (Manual Recommended) : http://abc.xy/manualrecommedation

    In the case there may some conflicts.

    Same value of "X" microtime can be equal to the already added custom value by the user. This can be solved by adding 1 with the microtime.

    I can check the value if already added or not by mysql query. But in case if many checks lead to result of "value already added by user". I m wasting time. Am I right? So, What can be the solution.

  13. #13
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,373
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    My favoured approach is to build a large series of "random" short identifiers (or, shuffled sequential numbers). Then, whenever you need to use a new one, just ask your database or wherever for the next one (or, any one) in the series that isn't being used.

    There is absolutely no need for anything to happen on-the-fly like converting decimal integers (e.g. AUTO INCREMENT primary keys) into something shorter, nor for the wasteful process of generating a usable random short identifier by polling to the database to see if it exists, and repeating until it does not.

    This also helps with the possible collisions that may occur when allowing customised short identifiers: if your system already has a list of all (used or unused) identifiers, disallowing a custom choice that already exists in the list is trivial.
    Salathe
    Software Developer and PHP Documentation Team.

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I cannot understand well. Please can be explain briefly.

  15. #15
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    My favoured approach is to build a large series of "random" short identifiers (or, shuffled sequential numbers). Then, whenever you need to use a new one, just ask your database or wherever for the next one (or, any one) in the series that isn't being used.
    Perhaps you should provide an example of this.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •