Jump to content

Recommended Posts

This might be more than just a php question but here goes anyways. 

I have designed a php chat room page on my home Apache server just for a few friends & family. The problem I have is the only way for me to know if anyone is on the chat room page is if I'm actually viewing the php page. Is there a way that I can have the page alert me when someone accesses it? I was thinking something like activating  a popup in my browser or even a beep so as long as I'm at my computer with my browser open that I will know someone has accessed the page.       

Link to comment
https://forums.phpfreaks.com/topic/310652-alert-when-page-is-called/
Share on other sites

So what you're saying is that I can put an exec() command on the php page that when it is accessed by another computer it would open up my mp3 player.  That sounds simple enough but I'm not sure I know how to do it. I'll give it a try.

Edited by garyed

I'm not having any luck even getting a .wav file to play through a php file at all even from the server side let alone trying to get it to work from the client side. I've tried the suggestions in the link & nothing has worked so far. 

I must be missing something. I tried this in my php file:   

exec('/usr/bin/play /home/me/song1.wav');

When I access the php file I just get a blank screen. If replace  the play command with "ls -l" it will show the file info when I access the file so i know I have the path right. If I type the same play command in a terminal the song will play so I don't understand what I'm missing. 

Well I finally got music playing through php by using this code that someone had posted in this forum a while back:

$_wav = "my.wav";
$_play = "<embed src='".$_wav."'>";
echo $_play;

The problem is it only works on the computer who is accessing the page.  Is there a way to make it work on my computer(the server) or in my browser when someone accesses the page so I can know when the page is being accessed? 

6 hours ago, garyed said:

I must be missing something. I tried this in my php file:   


exec('/usr/bin/play /home/me/song1.wav');

 

Does /usr/bin/play exist?  If not you need to install it first.

You need to find some program that will allow you to play media from the command line.  Just test things out via a terminal or ssh session until you get it working that way, then take whatever command you come up with and put that into your PHP page with exec().

This is all assuming the computer running apache and the one you want the sound played on are the same.  If they are not you will need another solution.

9 hours ago, kicken said:

Does /usr/bin/play exist?  If not you need to install it first.

You need to find some program that will allow you to play media from the command line.  Just test things out via a terminal or ssh session until you get it working that way, then take whatever command you come up with and put that into your PHP page with exec().

This is all assuming the computer running apache and the one you want the sound played on are the same.  If they are not you will need another solution.

I actually tried all the steps you mentioned. I issued the same command I used in the exec() command on the php page through the terminal & the .wav file plays perfectly. I even echoed the " ls -l" command in the php file & got the correct results to make sure the path worked to the .wav file.

Maybe what I'm trying to do can't be done in php but I would think it could be done.

All I'm trying to do is be alerted in some way on my computer which is the Apache server, whenever someone accesses a particular web page on that server.

Whether it's playing a wav file or sending a pop up on the screen it doesn't matter as long as it shows up on my computer. So far everything I've been able to do only creates an action on the client's computer that is accessing the web page but I can't get the client to create an action on the server(my computer).  

I setup a Linux VM and tried this as it should be possible.  It seems, at least with Linux MX but probably similar on other distros, that a user needs to be in the audio group in order to access the audio devices. 

Using the second and third parameters to exec, you can get the output of the command and that is where I started at.  The -q flag suppresses the progress bar and the 2>&1 redirects the error output to standard out so it can be captured into $output.

exec('/usr/bin/play -q notification.wav 2>&1', $output, $ret);
var_dump($output, $ret);

This produces the output

array(11) {
  [0]=>
  string(48) "Home directory not accessible: Permission denied"
  [1]=>
  string(48) "Home directory not accessible: Permission denied"
  [2]=>
  string(57) "ALSA lib confmisc.c:767:(parse_card) cannot find card '0'"
  [3]=>
  string(115) "ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory"
  [4]=>
  string(66) "ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings"
  [5]=>
  string(110) "ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory"
  [6]=>
  string(63) "ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name"
  [7]=>
  string(109) "ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory"
  [8]=>
  string(82) "ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory"
  [9]=>
  string(63) "ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default"
  [10]=>
  string(24) "Floating point exception"
}
int(136)

With the default PHP-FPM setup the PHP script runs as user www-data.  The first two errors about the home directory are due to that user's home directory (/var/www) not being writable by the user.  I fixed this by changing ownership of /var/www to www-data with chown -R www-data:www-data /var/www

The remaining errors are due to the sound devices (in /dev/snd/) being limited in access to users in the audio group.  This I fixed by adding the audio group to the list of groups www-data belongs to using usermod -G audio www-data

After those changes and restarting Apache and PHP-FPM I was able to hear the sound when loading my test script.

 

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.