-1

I have code that generates random strings, but I want to generate random strings only once per day.

This is the code generating a random string by every page load. How can I achieve this?

function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));

for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}

return $key;
}
$sehiradi = "diyarbakir";
$subdomainadi = "$sehiradi."."".random_string(5);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

3 Answers3

2

You can achieve the same result using a hash function with the current date.

$sehiradi.substr(sha1('someString'.date('Ymd')),0,5);

This will work for you, as the hash will always bring the same result with the same input. Everyday, the function will use a different input value, as the day changes.

EDIT: Using your values you can have:

$sehiradi = "diyarbakir";
$subdomainadi = $sehiradi.substr(sha1('someString'.date('Ymd')),0,5);

Please replace 'someString' with some key that you have in mind. The point is to make it hard for someone to know the source of your "random" string.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
0

Have you tried looking into php cookies and storing it into a cookie file which has a ttl for 1 day? I only say this cause you mentioned you didn't want to store it in the database

Moe
  • 129
  • 3
  • 1
    cookies are user unique, what if he wants the same string for all users? –  Oct 11 '18 at 21:07
  • Yes I want same string for all users but only one day another day another string for all users – Şahin Coşkun Oct 11 '18 at 21:08
  • then I would suggest storing it to a txt file with the date generated. @FrankerZ is right he needs a storage system. – Moe Oct 11 '18 at 21:08
0

Since you don't want to store this information in a database and might not want to use cookies as they are user specific, here is a different approach:

Use the current Date (only day, month and year) as the seed for your random number generator. This will make it so it changes each day and is the same for all users without saving a single bit of data.

Ciciboy1
  • 51
  • 1
  • 5
  • 1
    this is more of a comment to which in so many words, I've said already and a few times. – Funk Forty Niner Oct 11 '18 at 21:14
  • How can i add current date function to my code ? Any idea? – Şahin Coşkun Oct 11 '18 at 21:17
  • @FunkFortyNiner well... Not really... Your approch would be to check if today is still a specific day, which means you need to keep the time when you generated the last string. – Ciciboy1 Oct 11 '18 at 21:26
  • 1
    @Ciciboy1 No, that isn't it. I'd of placed an answer of my own but couldn't since a one-liner I wrote a few years ago, is on a different PC that I'm on now and can't remember how I did it and it was weirdly slick *lol!*. – Funk Forty Niner Oct 11 '18 at 21:27
  • @FunkFortyNiner now im really interested in how you did that without saving anything anywhere, please tag me when you find your solution – Ciciboy1 Oct 11 '18 at 21:30
  • @Ciciboy1 Heh! Yeah... I like to fool around with code and some crazy thoughts sometimes. I wrote a "quasi table/column" bind for an sql prepared statement once using contants (tables/columns cannot be binded in a prepared statement) *theoretically*. I've got that too on that other PC. Well, if I think of it and I find it anytime soon, sure I'll share it in the answers area and you could get it off there :-) – Funk Forty Niner Oct 11 '18 at 21:32