Test If Port Open and Forwarded Using PHP

Test if port open and forwarded using PHP

I'm not sure what you mean by being "forwarded properly", but hopefully this example will do the trick:

$host = 'stackoverflow.com';
$ports = array(21, 25, 80, 81, 110, 443, 3306);

foreach ($ports as $port)
{
$connection = @fsockopen($host, $port);

if (is_resource($connection))
{
echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";

fclose($connection);
}

else
{
echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n";
}
}

Output:

stackoverflow.com:21 is not responding.
stackoverflow.com:25 is not responding.
stackoverflow.com:80 (http) is open.
stackoverflow.com:81 is not responding.
stackoverflow.com:110 is not responding.
stackoverflow.com:443 is not responding.
stackoverflow.com:3306 is not responding.

See http://www.iana.org/assignments/port-numbers for a complete list of port numbers.

Check if anything is listening in a port in PHP

You should actually check to see if fsockopen() has returned a resource:

$connection = @fsockopen('localhost', '81');

if (is_resource($connection))
{
echo 'Open!';
fclose($connection);
return true;
}
else
{
echo 'Closed / not responding. :(';
return false;
}

Check if client port is open and forwarded [C#, ASP.NET]

No, there isn't.

To check if a connection can be established, you need to try to establish a connection :) Using a Socket is a sensible way of doing just that.

You could possibly skip this step for some clients, by checking if the machine has a public IP address or a local one. Still, there is the possibility of company firewalls and things like that, so trying to open a connection is the safest way to go.

Using an ICMP echo request (ping) is not an option, because ICMP is a different protocol from TCP (or UDP) that has no concept of a port number. Pinging can be used to determine if an IP address is reachable, but this is of no added value to your case.

See if a server is listening on a port in PHP?

Attempt a connection on the port and return the result:

 <?php
function Connect($port) {
$serverConn = @stream_socket_client("tcp://127.0.0.1:{$port}", $errno, $errstr);
if ($errstr != '') {
return false;
}
fclose($serverConn);
return true;
}

if(isset($_POST['portTest'])){
switch ($_POST['portTest']){
case 'minecraft': $port= '25565';
break;
case 'Terraria': $port= '7777';
break;
default: exit;
}
if (Connect($port)){
echo "Server is running!";
}else{
echo "Server is down";
}
}
?>

<form method="POST">
<input type="submit" name="portTest" value="minecraft">
<input type="submit" name="portTest" value="Terraria">
</form>

Port forwarding ok but not able to access data

You might either face a configuration problem with all your forwards, or - the most common reason for this behaviour - your router does not support Loopback-Connections.

Meaning: Even IF your external ip would be 1.1.1.1 - calling 1.1.1.1 from within your local network does not work, because your router is unable to determine that 1.1.1.1 refers to itself on the WAN-side. (or it forwards all connections on the external ip to its internal ip, therefore failing for machines behind the router which rely on port-forwarding to be executed, which does not happen for requrests arriving on the internal ip adress.)

don't be afraid, only very little routers are supporting this. Even if you use an external dynamic dns, you usually can't connect from withing your local network. To verify whether this is the case or not - you need to move physicaly outside to check connectivity (or use a thethered connection with internet-Sharing on your mobile and connect through your smartphone, tablet or notebook using the internet sharing feature)

If canyouseeme.org can see you - but you can't from your local network I'll bet, you are running in exactly this issue.

Edit: I'm having the exact same Problem. (well not a problem, if you know about):

CanYouSeeMee

Sample Image

Local Network:

Sample Image

Smartphone (Wifi Disconnected)

Sample Image

Sidenode: To have everything like owncloud working internally as well, I just configured my DNS-Server to serve a fake Forward-Lookup, so that http://externaldomain.com is resolved as an local ip from inside my network.

So, from outside: http://externaldomain.com resoles to whatever my external ip is (91.48.*.*) but from the inside, it resolves to the internal ip of the very same server (192.168.5.5).

So, I could use all DNS-Names without limitation, only can't use my external ip.

Javascript/PHP - find if the client is in the same network

Any request to your server that has been port forwarded will appear to originate from the IP address of the device that performed the port forwarding.

So you just need to test:

if ($_SERVER['REMOTE_ADDR'] == $the_ip_address_of_the_router_on_the_lan)

where $the_ip_address_of_the_router_on_the_lan is a known value such as 192.168.0.1.

Devices on the LAN, assuming they access your server directly, will make requests that appear to originate from their own LAN IP address.

Warning: If the server is accessed using a hostname (such as foo.example.com), and you use the same hostname both inside and outside the LAN, and the DNS inside the LAN points that hostname to the Internet facing ip address of the router, then the requests will still be port forwarded by the router.

How can I check if ports 465 and 587 are open with PHP?

You can check for open/available ports with fsockopen:

$fp = fsockopen('127.0.0.1', 25, $errno, $errstr, 5);
if (!$fp) {
// port is closed or blocked
} else {
// port is open and available
fclose($fp);
}

...where 5 is the timeout in seconds until the call fails.

This is probably due to a firewall issue where your hosting provider is blocking you from connecting to outbound sockets and/or specific ports. Keep in mind that it is a very usual security configuration to block outbound SMTP ports. Back in the day, only port 25 was blocked, but I'm starting to see more and more SSL variants being blocked as well.

Most providers and hosting companies will only allow you to connect to their own SMTP server to prevent spammers from relaying junk mail.



Related Topics



Leave a reply



Submit