The data comes from the form (announcement), written in $_POST . Then recorded in the session:

 $_SESSION['ad'][time()] = array( 'private' => $_POST['private'], 'seller_name' => $_POST['seller_name'], 'email' => $_POST['email'], 'phone' => $_POST['phone'], 'location_id'=> $_POST['location_id'], 'metro_id' => $_POST['metro_id'], 'category_id'=> $_POST['category_id'], 'title' => $_POST['title'], 'description'=> $_POST['description'], 'price'=> $_POST['price'] ); 

The uniqueness of the recording in the session is provided by the time() function. But here is one "but": if two announcements arrived at the same second, only one announcement will be recorded.

And finally, the question: how to ensure the uniqueness of each declaration without the time() function, or how to replace the time() function?

  • You assume that one user will send you ads more than 1 time per second? - Visman

3 answers 3

It is better to use the uniqid () function, it is just intended for such cases.

http://php.net/manual/ru/function.uniqid.php

  • 2
    Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - Peter Olson
  • Thanks for the comment! - user3127286

If you do not like the array_push() function, then try your luck with microtime() .

  • one
    It seems that the questioner did not understand the essence of the proposed solution, but it is good! Let the session be non-associative array. When a new ad is received, it is simply added to the tail of the array. If you need its unique id, immediately save the length of the array minus one: $id = array_push( $_SESSION['ad'], array('private'=>...)) -1; - Sergiks
  • Thanks for the clarification. I hoped that the one who asked would read the description of the function on his own and get to the solution. It would be more useful from an educational point of view. :) - Dmitriy
  • Yes, I really understood the essence of the proposed solution only after clarification. A unique id is needed in order to send it by the GET method to another page and extract (display) an announcement (with this id) from the $ _SESSION array. Prompted another solution. In the code, instead of $ _SESSION ['ad'] [time ()] = array (...) will be $ _SESSION ['ad'] [] = array (...) - Ilnar

Try this option:

 $hash = substr(md5(mt_rand()), 0, 8);