The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
My menu function [new code -revised] amatuer_lee_3 Shell Programming and Scripting 3 05-18-2008 08:05 AM
kills command shwe UNIX for Dummies Questions & Answers 1 05-13-2008 12:03 PM
how to write a wrapper c code to return uid using getuid() function pwd High Level Programming 4 05-13-2008 11:01 AM
Linux kills windows :( woofie SuSE 6 05-16-2005 09:19 PM
Crash Kills Printer Device ckurt UNIX for Dummies Questions & Answers 2 02-08-2002 03:10 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-11-2003
l008com l008com is offline
Registered User
  
 

Join Date: Jun 2002
Location: Stoneham MA
Posts: 45
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
PHP Code:
<? function sniff$portnumber ) {
         if (! 
$sock fsockopen($REMOTE_ADDR$portnumber$errno$errstr7) )
             {
              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 Code:
<? sniff(22); ?>
I'm new to writing php's, but I don't think I have any syntax problems?
  #2 (permalink)  
Old 12-11-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
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"


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


Last edited by google; 12-11-2003 at 09:30 PM..
  #3 (permalink)  
Old 12-11-2003
l008com l008com is offline
Registered User
  
 

Join Date: Jun 2002
Location: Stoneham MA
Posts: 45
Quote:
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"


Code:
<? 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.
  #4 (permalink)  
Old 12-11-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
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"

Last edited by google; 12-11-2003 at 11:14 PM..
  #5 (permalink)  
Old 12-11-2003
l008com l008com is offline
Registered User
  
 

Join Date: Jun 2002
Location: Stoneham MA
Posts: 45
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.
  #6 (permalink)  
Old 12-12-2003
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
Put a return into your code.

such as

Code:
return $sock;

or

Code:
return 0;

and in your main code you need a var.


Code:
$var = sniff(22);

or just use a procedure.
  #7 (permalink)  
Old 12-12-2003
l008com l008com is offline
Registered User
  
 

Join Date: Jun 2002
Location: Stoneham MA
Posts: 45
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0