What I an wanting to do here is have the script email me when the user count reaches X number of listeners on my RR Scanner Feed.
Is This Possible?

<?php
    $filename = '<script language="JavaScript" src=""></script>'; 
    $fp = fopen( $filename,"r"); 
    if ($filename >= X) {
  //// ($email Me @ My Email Address Or Any Address I Add Here) ////
  exit();
}                       
    fclose( $fp ); 
    print "Online=$filename";   
?>

Dani AI

Generated

A robust production approach builds on ' idea (fetch the remote script and extract the number) while adding error checking, persistence and throttling so alerts are reliable and not spammy.

Fetch and parse

  • Server-side fetch works either with file_get_contents (requires allow_url_fopen) or with cURL as a fallback; check the PHP docs for that setting if fetches fail (see allow_url_fopen docs and the cURL manual).
  • Parse defensively: verify the HTTP response, confirm the payload contains the expected pattern, and convert the extracted text to an integer before any comparisons. Log or abort if parsing fails so a format change on the remote side does not trigger wrong emails.

Persist and threshold logic

  • Store the last observed count in a small file or a database row. Use atomic writes (for example file_put_contents with LOCK_EX) so concurrent runs do not corrupt the value (see file_put_contents).
  • Only trigger an email when the count crosses the configured threshold (e.g., last < X and current >= X). Add simple hysteresis or a time-based suppression window (suppress repeat alerts for N minutes) to avoid repeated messages during flapping.

Running and delivery

  • Run the script from cron at an interval that balances timeliness with politeness to the remote service (and to stay within any API or TOS limits). Record failures and successes to a log for later debugging.
  • Use authenticated SMTP (PHPMailer or similar) if deliverability is important; PHP mail() is OK for basic alerts but can be blocked by spam filters (mail() docs).

Edge cases and cautions

  • Handle network errors, empty responses, and format changes gracefully. Confirm permission to poll the remote feed and respect any published rate limits or terms. With these safeguards the solution suggested by becomes a reliable alerting mechanism for 's listener threshold.

Recommended Answers

All 8 Replies

You'd have to store the file open count somewhere (file/database), but you would need a decrement too when sombody stops using it.

You'd have to store the file open count somewhere (file/database), but you would need a decrement too when sombody stops using it.

Thanks I see what you are saying I have found this code here but for the life of me I cant figure out how to get it to read the above mentioned

$filename = '<script language="JavaScript" src=""></script>';

and get it to write to a txt file.

<?php
    $filename = "PHPCounter.txt"; 
    $fp = fopen( $filename,"r");             
    $Old = fread($fp, 100);
    fclose( $fp );
    $Old = split ("=", $Old, 5);        
    $NewCount = $Old[1] + '1';        
    $New = "Online=$NewCount";        
    $fp = fopen( $filename,"w+");         
    if (flock($fp, 2)) { 
      fwrite($fp, $New, 100);
    }         
    fclose( $fp );

    print "Online=$NewCount";     
?>

What do you want with that script tag ?

What do you want with that script tag ?

What I am wanting is have the script email me when the user count reaches X number of listeners on my RR Scanner Feed.
So it would read the file

$filename = '<script language="JavaScript" src=""></script>';

and write it to a txt file and then email me when the listener count reaches X number if listeners.

That script is hosted by another party, and you want to read the number it returns in the document.write ? And based on that number send an email ?

That script is hosted by another party, and you want to read the number it returns in the document.write ? And based on that number send an email ?

pritaeas, Yes sir that is correct.

I think this should do it (can't test right now):

$filename = "";
$data = file_get_contents($filename);
preg_match('/document\.write\(\'(.*?)\'\);/i', $data, $result)
$listeners = $result[1];
echo $listeners;

I think this should do it (can't test right now):

$filename = "";
$data = file_get_contents($filename);
preg_match('/document\.write\(\'(.*?)\'\);/i', $data, $result)
$listeners = $result[1];
echo $listeners;

Thank you so much. That was it and works great its nice to come on here and always get the help needed. its people like you that make Daniweb what it is today, Thanks again for the help..

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.