• how to set random string that refresh after every 5 min itself without reloading the webpage in php ?

What do you mean by a random string?

What about the refresh? Are you talking about an AJAX call?

i mean suppose i want a user to enter a random String that i will generate through code and if the user is not able to fill the in 5 min then the again a different key will bw given to him through code

I am assuming you are requiring a captcha of some sort.
As pritaeas stated, look into AJAX.
You can refresh a css DIV using AJAX without refreshing the entire page.
Generate the string with PHP and embed the string/function into the css DIV.
Make an AJAX call whenever you want it to refresh, by doing this you should generate a function call to your PHP.
Update your database and/or sessions to the newly generated string to allow comparison.

Doesn't really sound like a very secure captcha check though. If the text is just floating around in a DIV on the page, not as a part of an image or some other form hard to extract the string from, then it's more or less pointless. No bot would be stopped by that.

If this is really for a captcha check, then an AJAX request wouldn't even be needed. You could have PHP embed the keyword into an image and deliver that to a HTML <img> tag like any other image. Then that image's src attribute could be updated every so often by JavaScript to bring down a new one.

For example: (Using jQuery for simplicity)

<img id="captcha_check" src="/img/captcah_loading.png" alt="Captcha">

<script type="text/javascript">
    $(function() {
        var updateCaptcha = function() {
            $("#captcha_check").attr("src", "/generate_captcha.php?ts=" + (new Date()).getTime());
        }
        updateCaptcha();
        setInterval(updateCaptcha, 300000);
    });
</script>

The timestamp attached to the new src would make sure you get a new image, rather than a cached one.

If it's actually just a matter of bringing down a new string from PHP into a div, with the help of a library like jQuery, it gets even simpler. These three lines would accomish that.

setInterval(function() {
    $("#target_div").load("generate_random_string.php");
}, 300000);

The jQuery .load function replaces the content of the target element - the element with the ID "target_div", in this case - with the results of whatever the specified URL returns. The setInterval makes that happen ever 300000 milliseconds; ever 5 minutes.

Member Avatar for diafol

THis certainly sounds like a captcha. But it doesn't make much sense to me. A user has to wait 5 minutes for a new string? Or can they just refresh the page? Or open multiple tabs or windows?

function rand_string( $length )
   {
      $str='';
      $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
      $size = strlen( $chars );
      for( $i = 0; $i < $length; $i++ )
      {
          $str .= $chars[ rand( 0, $size - 1 ) ];
      }
      return $str;
  }
  $random = rand_string(6);
  echo $random;

i am using this code and i just want to add time insert time periad after which it will automatically refreshed like after every 5 min

OK, then refer to the second code snippet in my last reply. It does exactly that.

hey dear atil can you please write a code that set timer in my code

No. Your code is PHP. A timer like this needs to be written in JavaScript.

If this is all of your code, you need to create a separate HTML page with JavaScript like the one I posted above to call the script you posted above, to fetch the random string into the HTML page.

Member Avatar for diafol

@ shika_1

Several times now you've asked for code or for people to just repeat stuff that's available on sites to which you've been given links. We don't mind helping you, but you've got to do the work yourself. Spoonfeeding won't make you a better programmer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.