Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:05:40+00:00 2026-06-09T21:05:40+00:00

I am working on a url Shortener and Currently i am facing a problem

  • 0

I am working on a url Shortener and Currently i am facing a problem with Random Generated String. Things i want to do is:

  1. Generate A Random String [ Already Done ]
  2. Searching into Database Whether it exists or not [ Already Done ]
  3. If it already Exists, Generate a new one and Again Proceed for checking [ Stacked !]
  4. if Its Unique, Go for Next Process

Now This Is the code is used for generate Random String

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
$str = '';
$count = strlen($charset);
while ($length--) {
    $str .= $charset[mt_rand(0, $count-1)];
}
return $str; }

What can i do now?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:05:42+00:00Added an answer on June 9, 2026 at 9:05 pm

    Use a while loop:

    $random_string = randString(10);
    $is_unique = false;
    
    while (!$is_unique) {
        $result = query_the_database('SELECT id FROM table_with_random_strings WHERE random_string_column = "'.$random_string.'" LIMIT 1');
        if ($result === false)   // if you don't get a result, then you're good
            $is_unique = true;
        else                     // if you DO get a result, keep trying
            $random_string = randString(10);
    }
    

    I left the database code generic, since I am not sure what you’re using… but I hope it is mysqli or PDO 🙂

    Also wanted to mention, there are functions out there that generate unique strings for you, for example uniqid. Such functions would probably have more success at generating a unique string in the first go, making the while loop unnecessary most of the time — which is a good thing.

    echo uniqid(); // 502ec5b8ed2de
    

    However, you don’t have as much control over the length, and if that is important than you can stick with your homebrew random generator — just expect the likelihood of collisions to be greater.

    Edit Another thing: usually, instead of a random string that is meaningless to your user, many content publishing systems will use the article title. This is called (sometimes) a “post slug”. If you have a title of “November 17th: Gorillas Gone Wild, Topless Apes Live!”, the url would be:

    http://www.mywebsiteaboutgorillas.com/november-17th-gorillas-gone-wild-topless-apes-live
    

    Such a URL has more meaning to your user than:

    http://www.mywebsiteaboutgorillas.com/jh7sj347dfj4
    

    To make a “post slug”:

    function post_slug($url='', $sep='-') {
        // everything to lower and no spaces begin or end
        $url = strtolower(trim($url));
    
        // adding - for spaces and union characters
        $find = array(' ', '&', '\r\n', '\n', '+',',');
        $url = str_replace ($find, '-', $url);
    
        //delete and replace rest of special chars
        $find = array('/[^a-z0-9\-<>]/', '/[\-]+/', '/<[^>]*>/');
        $repl = array('', '-', '');
        $url = preg_replace ($find, $repl, $url);
    
        if ($sep != '-')
            $url = str_replace('-', $sep, $url);
    
        //return the friendly url
        return $url; 
    }
    

    … you do still have to watch out for uniqueness there, sometimes CMSes will append the date or id as a pseudo-subdirectory to help mitigate against repetition. You might be able to work something like that into your URL shortener, to give the user at least some indication of what they’re about to click.

    Documentation

    • while – http://us.php.net/manual/en/control-structures.while.php
    • mysqli – http://us.php.net/manual/en/book.mysqli.php
    • PDO – http://us.php.net/manual/en/book.pdo.php
    • uniqid – http://php.net/manual/en/function.uniqid.php
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK, so I'm working on my first solo Rails app, a URL shortener, and
I'm working on a URL shortener and attemtping to convert the URL ID, which
I am working on URL Get content. If i want to fetch ONLY the
I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
Working live URL showing problem: http://69.24.73.172/demos/newDemo/test.html The HTML : <div class="small-vote"> <a href="#" class="s
I am working on a URL shortening site which uses PHP, MySQL and Apache.
My url routing is working properly inside Areas folder, that means i can go
youtube url is not working in javascript,why? where is the mistake in my code:
Im working on an app that now has to go to a URL online
I have this working query that outputs URL and click counts from database. <?php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.