0

Right now im using auto increment to identify resources in my website.

The problem is i dont want the users to know how many resources there are.

How could i instead use some kind of structured "random" combination of letters and numbers (say 6 digits) that i could use instead of (or in addition to) the auto incrementing primary key numbers.

Basically i want the users to see

website.com/page.php?4jnd32

instead of

website.com/page.php?13

I dont even know what this practice is called which hampers my ability to search for an answer

mrpatg
  • 10,001
  • 42
  • 110
  • 169

3 Answers3

1

If you really want this, create a new field (e.g: fake_id) then use php's uniqid function.

erenon
  • 18,838
  • 2
  • 61
  • 93
0

You could use a GUID although this would be substantially longer than "4jnd32". A guid is a Globally Unique Identifier. It would identify pages uniquely, but it wouldn't give the user any idea how many pages there are or give an ordering to the page.

I'm not an expert on PHP, but a quick search seems to indicate that php has a function to generate GUIDs.

Daniel Brotherston
  • 1,954
  • 4
  • 18
  • 28
0

MySQL supports UUIDs. But you'll want to employ a trigger to get this working automatically.

CREATE TABLE `tester` (
  `tester_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `uuid` VARCHAR(64),
  `foo` VARCHAR(45),
  PRIMARY KEY (`tester_id`),
  INDEX `index_uuid`(`uuid`)
)
ENGINE = InnoDB;

delimiter |
CREATE TRIGGER tester_uuid BEFORE INSERT ON `tester`
FOR EACH ROW BEGIN
  SET NEW.uuid = UUID();
END;
|
delimiter;

INSERT INTO tester (foo) VALUES('bar'), ('baz');

SELECT * FROM tester;
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206