PHP function kills my code?

So I'm making a page for one of my web sites that scans the visitors ports (just a few specific ones, not ALL) and tells them if they have any open. Now I made up the code and it worked great. Now instead of having the same chunk of code all over my script, I tried to make my own subroutine so I could just call it from all over. But since I did that, it now gives me an error of type 0, which means it couldn't even try to connect to the host, before it got an error. I can't figure it out at all?

Here's my code

<? function sniff( $portnumber ) {
 		if (! $sock = fsockopen($REMOTE_ADDR, $portnumber, $errno, $errstr, 7) )
 			{
  			if ($errno == 60)
  				{
  				echo "Timeout";
  				}
  				else 
  				{
  				echo "Closed $errno";
 				}
			}
			else
			{
  			echo "<font color=#ff3300>Open</font>";
  			fclose($sock);
  			}
  	} ?>

And here is how I call the function...

<? sniff(22); ?>

I'm new to writing php's, but I don't think I have any syntax problems?

OK, I am very much a beginer at PHP. It appears to work fine if you add quotes around the first argument (maybe because the fsockopen's first argument is a string? (anybody?)). Also slightly modifed your syntax. See below. On my PC, the output was: "Closed 9 " when trying port 22, but trying port 80, the output was "Open"

<? function sniff( $portnumber ) {
	$REMOTE_ADDR="localhost";
	$sock = fsockopen("$REMOTE_ADDR", $portnumber, $errno, $errstr, 80);
         if (!$sock)
             {
              if ($errno == 60)
                  {
                  echo "Timeout";
                  }
              else 
                  {
                  echo "Closed $errno";
                 }
            }
            else
            {
              echo "<font color=#ff3300>Open</font>";
              fclose($sock);
            }
      } ?>

<? sniff(22); ?>

Why do you keep posting then deleting your posts? Anyway most of the changes you made are no change. I don't think adding the quotes is going to make a difference because the code worked fine without them when it wasn't in its own function. Nope, just tested, no change.

Sorry, editing the post. Well, all I can say is that the function works on my pc as written. As I said, I am a beginer at PHP too. I will add though that the code you posted does not work for me. I get the same error as you mentioned "Closed 0"

The code works perfect for me when I insert it directly into my page. But when I try to encapsulate it into a function, it stops working and I have no clue why.

Put a return into your code.

such as

return $sock;

or

return 0;

and in your main code you need a var.

$var = sniff(22);

or just use a procedure.

I tried that and it had no effect. I don't think my problem is calling my function, the problem is somehow that the script can't great a new socket from within the function. I do already get a result from the function, its just an error code from the socket. And I thought you didn't NEED a return if you didn't need the result of the function.

What is the value you are passing to REMOTE_ADDR? Have you tried hard coding a value to test the function? Also, what scope does the variable REMOTE_ADDR have within the function? Is PHP like shell where a variable within a function has global scope? or is it a more limited scope?

$remote_addr is a system variable, it is already defined. I never pass anything to it, and it is always equal to the visitors IP address. Also this chunk of code works perfectly fine when its not in my function, only when it is in, does it give me a connection error from the fopensocket.

google is right. REMOTE_ADDR is not
defined in your function.

echo it in your function and see.
echo $REMOTE_ADDR

To fix it, declare it in main code and
pass it into function, such as:

<?php function sniff( $portnumber, $REMOTE_ADDR ) {
        if (! $sock = fsockopen($REMOTE_ADDR, $portnumber, $errno, $errstr, 7) )
            {
              if ($errno == 60)
                  {
                  echo "Timeout";
                  }
                  else 
                  {
                  echo "Closed $errno";
                }
            }
            else
            {
              echo "<font color=#ff3300>Open</font>";
              fclose($sock);
              }
      } 
?>

<?php
$REMOTE_ADDR = $HTTP_SERVER_VARS['REMOTE_ADDR'];
sniff(22, $REMOTE_ADDR);
?>

I got this all figured out on another forum. I thought $REMOTE_ADDR was global because outside of functions you can access it no problem. But to use it in functions, you have to make it a global, which is what i did and now it works fine. Its strange though. Thats why it worked when it wasn't in a function but didn't when it was in one.