1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

URL shortening - how to generate alpha-numeric links instead of just numeric ones?

Discussion in 'PHP' started by gingerbreadweb, Jul 14, 2009.

  1. #1
    Hey all

    I'm putting a URL shortening website together and I just wondered if anyone knows how to generate alpha-numeric links, rather than just numeric?

    For example, I know how to create numeric ones as follows:

    website.com/123

    That's easy. But what I want is the kind of alpha-numeric links you get from bit.ly and tinyurl:

    website.com/5fj8i

    I've tried searching for a script but I can't find one that meets all my criteria - the closest match does everything else I want it to, but not the alpha-numeric thing.

    If anyone can give me any help/advice I'd really appreciate it :)

    Thanks for your time

    Adam
     
    gingerbreadweb, Jul 14, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try this:

    
    
    function random_string($length=16)
    	{
    		$string = '';
    		$possible = "0123456789abcdefghijklmnopqrstuvwxyz";
    		
    		for($i=1;$i<$length;$i++):
    			$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
    				$string .= $char;
    		endfor;
    		
    		return $string;
    	}
    
    
    PHP:
    You will want to store url's in a database and check to make sure one doesn't exist before using it. Change the default length to whatever you want, or just call it with a specified length.
     
    jestep, Jul 14, 2009 IP
    gingerbreadweb likes this.
  3. gingerbreadweb

    gingerbreadweb Active Member

    Messages:
    512
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks jestep, will give this a try!
     
    gingerbreadweb, Jul 15, 2009 IP
  4. JefK

    JefK Member

    Messages:
    66
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #4
    why not using a random alpha-numeric string generator function? just like password generators, if you search, there are lots of scripts around.
     
    JefK, Jul 18, 2009 IP
  5. gingerbreadweb

    gingerbreadweb Active Member

    Messages:
    512
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I've got the random string generation sussed, thanks to jestep. Now I just need to figure out the most efficient way of creating a unique string each time, as each string that is generated is inserted into the database and must be unique. Bit of a headache!
     
    gingerbreadweb, Jul 18, 2009 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    This should be easier and more efficient:

    
    $lengthOfString = 4;
    $randomString = substr(md5(uniqid(rand(),1)),1,$lengthOfString); 
    echo $randomString;
    
    PHP:
     
    ThePHPMaster, Jul 18, 2009 IP
    gingerbreadweb likes this.
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Not sure what your DB name or columns are but try this.

    
    
    $new_url = random_string();
    
    function random_string($length=16)
        {
            $string = '';
            $possible = "0123456789abcdefghijklmnopqrstuvwxyz";
           
            for($i=1;$i<$length;$i++):
                $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
                    $string .= $char;
            endfor;
    
            $query = mysql_query("SELECT id FROM my_table WHERE url = ".$string."");
    
            if(mysql_num_rows($query) > 0)
            {
                    random_string($length);
            }
           
            return $string;
        }
    
    
    PHP:
    You'll need to adjust the query to fit your db, but this will reiterate the function until a unique url is created.
     
    jestep, Jul 18, 2009 IP
  8. Wuiqed

    Wuiqed Well-Known Member

    Messages:
    110
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #8
    I would suggest that you use base_convert(). Consider the following:

    1. The visitor goes to yoursite.com/5fj8i
    2. Your server rewrites yoursite.com/5fj8i into yoursite.com/redirect.php?id=5fj8i
    3. With the help of base_convert(), your script converts 5fj8i (which is base 36) into 9122850 (which is base 10)
    4. You retrieve the URL from database row with the id 9122850 and redirect the visitor
    This is the way I've done it before and it makes the process a lot easier with no need for storing any random unique alphanumeric strings.
     
    Wuiqed, Jul 19, 2009 IP
    gingerbreadweb likes this.
  9. gingerbreadweb

    gingerbreadweb Active Member

    Messages:
    512
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #9
    Cheers for the tips guys, really appreciate it. Going to try and get it working this week!
     
    gingerbreadweb, Jul 26, 2009 IP
  10. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Sounds like a shorter version of using the base64_encode and base64_decode functions.
     
    kblessinggr, Jul 26, 2009 IP