Pagina de start a forumului Forum PHP Romania - Discutii despre PHP, MySQL, Javascript, AJAX, etc Forum PHP Romania - Discutii despre PHP, MySQL, Javascript, AJAX, etc
Comunitatea PHP Romania
 

verificare link
Vezi mesajul original

 
       Pagina de start a forumului Forum PHP Romania - Discutii despre PHP, MySQL, Javascript, AJAX, etc -> Cod PHP
Subiectul anterior :: Subiectul următor  
Autor Mesaj
sparco



Data înscrierii: 03/Mai/2003
Mesaje: 3

Trimis: Sâm Mai 03, 2003 8:38 am    Titlul subiectului: verificare link  

8O Am nevoie de un script php care sa verifice un link, daca e valid sa plece acolo, iar daca nu sa plece la o alta pagina .... va rog! 8O
Sus  
cristi



Data înscrierii: 26/Mai/2002
Mesaje: 270
Locație: Timisoara

Trimis: Dum Mai 04, 2003 7:03 pm    Titlul subiectului:  

Ai mai jos exemplul complet de la zend.com:

Cod: // 000712, steven@haryan.to
// 010207, carinridge@hotmail.com (Added file validity check)
// 010725, carinridge@hotmail.com (Updated regex, & errors)  Thanx to Luis Gonzaga for the heads up...

/*

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

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

                if ($fp) {
                    $sstatus = "OK";
                    fputs( $fp, sprintf( "GET %s HTTP/1.0\n\n", $url ) );
                    for( $try = 1; $try <= 3; $try++ )
                    {
                        $fstatus = "CHECKING";
                        if( ($got = fgets( $fp, 256 )) == 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"; }
    /* If the server is up, tell how the file is doing... */
    if ($sstatus == "OK") { echo " F:$fstatus"; }
    else { echo ")"; }

    error_reporting($e);
}

?> 
Example 
<? 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>   
Sus  
cristi



Data înscrierii: 26/Mai/2002
Mesaje: 270
Locație: Timisoara

Trimis: Dum Mai 04, 2003 7:05 pm    Titlul subiectului:  

Un exemplu mai simplu ar fi:

Cod: $test = fsockopen("www.phpromania.as.ro", 80, &$nr_eroare, &$str_eroare, 30);
if(!$test)
{
   echo "phpromania offline";
}
else
{
   echo "phpromania online";
}
Sus  
sparco



Data înscrierii: 03/Mai/2003
Mesaje: 3

Trimis: Lun Mai 05, 2003 4:22 pm    Titlul subiectului: ok....  

since...io nu prea le am cu php...sun newbie...al doile exemplu e mai simplu...soar ca nu vreau sa-mi arate status..adik daca e online sau offline...daca e online...sa mearga spre adresa...iar daca nu sa scrie ca e offline...poate ce iti cer tie ti se pare banal..dar asa e cu incepatorii :D , mersi mult...
Sus  
cristi



Data înscrierii: 26/Mai/2002
Mesaje: 270
Locație: Timisoara

Trimis: Lun Mai 05, 2003 4:35 pm    Titlul subiectului:  

In loc de:
Cod: echo "phpromania online";
scrii
Cod: header('Location:www.locatiata.ro');
Sus  
Constantin



Data înscrierii: 20/Sep/2002
Mesaje: 236

Trimis: Lun Mai 05, 2003 10:45 pm    Titlul subiectului:  

Ca sa eviti problemele cu functia header(), citeste si acest topic
http://www.phpromania.as.ro/forum2/viewtopic.php?p=613
Sus  
sparco



Data înscrierii: 03/Mai/2003
Mesaje: 3

Trimis: Mar Mai 06, 2003 6:28 pm    Titlul subiectului: Va multumesc  

Sunteti cei mai tari... in multe forumuri de obicei se raspunde f greu...dar voi sunteti f de treaba...va multumesc tuturor...tineti-o tot asa, apreciez ceea ce faceti...BAFTA, multumesc pentru tot ajutorul
Sus  
PHPRomania Bot
Bot Member


Data înscrierii: 27/Dec/2007
Mesaje: 1
Locaţie: Server Google
Trimis: Mie Dec 26, 2007 7:01 pm   Titlul subiectului: Ad  

Sus  
 
       Pagina de start a forumului Forum PHP Romania - Discutii despre PHP, MySQL, Javascript, AJAX, etc -> Cod PHP
Pagina 1 din 1


Powered by phpBB 2.0.22 © 2001, 2002 phpBB Group
Varianta în limba română: Romanian phpBB online community