Reputation: 4539
I have installed php 5.4.13 on Linux 2.6.34.
I have make simple client/server page using socket but it did not work on it.
It give permission denied error
Below is my php code
if (false == ($socket = socket_create(AF_INET, SOCK_STREAM, 0))) // create socket
{
$stringData= date("D M d, Y g:i A"). " socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
}
else
{
$timeout = array('sec'=>5,'usec'=>500000);
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);
if(false==($result = socket_connect($socket, $host, $port)))
{
$stringData= date("D M d, Y g:i A"). " socket_connect() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
socket_close($socket);
}
else
{
$stringData= date("D M d, Y g:i A"). " Socket connected succefully <br>";
echo $stringData;
if(false==(socket_write($socket, $command, strlen($command))))
{
$stringData= date("D M d, Y g:i A"). " socket_write() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
socket_close($socket);
}
else
{
if(false===($cmd = socket_read ($socket, 65536)))
{
//10060 for windows and 11 for linux
if(10060!=socket_last_error() && 11!=socket_last_error())
{
$stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
socket_close($socket);
}
switch(socket_select($r = array($socket), $w = array($socket), $f = array($socket), 5))
{
case 2:
$refused=1;
break;
}
if($refused==1)
{
$stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: Connection Refused <br>";
$ourFileHandle = fopen(SOCKET_LOG, 'a');
echo $stringData;
socket_close($socket);
}
}
else
{
echo "<pre>".html_entity_decode(print_r($cmd,true))."</pre>";
}
}
}
}
Above code work fine on command prompt but it gives error Permission denied when page open from any browser.
Command of run php from terminal : /usr/local/rootfs/php5/bin/php /www/socket_client.php
Upvotes: 3
Views: 18398
Reputation: 6003
In my case, the .sock file didn't have enough permission for the user running php (apache).
Was easier to fix.
Upvotes: 0
Reputation: 81
I had a similar problem:
<?php
fsockopen('udp://192.168.1.255', 7, $errno, $errstr, 2);
?>
Warning: fsockopen(): unable to connect to udp://192.168.1.255:7 (Permission denied) in /var/www/html/udp.php on line 1
Fatal error: Uncaught Exception: Cannot open UDP socket: Permission denied in
php.ini if the server let's you change this configuration:
allow_url_fopen = On // Was On
allow_url_include = Off // On Didn't change the problem
Terminal check Firewall:
ufw status
Status: Inaktiv
/usr/sbin/sestatus -v
SELinux status: disabled
Solution:
Broadcasting to 192.168.1.255 is not allowed, I changed it to the according IP address, i.e. 192.168.1.24
OR
By default broadcasts are disabled for sockeds. The socket_set_option command enables broadcast messages.
The follwoing script on the official PHP site:
<?php
# Wake on LAN - (c) HotKey (at SPR dot AT), upgraded by Murzik <[email protected]>
flush();
function WakeOnLan($addr, $mac)
{
$addr_byte = explode(':', $mac);
$hw_addr = '';
for ($a=0; $a < 6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false)
{
echo "Error creating socket!\n";
echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
}
else
{
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret < 0)
{
echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
}
$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
echo $e;
socket_close($s);
echo "Magic Packet sent (".$e.") to ".$addr.", MAC=".$mac;
}
}
#WakeOnLan('yourIPorDomain.dyndns.org', 'your:MAC:address');
#WakeOnLan('192.168.0.2', '00:30:84:2A:90:42');
#WakeOnLan('192.168.1.2', '00:05:1C:10:04:05');
//if you have switch or other routing devices in LAN, sendign to
// the local IP isn't helps! you need send to the broadcast address like this:
WakeOnLan('192.168.0.255', 'xx:xx:xx:xx:xx:xx');
hth someone
Additional Information:
https://www.php.net/manual/de/function.fsockopen.php
fsockopen() enabling it in PHP.ini
https://serverfault.com/questions/371000/how-to-enable-fsockopen-function-in-php
https://ubuntuforums.org/showthread.php?t=919407
https://forum.qnap.com/viewtopic.php?t=39154
Upvotes: 0
Reputation: 963
This issue seems to have nothing to do with PHP but with the permissions of the Apache web server (httpd).
I tried to fix the permission issue, but was not yet succesful.
If I disable the user switching in httpd.conf, my (Unix socket) connection works. However, this is only a quick development workaround is not the final solution, since the whole httpd runs as root this way.
#User _www
#Group _www
Upvotes: 0
Reputation: 10349
Check your php.ini settings for the parameter "safe_mode". It should be "Off".
Another problem could be caused by "selinux" that is blocking your mod_php (apache process) from connecting via socket. In this case:
# check if it's enabled:
/usr/sbin/sestatus -v
to add the connecting rule:
setsebool httpd_can_network_connect=1
If you want to disable it completely:
setenforce 0
Also, for debug reasons, disable any security module on apache/PHP. For example try to disable Suhosin if it's running.
Upvotes: 13