Hi this is my first post 😃 but anyway...
So I'm not new to running PHP's, I run about 4 sites on an OS X Server and every pages is a php, at minimum using traffic tracking software, at most, I have pages that use vB forums as news portals and all that good stuff. So I'm certainly not new to PHP, just new to writing my own. So I'm making a script that scans the visitors ports and alerts them if any are open (not all ports, just a standard basic security check type thing). Now so far I'm up to 11 ports but I'm sure I'll be adding more. So originally I had the same 11 blocks of code, one for each port. Once I got it working, I decided to put the code in a function and just call it, sending it the port number, each time. Standard stuff. The problem is, when I use the function to check the ports, I get errors for each. Not a "couldn't connect" or a "timeout" error, and error of type 0, which is described as an error that happens before it even tries to connect to a port. The code hasn't changed, just putting it in a function seems to disable it. I don't understand this but I hope its an easy one. Here is my function code.

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

And here is what I use to call this function:

<? sniff(22); ?>

Seems perfect to me??
And don't worry about the 7 second timeout, I'm testing it only on my LAN, and it loads instantly, and when I had it the other way (no function, that code duplicated 11 times), it also worked perfectly, and instantly, so the 7 seconds ain't it unless there's something I don't know going on.

    The problem I got was with $REMOTE_ADDR. Script works perfectly when you use $_SERVER['REMOTE_ADDR']

      Originally posted by bad76
      Welcome,
      your problem is the variable scope.
      $sock outside of function has not value...

      But how is that a problem, I don't use $sock outside of the fucntion??

      Originally posted by cahva
      The problem I got was with $REMOTE_ADDR. Script works perfectly when you use $_SERVER['REMOTE_ADDR']

      Maybe your server is different than mine? I use $REMOTE_ADDR all over the place on my site and it works fine, see www.whatsmyip.org :-D

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

          That one gives me a parse error over a plain white screen. But I don't think anything like that needs to be done anyway, that variable is a system variable, and its already global, is it not? I use it elsewhere in echo commands no problem.

            i might be wrong but estinelly the function is like a script, it doesnt know ne variables other then the one u specifiy on input. so that $sock is coming out of no where.

              That 'if' statement is creating the $sock variable, and storing the result of the connection attempt in it.

                this if statement is not creating $sock, its comparing the two to see if there equal

                  Originally posted by artic
                  this if statement is not creating $sock, its comparing the two to see if there equal

                  uhh, no dude, then it would be "==" rather than "=" 😉

                    where are $errno and $errstr coming from in your fsockopen() ?

                      <?  
                      function sniff($portnumber){ global $_SERVER; //$errstr and $errno are values used by fsockopen, not passed to it so this isnt a problem (commenting for other peoples replies) if($sock = fsockopen($_SERVER['REMOTE_ADDR'], $portnumber, $errno, $errstr, 7)) { echo "<font color=#ff3300>Open</font>"; fclose($sock); }else{ if ($errno == 60){ echo "Timeout"; }else{ echo "Closed $errno"; } } } ?>

                      Note: I would recommend replacing $REMOTE_ADDR for $_SERVER['REMOTE_ADDR'] so in futre PHP updates you wont have to redo all your scripts... and it would also solve some issues with scoping I would think.... reason being is =>4.0 PHP comes with register globals turned OFF by default for security purposes

                      give it a try like that and see what happens....

                        OK I didn't change my use of the remote addr thing to what you used, I only used the global statement at the beginning. And it worked! I don't understand why this worked. Mainly because I tried it when the last person suggested it and it caused a fatal error. Yet now it works. Plus why does $REMOTE_ADDR work fine outside of a function, but fail so completely inside of one, it does both in one script at the same time.

                          Originally posted by jebster
                          uhh, no dude, then it would be "==" rather than "=" 😉

                          thanks for telling me mate. im still kind of a newb at php lol

                            Originally posted by l008com
                            why does $REMOTE_ADDR work fine outside of a function, but fail so completely inside of one, it does both in one script at the same time.

                            as was said earlier, it's out of scope within the function. in a function, you can only access variables assigned within the function, passed to the function, the superglobals ($SERVER, $COOKIE, $_GET etc.) and (I think) static variables. if you want to access normal variables outside of your function from within it, you need to use global $myvar;.

                            global $REMOTE_ADDR will work, but you should really use $_SERVER['REMOTE_ADDR'] as was said above for forwards compatibility.

                              Originally posted by daveyboy
                              ....it's out of scope within the function. in a function, you can only access variables assigned within the function, passed to the function, the superglobals ($SERVER, $COOKIE, $_GET etc.)....

                              Ooh, I should have seen that - I was fixing an old script just the other day where that was an issue.

                              Turning up error reporting so that Notices are given (generally a good idea during development) would have pinpointed this problem early, as it would have generated a "Notice: Undefined variable: REMOTE_ADDR in ...".

                              (But no-one else spotted the other two errors: using <font color=#ff3300> 🙂)

                                Write a Reply...