PHP code I need help with

11/30/2022 01:17 denominator#1
Code works fine using iis but my webhost has an issue with it code is as follows

Code:
<?php
$ts_ip = "External IP"; // Change to your server's IP external or domain name
$ts_port = "8000"; // Make sure this port is open on the router or firewall

$output = @fsockopen("$ts_ip", $ts_port, $errno, $errstr, 2);
socket_set_timeout($output, 000002);

if (!$output) {
echo "<font color=#FFFFFF>Shadowlands is </font><FONT COLOR=#DD0000><B>OFFLINE</B></FONT>";
} else {
echo "Shadowlands is <FONT COLOR=#00DD00><B>ONLINE</B></FONT>";
}
@fclose($output);
?>
The reason why it possibly won't work is "Fatal error: Uncaught Error: Call to undefined function fsockopen()"
11/30/2022 10:03 Devsome#2
You need to enable / install fsockopen. You can run a simple PHP snippet and search for „socket“ (I guess) to see if its enabled or not.

Code:
<?php
phpinfo();
12/01/2022 00:34 denominator#3
Yup "Sockets Support" Enabled

[Only registered and activated users can see links. Click Here To Register...]

That is my host site not localhost you can see for yourself, any ways I've managed to get it to show online even if the game server itself is offline. Not very happy about it but at least it shows online now :(
12/01/2022 00:35 Carzen#4
Are you sure your port was open?or no one use port 8000 on your system
12/01/2022 01:17 Devsome#5
You can also try to catch the expection and see whats wrong.

Code:
try {
    $output = @fsockopen($ip, $port, $errno, $errstr, 5);
    @fclose($output);
    return true;
} catch (\Exception $e) {
    return $e;
}
12/01/2022 03:58 denominator#6
Quote:
Originally Posted by Carzen View Post
Are you sure your port was open?or no one use port 8000 on your system

Port 8000 was an example and yes the port in question is definitely open, it is running on a vps and if the port was not open my game client would not connect but it does

Quote:
Originally Posted by Devsome View Post
You can also try to catch the expection and see whats wrong.

Code:
try {
    $output = @fsockopen($ip, $port, $errno, $errstr, 5);
    @fclose($output);
    return true;
} catch (\Exception $e) {
    return $e;
}
So like this

Code:
<?php
$ts_ip = "external ip"; // Change to your server's IP external or domain name
$ts_port = "8000"; // Make sure this port is open on the router or firewall
try
{
$output = @fsockopen("$ts_ip", $ts_port, $errno, $errstr, 2);
socket_set_timeout($output, 000002);

if (!$output) {
echo "<font color=#FFFFFF>Shadowlands is </font><FONT COLOR=#DD0000><B>OFFLINE</B></FONT>";
} else {
echo "Shadowlands is <FONT COLOR=#00DD00><B>ONLINE</B></FONT>";
}
@fclose($output);
return true;
} 
catch (\Exception $e) 
{
    return $e;
}
?>
If correct where will I find the information needed to rectify it being as it's not hosted on my pc?

Code:
[01-Dec-2022 03:34:04 UTC] PHP Warning:  fsockopen(): unable to connect to externalip:8000 (Connection refused) in /home/anarchyc/public_html/shadowlands.php on line 20
[01-Dec-2022 03:34:04 UTC] PHP Warning:  socket_set_timeout() expects parameter 1 to be resource, bool given in /home/anarchyc/public_html/shadowlands.php on line 21
[01-Dec-2022 03:34:04 UTC] PHP Warning:  fclose() expects parameter 1 to be resource, bool given in /home/anarchyc/public_html/shadowlands.php on line 28
The errors as far as I can see, so if I remove the @ then I get three errors but if I include the @ then I just get the one error
Code:
[01-Dec-2022 03:57:11 UTC] PHP Warning:  socket_set_timeout() expects parameter 1 to be resource, bool given in /home/anarchyc/public_html/shadowlands.php on line 21
12/01/2022 10:48 Devsome#7
You wrote your answer.
Quote:
fsockopen(): unable to connect to externalip:8000 (Connection refused)
try to open the port from your server where you wanna connect. You can also try with telnet from a cmd to connect and see if its working or not.
12/01/2022 10:51 Carzen#8
or try use portchecker try search on google you should check where you put the php files
12/01/2022 12:07 denominator#9
Yeah already checked if port is open and it actually is. From telnet I get
Code:
WORLD OF WARCRAFT CONNECTION - SERVER TO CLIENT - V2
Also using a slightly different code

Code:
<?php
//ip address of you server
$ip = "191.101.59.118";
//port of your server
$port = 8085;

// create a connection using the fsockopen function

$connectionStream = fsockopen($ip, $port, $errno, $errstr, 2);

//check if the connection worked and the server is online

if($connectionStream >= 1)
{
    echo "<font color=#FFFFFF>Shadowlands is </font><FONT COLOR=#DD0000><B>OFFLINE</B></FONT>";
    //echo out information if server is online
} 
else
{
    echo "<font color=#FFFFFF>Shadowlands is </font><FONT COLOR=#00DD00><B>ONLINE</B></FONT>";
    //echo out information if server is offline
}
?>
Just that now it is always saying online even if the game server and port are offline lol, all necessary ports are added to the firewall ingoing and outgoing.

Still the same unable to connect issue, okay I give up
12/01/2022 19:56 False#10
The Problem is your if statement, you check if $connectionStream is larger or equals to 1.
But fsockopen don't returns an int at all, check the [Only registered and activated users can see links. Click Here To Register...].

Quote:
fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()). If the call fails, it will return false
to fix you're issue just replace it with a "is_resource" check and switch the content of the if and else like this:

PHP Code:
if(is_resource($connectionStream))
{
    echo 
"<font color=#FFFFFF>Shadowlands is </font><FONT COLOR=#00DD00><B>ONLINE</B></FONT>";
    
//echo out information if server is online
}
else
{
     echo 
"<font color=#FFFFFF>Shadowlands is </font><FONT COLOR=#DD0000><B>OFFLINE</B></FONT>";
    
//echo out information if server is offline

After this change it should work fine!
Just as info, you should not use PHP 7.4 it's end of life since today ([Only registered and activated users can see links. Click Here To Register...]).
12/02/2022 00:20 denominator#11
Appreciated, however I'm still getting the unable to connect. I have spoken with my host and they said to use a common port >.< Can't seem to get it into their thick skulls that my original code WORKS in IIS but fails with their service.

I'm just going to host it all on my PC because it works
12/13/2022 00:36 aistis1345#12
install fsockopen
12/13/2022 02:07 denominator#13
Quote:
Originally Posted by aistis1345 View Post
install fsockopen
It IS installed, anyways it doesn't really matter any more. It works fine on my own IP so I will just have to stick with that
05/08/2023 12:25 gulshan212#14
Hello this is Gulshan Negi
Well, the error message "Fatal error: Uncaught Error: Call to undefined function fsockopen()" indicates that the fsockopen() function is not enabled on your web host's server. This function is a built-in PHP function that is used to open a network connection to a remote server.
You may need to check with your web host to see if the function is supported or if there are any configuration issues that might be preventing it from working. Alternatively, you could try using a different function such as cURL to establish the network connection instead of fsockopen().
Thanks
05/08/2023 19:43 False#15
Quote:
Originally Posted by gulshan212 View Post
Hello this is Gulshan Negi
Well, the error message "Fatal error: Uncaught Error: Call to undefined function fsockopen()" indicates that the fsockopen() function is not enabled on your web host's server. This function is a built-in PHP function that is used to open a network connection to a remote server.
You may need to check with your web host to see if the function is supported or if there are any configuration issues that might be preventing it from working. Alternatively, you could try using a different function such as cURL to establish the network connection instead of fsockopen().
Thanks
Nice AI generated answer, keep it up :confused: