Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Web Development
You last visited: Today at 10:53

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



PHP code I need help with

Discussion on PHP code I need help with within the Web Development forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
PHP code I need help with

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()"
denominator is offline  
Old 11/30/2022, 10:03   #2
dotCom
 
Devsome's Avatar
 
elite*gold: 9842
The Black Market: 107/0/0
Join Date: Mar 2009
Posts: 16,845
Received Thanks: 4,675
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();
Devsome is offline  
Thanks
1 User
Old 12/01/2022, 00:34   #3
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
Yup "Sockets Support" Enabled



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
denominator is offline  
Old 12/01/2022, 00:35   #4
 
elite*gold: 0
Join Date: Nov 2022
Posts: 3
Received Thanks: 0
Are you sure your port was open?or no one use port 8000 on your system
Carzen is offline  
Old 12/01/2022, 01:17   #5
dotCom
 
Devsome's Avatar
 
elite*gold: 9842
The Black Market: 107/0/0
Join Date: Mar 2009
Posts: 16,845
Received Thanks: 4,675
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;
}
Devsome is offline  
Thanks
1 User
Old 12/01/2022, 03:58   #6
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
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
denominator is offline  
Old 12/01/2022, 10:48   #7
dotCom
 
Devsome's Avatar
 
elite*gold: 9842
The Black Market: 107/0/0
Join Date: Mar 2009
Posts: 16,845
Received Thanks: 4,675
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.
Devsome is offline  
Old 12/01/2022, 10:51   #8
 
elite*gold: 0
Join Date: Nov 2022
Posts: 3
Received Thanks: 0
or try use portchecker try search on google you should check where you put the php files
Carzen is offline  
Old 12/01/2022, 12:07   #9
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
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
denominator is offline  
Old 12/01/2022, 19:56   #10


 
False's Avatar
 
elite*gold: 0
The Black Market: 243/0/0
Join Date: Apr 2011
Posts: 11,117
Received Thanks: 2,436
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 .

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 ().
False is offline  
Thanks
2 Users
Old 12/02/2022, 00:20   #11
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
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
denominator is offline  
Thanks
1 User
Old 12/13/2022, 00:36   #12
 
elite*gold: 78
Join Date: May 2020
Posts: 16
Received Thanks: 8
install fsockopen
aistis1345 is offline  
Thanks
1 User
Old 12/13/2022, 02:07   #13
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
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
denominator is offline  
Old 05/08/2023, 12:25   #14
 
gulshan212's Avatar
 
elite*gold: 0
Join Date: Apr 2023
Posts: 5
Received Thanks: 0
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
gulshan212 is offline  
Old 05/08/2023, 19:43   #15


 
False's Avatar
 
elite*gold: 0
The Black Market: 243/0/0
Join Date: Apr 2011
Posts: 11,117
Received Thanks: 2,436
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
False is offline  
Reply


Similar Threads Similar Threads
[Selling] [CODE][/CODE][BATTLEBYE]-✅ESP,GLOW,NO-RECOIL,FLY,TP,AIM.✅[CODE][/CODE]
07/19/2019 - Rainbow Six Siege Trading - 0 Replies
I'm passing here to tell you that the cheat -BattleByeR6- is the better cheat, 100% reliable. In this cheat -BatlleByeR6- there is no risk of getting banned, it will be added several other options in our cheat -BattleByeR6- ● Interested in adding contact at discord : BattleBye #9647 Or enter into our discord BattleBye : https://discord.gg/dbHUSmu



All times are GMT +1. The time now is 10:53.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.