Create random code when loading page PHP [closed]

-2

I'm creating a system for a real estate, I need to create a random code for each insert.

Examples: C for home = C5241a4d / T for land = T77d4a1r

I want to play this code in an input, to create the folder that will receive the files, among other things.

I think it looks something like the example below:

    <?
$num = rand(1, 10);
  echo "O número gerado foi: " . $num;
?>

I would like instructions.

    
asked by anonymous 13.07.2018 / 02:17

2 answers

1

I would not do this in any way, there are no guarantees of "integrity", names can be repeated mixing files, folders that will be empty can be created and remain there without link with the bank, other problems can occur like this (depending on system).

You can solve everything based on the item ID in the database table, when you create a table in a reasonable format, usually in most database types you have the ID column, which is usually AUTO_INCREMENT , ie using this ID will solve the problem.

Then when registering the property, take the ID, for example at the time of registering the property you use insert, then soon after use LAST_INSERT_ID , something like:

INSERT INTO imoveis (categoria, descricao) VALUES ('casa', 'foo bar baz');
SELECT LAST_INSERT_ID();

Then you can create the name of a folder based on this ID.

Of course, since you are using the PHP API, you could do everything in it, you can create a function to generate the folder based on the ID:

function GerarPasta($categoria, $id, $target) {
    $prefixo = strtoupper($categoria{0}); // Gera C (casa), T (terreno), A (apartamento)

    $path = $prefixo . str_pad($id, 10, '0', STR_PAD_LEFT);

    if (!is_dir($path)) mkdir($path, 0755);
}

The use would be:

GerarPasta(<categoria>, <id do banco do imovel>, <pasta aonde vai criar as pastas>);

In this function I did not generate strings with youtube url style format, but only based on ID, then real estate with ids like 1, 2, 3, 4 and 100 will generate:

GerarPasta('casa', 1, '/foo/bar/');        // Gera pasta: /foo/bar/C0001
GerarPasta('terreno', 2, '/foo/bar/');     // Gera pasta: /foo/bar/T0002
GerarPasta('apartamento', 3, '/foo/bar/'); // Gera pasta: /foo/bar/A0003
GerarPasta('casa', 4, '/foo/bar/');        // Gera pasta: /foo/bar/C0004
GerarPasta('casa', 100, '/foo/bar/');      // Gera pasta: /foo/bar/C0100

13.07.2018 / 18:44
1

I'll give you an opinion:

I want to play this code in an input, to create the folder that will receive the files, among other things.

Seriously? You will really trust information from the client (understand "browser").

It is not a good idea to use a value of input to create a folder based on a value from that input.

But objectively answering the question: You can generate unique values through some PHP functions like uniqid .

   $valor = uniquid()

You can pass in the first parameter a value to be the prefix of the generated code, and in the second parameter to "leave more single still", if necessary.

If you want to put this in an input, you would just do something like:

<input type="hidden" value="<?= uniquid() ?>" name="codigo_aleatorio">

But honestly, mistrusting to the end that will be used, I would not advise.

If you want to have a unique code to create a folder after submitting a form, use a value in the session, or it generates at the time of upload, or anything else, except trust what comes from the client to do that, for security .

    
13.07.2018 / 02:44