1

I am trying to create a unique URL number of each page in my website to include it into a sitemap easily. My urls look like this : site.com/book/11111111, site.com/book/11111112

I want to insert a unique URL for each ID, but stuck with a little problem. I use the code below, and it doesn't guarantee the uniqueness of my thing_url for every time. I can check its uniqueness with an if-else statement but even I check it for the second time, my string may or may not be unique for the second time, either. I don't want to have lots of variables and if-else statements using $thing_url, $thing_url2, $thing_url3, etc.

Is there a way to check the uniqueness using a single loop, until it succeed? Here's what I do now.

$thing_name = $_POST['thing_name']; // form value
$thing_url = mt_rand(10000000, 99999999); // generates 8 digit numeric string

$query = "SELECT thing_url FROM thing WHERE thing_url = '$thing_url'";
$result = $sqli->query($query);
$row = $result->fetch_assoc();
$finish = $row['thing_url'];

if($finish == $thing_url) {
    echo "Bad luck";
} else {
 $thing = $sqli->prepare("INSERT INTO thing(thing_url, thing_name ) VALUES (?,?)"); // prepare
    $thing->bind_param("ss", $thing_url, $thing_name); // bind
    $thing->execute(); // execute
    $thing->close(); // close
}
salep
  • 1,332
  • 9
  • 44
  • 93
  • 3
    Use autoincrement id. Here are more informations http://www.w3schools.com/sql/sql_autoincrement.asp – Jozef Dúc Jan 11 '15 at 20:40
  • 1
    I already use auto_incremented ID for each record and they're unique, but I want to use these thing_url's as a unique URL. auto_increment ID values are easy to guess and I don't want my site content to be crawled that easy. – salep Jan 11 '15 at 20:41
  • 2
    And crawling ids from 1000000 to 999999 is much harder? – u_mulder Jan 11 '15 at 20:43
  • @u_mulder, I laughed so hard :D That's correct. but I want to make it a little harder, will change URL structure to SEF url's soon, but know I don't want to mess with htaccess much. – salep Jan 11 '15 at 20:45
  • You can use random string for example 64 chars are much enough for your app :) http://stackoverflow.com/questions/4356289/php-random-string-generatorand use one function with while to check it uniqueness. – Jozef Dúc Jan 11 '15 at 20:48

1 Answers1

1

Clients should never try to generate anything unique (other than a GUID) due to this type of synchronization issue. Instead, a central coordinator should have that responsibility.

In your case the ideal coordinator is your database, MySQL. You can use the primary key of your URL (from thing_url) as the unique identifier. That will work fine as long as the database is following the typical pattern of incrementing the primary key by 1 and you started at a low number (usually 1).

I already use auto_incremented ID for each record and they're unique, but I want to use these thing_url's as a unique URL. auto_increment ID values are easy to guess and I don't want my site content to be crawled that easy

Then permute the ID in some bijective manner, e.g. XOR with some bit pattern, rotate the bits, swap the bytes, etc.

Eric J.
  • 147,927
  • 63
  • 340
  • 553