Upcoming Games

(UTC times)


Full list
Add a game

Upcoming Events

No events to display

PHP expert wanted

You are here: Home > Forum > General > Website / Forum > PHP expert wanted

Page 1 of 1

PHP expert wanted 17/01/2010 at 20:35 #556
GeoffM
Avatar
6376 posts
Online
There are a couple of PHP scripts on this website, firstly to obtain your IP address, and secondly to check whether the appropriate port is open. The former works fine; the latter doesn't. It is failing to detect an open port on a machine that I know is open - and double checking with http://www.yougetsignal.com/tools/open-ports/ confirms that the port is open.

If anybody can (a) fix the problem; (b) check the PHP/HTML code; (c) tidy up, then I'd be grateful.

whatsmyip.php:

<?php
$userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
if($userIP == "")
{
$userIP = $_SERVER['REMOTE_ADDR'];
}
echo "<html><head><title>SimSig</title>";
echo '</head><body><center><font size="+1">Your IP address is:<p><font size="+2">' . $userIP . '</font></font><br>';
echo '<p>You can now perform a port check. This will check that this website can connect to the port of your choice on your PC. Start SimSig in Server mode and select the port number below. Finally, press "Check port" to check whether the port can be accessed. If this succeeds then you are likely to be able to host a game. If not, check your network and/or router settings and try again.</p>';
echo '<table><tr>';
echo '<form action="http://www.SimSig.co.uk/portcheck.php" method="post">';
echo '<tr><td><input type="radio" name="port" value="50505"></td><td>Primary port (50505)</td></tr>';
echo '<tr><td><input type="radio" name="port" value="50507"></td><td>Secondary port (50507)</td></tr>';
echo '<tr><td><input type="radio" name="port" value="0"></td><td>Port:<input type="text" name="portnum" size="5"></td></tr>';
echo '<tr><td>Address</td><td><input type="text" name="ip" value="' . $userIP . '"></td></tr>';
echo '<tr><td colspan="3" align="center"><input type="submit" value="Check port"></td></tr>';
echo '</table>';
echo '</form>';
echo '</center></body></html>';
?>


portcheck.php:

<?php
$userIP = $_POST['ip'];
$radio = $_POST['port'];

if ($radio == "50505")
$userPort = 50505;
else if ($radio == "50507")
$userPort = 50507;
else
$userPort = $_POST['portnum'];

echo '<html><head><title>SimSig</title>';
echo '</head><body><center><font size="+1">Checking port ' . $userPort . ' on IP address ' . $userIP . ' - please wait...<br>';
echo '<font size="-3">';

$fp = fsockopen($userIP, $userPort, $errno, $errstr, 5);
echo '</font>';
if (!$fp)
{
echo '<font color="#FF0000">Cannot connect to your computer. Check your network and/or router settings and try again.</font>';
}
else
{
echo '<font color="#008000">Connect was successful - enjoy hosting SimSig!</font>';
fclose($fp);
};
?>


Result (IP masked):

Checking port 50505 on IP address 89.100.xxx.xxx - please wait...

Warning: fsockopen() [function.fsockopen]: unable to connect to 89.100.xxx.xxx:50505 (Connection timed out) in /home/SimSig/public_html/portcheck.php on line 16
Cannot connect to your computer. Check your network and/or router settings and try again.


If I try www.SimSig.co.uk and port 80 then it succeeds, as you'd expect.

SimSig Boss
Log in to reply
PHP expert wanted 17/01/2010 at 20:35 #5950
GeoffM
Avatar
6376 posts
Online
There are a couple of PHP scripts on this website, firstly to obtain your IP address, and secondly to check whether the appropriate port is open. The former works fine; the latter doesn't. It is failing to detect an open port on a machine that I know is open - and double checking with http://www.yougetsignal.com/tools/open-ports/ confirms that the port is open.

If anybody can (a) fix the problem; (b) check the PHP/HTML code; (c) tidy up, then I'd be grateful.

whatsmyip.php:

<?php
$userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
if($userIP == "")
{
$userIP = $_SERVER['REMOTE_ADDR'];
}
echo "<html><head><title>SimSig</title>";
echo '</head><body><center><font size="+1">Your IP address is:<p><font size="+2">' . $userIP . '</font></font><br>';
echo '<p>You can now perform a port check. This will check that this website can connect to the port of your choice on your PC. Start SimSig in Server mode and select the port number below. Finally, press "Check port" to check whether the port can be accessed. If this succeeds then you are likely to be able to host a game. If not, check your network and/or router settings and try again.</p>';
echo '<table><tr>';
echo '<form action="http://www.SimSig.co.uk/portcheck.php" method="post">';
echo '<tr><td><input type="radio" name="port" value="50505"></td><td>Primary port (50505)</td></tr>';
echo '<tr><td><input type="radio" name="port" value="50507"></td><td>Secondary port (50507)</td></tr>';
echo '<tr><td><input type="radio" name="port" value="0"></td><td>Port:<input type="text" name="portnum" size="5"></td></tr>';
echo '<tr><td>Address</td><td><input type="text" name="ip" value="' . $userIP . '"></td></tr>';
echo '<tr><td colspan="3" align="center"><input type="submit" value="Check port"></td></tr>';
echo '</table>';
echo '</form>';
echo '</center></body></html>';
?>


portcheck.php:

<?php
$userIP = $_POST['ip'];
$radio = $_POST['port'];

if ($radio == "50505")
$userPort = 50505;
else if ($radio == "50507")
$userPort = 50507;
else
$userPort = $_POST['portnum'];

echo '<html><head><title>SimSig</title>';
echo '</head><body><center><font size="+1">Checking port ' . $userPort . ' on IP address ' . $userIP . ' - please wait...<br>';
echo '<font size="-3">';

$fp = fsockopen($userIP, $userPort, $errno, $errstr, 5);
echo '</font>';
if (!$fp)
{
echo '<font color="#FF0000">Cannot connect to your computer. Check your network and/or router settings and try again.</font>';
}
else
{
echo '<font color="#008000">Connect was successful - enjoy hosting SimSig!</font>';
fclose($fp);
};
?>


Result (IP masked):

Checking port 50505 on IP address 89.100.xxx.xxx - please wait...

Warning: fsockopen() [function.fsockopen]: unable to connect to 89.100.xxx.xxx:50505 (Connection timed out) in /home/SimSig/public_html/portcheck.php on line 16
Cannot connect to your computer. Check your network and/or router settings and try again.


If I try www.SimSig.co.uk and port 80 then it succeeds, as you'd expect.

SimSig Boss
Log in to reply
PHP expert wanted 17/01/2010 at 22:00 #5952
UKTrainMan
Avatar
1803 posts
Have you closed all tags, particularly those part of portcheck.php? Just looking at your code and unless you didn't for some reason include all bits of code, I cannot see the html tag closed with </html>.

Could be just taking a rough shot here but perhaps it is the problem?

If this isn't the problem then please let me know and I'll take another look.

Thanks.

Any views and / or opinions expressed by myself are from me personally and do not represent those of any company I either work for or am a consultant for.
Log in to reply
PHP expert wanted 17/01/2010 at 22:11 #5953
Sacro
Avatar
1171 posts
Code seems fine, more likely it's your server blocking outgoing connections, looks good here.

I'd add some kind of validation to your $_POST variables, that script is pretty easy to hack.

Log in to reply
PHP expert wanted 19/01/2010 at 14:01 #5973
GeoffM
Avatar
6376 posts
Online
UKTrainMan said:
Just looking at your code and unless you didn't for some reason include all bits of code, I cannot see the html tag closed with </html>
You're right that the PHP isn't generating the closing body/html tags, but the PHP code itself isn't working.

Sacro said:
Code seems fine, more likely it's your server blocking outgoing connections, looks good here.
Aha, Googling for that throws up loads of results, so it does look indeed like the webhost is blocking usage of fsockopen.

Sacro said:
I'd add some kind of validation to your $_POST variables, that script is pretty easy to hack.
Can you expand on that please? What's the worst that can happen? I know I can check that the $_POST data are IP addresses/names, and numeric for the port, but what/how can it be hacked? I'm not PHP-proficient by any means.

Thanks.

SimSig Boss
Log in to reply
PHP expert wanted 20/01/2010 at 05:42 #5994
afro09
Avatar
167 posts
Hi Geoff,

As I have mentioned to you I am currently doing a Web Design and Commputer Programming Degree, and as my finacial situation stands I can not donate to the continued success of the website financial therefore I would like to donate as much of my time as possible.

First bit of advice I can give you is. If you think you are having a problem with any of your code then www.w3schools.com have a validator you can run you code through and will return any discreprencies.

I will run the code above through for you but I need to know what doctype is used in the HTML code for the site i.e. strict, transitional or frameset?

If I cant solve the mystery, one of my lectures is a PHP expert so I will get her to scan her eyes over the code and see if she can suggest a fix to the problem.

Alan.

Log in to reply
PHP expert wanted 21/01/2010 at 15:44 #6037
TylerE
Avatar
149 posts
This is almost certainly NOT doing what you want. To able to open a socket to the remote host the remote host needs to actually be listening. For instance, in the case of the SimSig ports, there would need to be a SimSig multiplayer game actually being hosted. That's why it works when you run it against the web server on port 80.
Log in to reply
PHP expert wanted 21/01/2010 at 17:03 #6038
GeoffM
Avatar
6376 posts
Online
Afro - we know the problem; it's not with the HTML nor the PHP.

Tyler - have a read through the code as your concern is already handled.

SimSig Boss
Log in to reply
PHP expert wanted 21/01/2010 at 22:32 #6057
Sacro
Avatar
1171 posts
Geoff - not sure on whether your code specifically can be exploited but it is not recommended to use any of the $_POST or $_GET variables without checking them first.
Log in to reply