# PHP function kills my code?

**URL:** <https://community.unix.com/t/php-function-kills-my-code/147569>\
**Category:** Shell Programming and Scripting\
**Created:** [December 11, 2003, 7:11pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569 "2003-12-11T19:11:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![l008com](https://community.unix.com/letter_avatar/l008com/32/6_449c533f586939b6e5f246632a46421f.png) [@l008com](https://community.unix.com/u/l008com)\
**Post date:** [December 11, 2003, 7:11pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/1 "2003-12-11T19:11:36Z")

</div>

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

```php
<? 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...

```php
<? sniff(22); ?>

```

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

---

<div class="post-metadata">

**Author:** ![google](https://community.unix.com/user_avatar/community.unix.com/google/32/65_2.png) [@google](https://community.unix.com/u/google)\
**Post date:** [December 11, 2003, 8:17pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/2 "2003-12-11T20:17:15Z")

</div>

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"

```nohighlight
<? 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); ?>

```

---

<div class="post-metadata">

**Author:** ![l008com](https://community.unix.com/letter_avatar/l008com/32/6_449c533f586939b6e5f246632a46421f.png) [@l008com](https://community.unix.com/u/l008com)\
**Post date:** [December 11, 2003, 8:29pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/3 "2003-12-11T20:29:24Z")

</div>

> [@](#):
>
> Originally posted by google  
> OK, I am very much a beginer at PHP. It appears to work fine if you add quotes around the first argument: On my PC, the output was: "Closed 9 " when trying port 22, but trying port 80, the output was "Open"
> 
> ```plaintext
> <? 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);
> $sock</p>";
> }
> } ?>
> 
> <? 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.

---

<div class="post-metadata">

**Author:** ![google](https://community.unix.com/user_avatar/community.unix.com/google/32/65_2.png) [@google](https://community.unix.com/u/google)\
**Post date:** [December 11, 2003, 8:31pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/4 "2003-12-11T20:31:40Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![l008com](https://community.unix.com/letter_avatar/l008com/32/6_449c533f586939b6e5f246632a46421f.png) [@l008com](https://community.unix.com/u/l008com)\
**Post date:** [December 11, 2003, 8:33pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/5 "2003-12-11T20:33:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![photon](https://community.unix.com/user_avatar/community.unix.com/photon/32/66_2.png) [@photon](https://community.unix.com/u/photon)\
**Post date:** [December 12, 2003, 4:59pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/6 "2003-12-12T16:59:14Z")

</div>

Put a return into your code.

such as

```nohighlight
return $sock;

```

or

```nohighlight
return 0;

```

and in your main code you need a var.

```nohighlight
$var = sniff(22);

```

or just use a procedure.

---

<div class="post-metadata">

**Author:** ![l008com](https://community.unix.com/letter_avatar/l008com/32/6_449c533f586939b6e5f246632a46421f.png) [@l008com](https://community.unix.com/u/l008com)\
**Post date:** [December 12, 2003, 8:24pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/7 "2003-12-12T20:24:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![google](https://community.unix.com/user_avatar/community.unix.com/google/32/65_2.png) [@google](https://community.unix.com/u/google)\
**Post date:** [December 13, 2003, 12:36pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/8 "2003-12-13T12:36:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![l008com](https://community.unix.com/letter_avatar/l008com/32/6_449c533f586939b6e5f246632a46421f.png) [@l008com](https://community.unix.com/u/l008com)\
**Post date:** [December 13, 2003, 3:00pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/9 "2003-12-13T15:00:48Z")

</div>

$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.

---

<div class="post-metadata">

**Author:** ![photon](https://community.unix.com/user_avatar/community.unix.com/photon/32/66_2.png) [@photon](https://community.unix.com/u/photon)\
**Post date:** [December 15, 2003, 10:57pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/10 "2003-12-15T22:57:07Z")

</div>

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
<?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);
?>

```

---

<div class="post-metadata">

**Author:** ![l008com](https://community.unix.com/letter_avatar/l008com/32/6_449c533f586939b6e5f246632a46421f.png) [@l008com](https://community.unix.com/u/l008com)\
**Post date:** [December 15, 2003, 11:02pm UTC](https://community.unix.com/t/php-function-kills-my-code/147569/11 "2003-12-15T23:02:22Z")

</div>

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.
