garyed Posted April 27, 2020 Share Posted April 27, 2020 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. Quote Link to comment Share on other sites More sharing options...
chhorn Posted April 27, 2020 Share Posted April 27, 2020 if the software runs on your machine with enough priviledges you can play a sound by using exec() with appropriate comand line arguments to a local mp3-player. Quote Link to comment Share on other sites More sharing options...
garyed Posted April 27, 2020 Author Share Posted April 27, 2020 (edited) 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 April 27, 2020 by garyed Quote Link to comment Share on other sites More sharing options...
chhorn Posted April 27, 2020 Share Posted April 27, 2020 (edited) the only hint i found was something like exec('powershell -c (New-Object Media.SoundPlayer "C:\Windows\Media\notify.wav").PlaySync();'); but that's at least windows OS specific, but actually worked on my machine Edited April 27, 2020 by chhorn Quote Link to comment Share on other sites More sharing options...
garyed Posted April 27, 2020 Author Share Posted April 27, 2020 I'm running Linux Ubuntu but so far everything I've tried runs on the clients machine when they access the page & nothing has run on the server when they do so. Is "powershell -c" a Windows specific command or can it be used with Linux? Quote Link to comment Share on other sites More sharing options...
chhorn Posted April 27, 2020 Share Posted April 27, 2020 Yes, windows OS, there's a linux hint in the thread: https://stackoverflow.com/questions/8773043/how-to-play-a-sound-file-using-php-script-only Quote Link to comment Share on other sites More sharing options...
garyed Posted April 27, 2020 Author Share Posted April 27, 2020 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. Quote Link to comment Share on other sites More sharing options...
garyed Posted April 27, 2020 Author Share Posted April 27, 2020 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? Quote Link to comment Share on other sites More sharing options...
kicken Posted April 28, 2020 Share Posted April 28, 2020 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. Quote Link to comment Share on other sites More sharing options...
garyed Posted April 28, 2020 Author Share Posted April 28, 2020 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. Quote Link to comment Share on other sites More sharing options...
garyed Posted April 28, 2020 Author Share Posted April 28, 2020 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). Quote Link to comment Share on other sites More sharing options...
kicken Posted April 29, 2020 Share Posted April 29, 2020 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.