SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fstockopen() variable not being declared?

    I know there are other posts about fsockopen() but none of them can really help me...

    I found some potentially useful code on zend, a Link Checker. Unfortunately it seems that a variable, $sstatus isn't being declared, and I can't for the life of me, figure out why, or how to correct it.

    If someone can help me and figure out what is wrong with it I'd appreciate it! I receive no errors, so I can't even find documentation for a problem. I know, I hate to do the "please do this for me" posts as much as you hate reading them, but I am as stuck as I can be!

    Thanks ahead of time! See it in action

    PHP Code:
    <? 
    //    Code from:
    //    http://www.zend.com/codex.php?id=453&single=1

    /* 

    an example of usage: 

    <? require "/path/to/this/file"; ?> 
    My validated bookmarks: 
    <ul> 
    <li><? disp_link("http://slashdot.org", "Slashdot"); ?> 
    <li><? disp_link("http://www.zope.org", "Zope"); ?> 
    <li><? disp_link("http://www.python.org", "Python"); ?> 
    <li><? disp_link("http://www.perl.com", "Perl"); ?> 
    <li><? disp_link("http://www.php.net", "Zend"); ?> 
    <li><? disp_link("http://www.perl.com/CPAN-local/", ""); ?> 
    </ul> 

      btw, yup, of course this code is under GPL. 

    */ 

    $debug 0
    $timeout 30// give up if can't connect within 30 secs. 
    $check_freq 5*60// cache for 5 minutes. 

    session_register('statuses'); 
    session_register('hostnames'); 

    function 
    flush_disp_link_cache() { 
        global 
    $statuses
        global 
    $hostnames
        
    $statuses = array(); 
        
    $hostnames = array(); 


    function 
    disp_link($url$text) { 
        global 
    $debug
        global 
    $timeout
        global 
    $statuses
        global 
    $hostnames
        global 
    $check_freq

        
    $now time(); 
        
    $e error_reporting(); error_reporting($e & (255-E_WARNING)); 

        if (!
    preg_match('/^(http|https|ftp)://((?:[a-zA-Z0-9_-]+.?)+):?(d*)/'$url$m)) { 

            
    // it's a relative link, or absolute link with unsupported protocol 
            // so there's no need to check 
            
    $sstatus ''
            if (
    $debug) { $sstatus='DEBUG:UNCHECKED'; } 

        } else { 

            
    $proto=$m[1]; 
            
    $hostname=strtolower($m[2]); 
            
    $port=$m[3]; 

            
    // is the host an IP address? 
            
    if (preg_match('/^d+.d+.d+.d+/'$hostname)) { 

                
    $ip $hostname

            } else { 
    // it's not, so we have to resolve it first 

                // have we tried to resolve it not so long ago? 
                
    $from_cache=0
                if (isset(
    $hostnames[$hostname])) { 

                    if (
    $debug) { 
                        echo 
    "DEBUG: last resolve = "$hostnames[$hostname][1], "<br>"
                    } 

                    if (
    $now $hostnames[$hostname][1] <= $check_freq) { 
                        
    $ip $hostnames[$hostname]; 
                        
    $from_cache=1
                    } 

                } 

                if (!
    $from_cache) { 
                    
    // we haven't, so resolve it 
                    
    $ip gethostbyname($hostname); 

                    
    // if the hostname was not resolvable, gethostbyname returns   
                    // its argument unchanged 
                    
    if ($ip === $hostname) { $ip=''; } 

                    
    // cache this resolve 
                    
    $hostnames[$hostname]=array($ip,$now); 
                } 
                  
            } 

            if (!
    $ip) { // was the hostname unresolvable? 

                
    $sstatus 'HOST NOT FOUND'

            } else { 

                
    // get universal port number defaults, if not specified 
                
    if (!$port) {   
                    if (
    $proto == 'http') { $port 80; } 
                    elseif (
    $proto == 'https') { $port 443; } 
                    elseif (
    $proto == 'ftp') { $port 21; } 
                } 

                
    $key "$ip:$port"

                
    // have we checked the site not so long ago? 
                
    $from_cache=0
                if (isset(
    $statuses[$key])) { 

                    if (
    $debug) { 
                        echo 
    "DEBUG: last check = ",$statuses[$key][2], "<br>"
                    } 

                    if (
    $now-$statuses[$key][2] <= $check_freq) { 
                        
    $sstatus $statuses[$key][0]; 
                        
    $from_cache=1
                    } 

                } 

                if (!
    $from_cache || ($from_cache && ($sstatus == "OK"))) { 
                    
    // we haven't, so check it 
                    // CR:  or we have and the host is ok, so check the file 

                    
    if ($debug) { 
                        echo 
    "DEBUG: checking: proto=$proto, hostname=$hostname, "
                             
    "ip=$ip, port=$port...<br>"
                    } 
                    
    $fp fsockopen($hostname$port, &$errno, &$errstr$timeout); 
    //                $fp = fsockopen($hostname, $port, $errno, $errstr, $timeout); 


                    
    if ($debug) { 
                        echo 
    "DEBUG: connect result: fp=$fp, errno=$errno, errstr=$errstr<br>";   
                    } 

                    if (
    $fp) { 
                        
    $sstatus "OK"
                        
    fputs$fpsprintf"GET %s HTTP/1.0nn"$url ) ); 
                        for( 
    $try 1$try <= 3$try++ ) 
                        { 
                            
    $fstatus "CHECKING"
                            if( (
    $got fgets$fp256 )) == NULL 
                              break; 
                            if( 
    eregi"HTTP/1.(.) (.*) (.*)"$got$parts ) ) 
                            { 
                                echo 
    "<!-- Found on try $try -->"
                                if( 
    $parts[2] == "200" 
                                    
    $fstatus "FOUND"
                                else if( 
    $parts[2] == "300" 
                                    
    $fstatus "MOVED"
                                else if( 
    $parts[2] == "403" 
                                    
    $fstatus "RESTRICTED"
                                else if( 
    $parts[2] == "404" 
                                    
    $fstatus "NOT FOUND"
                                else 
                                    
    $fstatus "ERR ".$parts[2]." - ".$parts[3]; 
                                break; 
                            } 
                            
    $fstatus "Bad Comms"
                        } 
                    } else { 
                        if (
    preg_match('/timed?[- ]?out/i'$errstr)) { 
                            
    $sstatus "TIMEOUT"
                        } elseif (
    preg_match('/refused/i'$errstr)) { 
                            
    $sstatus "OFF"
                        } else { 
                            
    $sstatus "DOWN?"
                        } 
                    } 

                    
    // cache this check 
                    
    $statuses[$key] = array($sstatus$fstatus$now); 
                } 

            } 

        } 

        echo 
    "<A HREF=" $url ">" . ($text $text htmlentities($url)) . "</A>"

        if (
    $sstatus) { echo " (S:$sstatus"; } 
        else { echo 
    "(ERROR! SOMETHING IS WRONG!"; }
        
    /* If the server is up, tell how the file is doing... */ 
        
    if ($sstatus == "OK") { echo " F:$fstatus"; } 
        else { echo 
    ")"; } 

        
    error_reporting($e); 


    ?>

    Example

    My validated bookmarks: 
    <ul> 
    <li><? disp_link("http://google.com/asdf/""Google"); ?> 
    <li><? disp_link("http://www.zope.org""Zope"); ?> 
    <li><? disp_link("http://www.python.org""Python"); ?> 
    <li><? disp_link("http://www.perl.com""Perl"); ?> 
    <li><? disp_link("http://www.php.net""Zend"); ?> 
    <li><? disp_link("http://www.perl.com/CPAN-local/"""); ?> 
    </ul>

  2. #2
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the beginning of the script add:
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', TRUE);

    then remove/comment out all error_reporting() calls...

    and you'll see there is something wrong with the regular expression...

    changed line 29 to if (!preg_match('#^(http|https|ftp)://((?:[a-zA-Z0-9_-]+.?)+):?(d*)#', $url, $m))

    and now it times out.... meaby changes the execution time and make sure that it uses $_SESSION.... etc etc... happy updating ;p

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help!
    I feel kinda dumb that I didn't do ini_set error_reporter on my own, but what can you do.

    Anyway, I'll do some debugging. Thanks again.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing I've seen mentioned previously is to use streams instead of sockets if your php version is < 4.3. http://au.php.net/manual/en/ref.stream.php

    From the comments on the Hive project from the PHP5 contest...
    Quote Originally Posted by Ilia Alshanetsky
    Well, I don't recall our rules about obscure extensions, but the sockets extension is very buggy and almost no one has it enabled. Had these guys used streams, which pretty much do all the same things in PHP 5, I would've given them extra points. But using sockets IMO is bad, when the same can be done with newer, better maintained and unique to PHP5 functionality.

    A bit hard to tell, but looks like it maybe prone to E_NOTICE message in validation of some of the user input. There are not isset & empty() calls in most of the input processing code. (Possible security issues(?)).

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's cool. Now, could you please explain what fsockopen has to do with sockets extension?

  6. #6
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you'd get a better answer here.

  7. #7
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    That's cool. Now, could you please explain what fsockopen has to do with sockets extension?
    fsockopen is used to open a network socket connection. It may have changed as I haven't used sockets since 4.2 or so, but you used to have to explicitly --enable-sockets when compiling, and without the extension, you can't use the network functionality (such as fsockopen). 4.3 and above has streams which covers all of what you can do with the socket extension (which was pretty much an direct wrap of functions you could use in C), but adds some extra PHP usability and reliability.

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're still confusing fsockopen and Sockets extension.

    fsockopen uses connect() and friends, socket_xxx provide an interface to it. Feel the difference.

  9. #9
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to a more appropriate forum. See Where should I post my thread? for details

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •