Jump to content

Help With Using PHP Within A Script


jerryb

Recommended Posts

Hello Folks!
I am new here so forgive me if I do not correctly follow your protocol.
I have created a live chat on my website. The chat works very well. However, I am attempting to add an 'alarm' of sorts that will indicate when someone either responds to the chat, or enters the chat.

 

I am attempting to create a setTimeout script that repeats every three seconds. At the beginning of the script I generate the length of the chat log file, decide whether the file is larger than before, then play a sound file if the file is larger than before. The script just keeps repeating the sound file.

 

My code is as follows...

 

<script>
 
(function(){
 
<?php
 
$handle = fopen("log.html", "r");
$contents = fread($handle,filesize("log.html"));
fclose($handle);
 
$strnew = strlen ($contents);
                    
if ($strnew > $strold) { ?>
 
var aSound = document.createElement('audio');
     aSound.setAttribute('src', '/sounds/door_chime.mp3');
     aSound.play(); 
 
<?php
                       }  
$strold = $strnew;    
                       ?>
 
        
    setTimeout(arguments.callee, 3000);
})
   ();
</script>
Link to comment
Share on other sites

You can't do that. All of the PHP is executed before the JS even exists. You can't loop or re-call PHP from JS. You'd have to use an AJAX request.

 

But what you're doing is crazy inefficient. To create a live chat you want to be using WebSockets, which create a persistent TCP connection between client and server. It is easily possible to achieve the alert with WebSockets because it is possible for all clients to know exactly when a message was pushed.

Link to comment
Share on other sites

I appreciate that response, but it appears that even they are still working on the technology :-\

 

WEB SOCKETS
THIS IS AN EXPERIMENTAL TECHNOLOGY
Because this technology's specification has not stabilized, check the compatibility for the proper prefixes to use in varius browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specs change.

 

Link to comment
Share on other sites

Just because Web Sockets haven't been formally standardized yet doesn't mean you cannot use them. The only problem would be legacy browsers.

 

However, I'd still stick to your current approach (polling). Sure, it's not super-efficient, but who cares when you clearly aren't running a big professional website? Making a request every few seconds is perfectly fine.

Link to comment
Share on other sites

Well, after some research and a bit of thought, I came up with a way to make this work....

 

 
$handle = fopen("log.html", "r");
$contents = fread($handle,filesize("log.html"));
fclose($handle);
 
$strnew = strlen ($contents);
 
$strold = $_COOKIE['strold']; 
 
if ($strnew > $strold) {?>
 
<script>
var aSound = document.createElement('audio');
     aSound.setAttribute('src', '/sounds/door_chime.mp3');
     aSound.play(); 
</script>
 
<?
setcookie("strold", $strnew);
 
}
 
 
The only problem I am having now is that this all works through a refresh code...
 
<meta http-equiv="refresh" content="30">
 
This causes problems if the refresh happens during a time when the user is filling in the comment form.
Any ideas on how I could possibly reload the entire page once the user submits the comments through the form submit?
Edited by jerryb
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.