4 Years
Discussion Span
Last Post by diafol

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.

Edited by Banderson


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());
        setInterval(updateCaptcha, 300000);

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() {
}, 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.


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 )
      $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.


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.


@ 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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.