-4

I need to create unique id on user's every click link. Let I have a code

<a class="popup-text" href="examplelink&aff_sub=<?php echo $up_id; ?>" data-toggle="modal">Upto Rs.10000 Cashback LED TVs 
+ Get additional upto Rs. 4 Cashback from afft</a>

when user click on that link $up_id should be automatically generated. $up_id should change every time when user click on that link. $up_id comes from following code

<?php 
$up_id=md5(uniqid(rand(), true)); 
?>

but I am getting same id every time.

Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
nehaJ
  • 77
  • 1
  • 11
  • 2
    take md5(timestamp), it will be unique. – Niranjan N Raju Oct 19 '15 at 07:24
  • Please check again it is not possible @nehaJ – Abhishek Sharma Oct 19 '15 at 07:26
  • It happens because php is static, it's loaded just one time. To get a different id, you need to reload de page. The solution is update the id using javascript or make a ajax request and get a new id every time you click on the link... And no one understood the real problem. – Iago Oct 19 '15 at 07:33
  • ok @lago. Do u have any idea how should I update it using javascript – nehaJ Oct 19 '15 at 07:48

3 Answers3

0

Best solution for this would be, get current timestamp and take md5() of thst

$up_id = md5(current time stamp);
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
  • `md5(uniqid(rand(), true))` already generate a random string based on the time. See [the doc](http://php.net/manual/pt_BR/function.uniqid.php). – Iago Oct 19 '15 at 07:37
0

Instead of PHP you can generate random number by using javascript, tryout following code:

<a class="popup-text" href="examplelink" onclick="location.href=this.href+'?&aff_sub='+Math.floor((Math.random()*1000)+1);return false;" data-toggle="modal">Upto Rs.10000 Cashback LED TVs 
         + Get additional upto Rs. 4 Cashback from afft</a>
hvs9
  • 70
  • 6
-1

You can easily achieve this with the following function:

function random_string($length)
{
   $string = "";
   $chars = "abcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   $size = strlen($chars);
   for ($i = 0; $i < $length; $i++) {
       $string .= $chars[rand(0, $size - 1)];
   }
   return $string; 
}

Here you first generate an empty string, then you define a string with all chars you want. After you also counted the numbers of chars you can start with filling your new string. You just use a for-loop to do this. Everytime the loopcontent is called, there is one random char (from your $char) added to your empty string.

You use this function (for example if you want to store your ID in a variable) by simply typing:

$password = rand_string(15) //the number specified in brackets is the amount of characters in your password

So with this function, you can just always generate a random string (in your case, a unique ID).

Let me know, if you're experiencing any issues.

nameless
  • 1,483
  • 5
  • 32
  • 78