php - Faster and if possible less memory cost method generate an alphanumeric code? -


I am trying to create a very simple short URL redirect without the database.

Here's what I have done so far:

  & lt ;? Php $ name = $ _GET ['file']; $ Name = preg_replace ("/ [^ a-za-z0- 9] /", '', $ name); $ File = 'data /' $ name; // file found (if_file ($ file)) // // Read the first row, we do not use file_get_contents because the data folder is secure and internally $ f = fopen ($ file, 'r') should be read; $ Data = fgets ($ f); Fclose ($ f); // redirect to the real URL header ("Location: $ data"); } Else {// The shame in the header of the URL is ("Location: http://www.mydomain.com/"); } Go out(); ? & Gt;   
  • I would like to know what will be faster and if there is a method of potential low memory to generate alphanumeric code from 6 to 8 characters that do not collide Does the data exist in the folder?

    Do you also require that any URL Will it be possible to see the short code? Only the counting of numbers will move to create unique file names, but of course this is not a repeatable method, so if the same URL goes too many times, it will come out with different keys each time

    If it is acceptable, then I suggest only one counter, probably the base 36 (case insensitive alphanumeric) or you have to give the maximum key size. Similar to You can have a file in which the current count (can be stored in memory but it will need to be reloaded on restart) and then of course you have to be careful about multi-thread access, both of them At the same time the next price will be read.

    If you want to consistently submit a single ID to a URL, then there may be a second directory storage file in the name of the URL (escaped as appropriate), in which you have generated the first key. When you create a new key, you can see this file in the directory if the URL already has a key and it is returned, if it is there.

    As you can see, it principally mimics the database's working method, originally with two directories indexing on a table of URLs and keys.

    The only way I can do this is to think that there is such a function that will be guaranteed for one-to-one input that you are looking for to produce a string under a certain length I do not think how you will get such a function. Compression algorithms are the closest thing, but of course, they produce these products which do not suit your needs (because binary will probably be larger as the original string when the base 64 is encoded or similar).

    Fardjad has suggested that a hinting will be cured, but there is no way to return the URL from the heath value and there is no guarantee that two inputs are unique (Although the possibilities of them are not very small).

    I think it would be good to resolve Fardjad as much as you need in practice but it depends how strong it should be.

    I should note that I have never written or seen much. What I say in less URL services is expert expert advice, just how does it feel if I do not do any research :)

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -