0

got this code:

<?php
function test ($url){
$starttime = microtime(true);
$valid = @fsockopen($url, 80, $errno, $errstr, 30);
$stoptime = microtime(true);
echo (round(($stoptime-$starttime)*1000)).' ms.';

if (!$valid) {
   echo "Status - Failure";
} else {
   echo "Status - Success";
}
}
    test('google.com');
?>

i want to use something like this:

<script>
setInterval(function(){<?php  test('google.com'); ?>},3000);
</script>

How di i call php function 'test' with setInterval?

1
  • 1
    You are conflating two seperate application domains - PHP on the server, and Javascript in the client browser. For the browser to communicate to the server you have two options - Form post, or Ajax request. Commented Mar 4, 2013 at 13:21

1 Answer 1

1

You can send AJAX request to the server to execute that script after x seconds.

var milliSeconds = 1000; // <-- As an example
setInterval( function() {
  // <-- Your AJAX request here
}, milliSeconds);
Sign up to request clarification or add additional context in comments.

3 Comments

@karaxuna oh yeah, it is :)
do i store the script in another file, and then for ajax request i write xmlhttp.open("GET","script.php",true); xmlhttp.send(); ? dont quite get this ajax request stuff..
yeah save that file separately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.