用PHP进行WordPress版本检查

时间:2017-02-24 作者:Alex Grecu

我制作了一个小脚本,用于检查网站的上一个wordpress版本,对于一些网站来说,如果网站显示关于版本的信息就可以了,但是如果有更多的name=“generator”,它会得到最后一行。

有什么想法吗伙计们?谢谢

<?php 

    function file_get_contents_curl($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    $link = mysqli_connect("localhost", "", "", "");    

    if($link === false) { die(\'No connection!\' . mysqli_connect_error()); }

    $sql = "SELECT * FROM My_Websites ORDER BY date";

    if($result = mysqli_query($link, $sql)) {

        if(mysqli_num_rows($result) > 0) {

            echo \'<table class="table table-striped table-hover">\';        
            echo \'<thead>\'; 
            echo "<tr>";
            echo "<th>ID</th>";
            echo "<th>Adaugat</th>";
            echo "<th>Domeniu</th>";
            echo "<th>Versiune WP.</th>";
            echo "<th>Admin</th>";
            echo "</tr>";
            echo "</thead>";

            while($row = mysqli_fetch_array($result)){   

                $domeniu = $row[\'website\'];
                $html = file_get_contents_curl($domeniu);

                //parsing begins here:
                $doc = new DOMDocument();
                @$doc->loadHTML($html);
                $nodes = $doc->getElementsByTagName(\'title\');

                //get and display what you need:
                $title = $nodes->item(0)->nodeValue;

                $metas = $doc->getElementsByTagName(\'meta\');

                for ($i = 0; $i < $metas->length; $i++)
                {
                    $meta = $metas->item($i);
                    if($meta->getAttribute(\'name\') == \'description\')
                        $description = $meta->getAttribute(\'content\');
                    if($meta->getAttribute(\'name\') == \'generator\')
                        $generator = $meta->getAttribute(\'content\');
                }

                echo "<tr>";
                echo "<td> " . $row[\'id\'] . " </td>";
                echo "<td> " . $row[\'date\'] . " </td>";
                echo \'<td> <a href="http://\' . $domeniu . \'" target="_blank">\' . $domeniu . \'</a></td>\';
                echo "<td> " . $generator . " </td>";
                echo \'<td> <a href="http://\' . $domeniu . \'/wp-admin" type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-link" style="color:blue;"></span> acceseaza </a></td>\';
                echo "</tr>";

            }     

            echo "</table>";      
            mysqli_free_result($result);    

        } else { print (\'Error!\'); }    

    } else { print \'Error!\' . mysqli_error($link); print \'.\'; }

    mysqli_close($link);

?>

1 个回复
SO网友:majick

在设置值之前,请尝试检查属性内容:

$generator = \'\'; // clear previous value
for ($i = 0; $i < $metas->length; $i++)
    {
        $meta = $metas->item($i);
        if($meta->getAttribute(\'name\') == \'description\')
            $description = $meta->getAttribute(\'content\');

        if($meta->getAttribute(\'name\') == \'generator\') {
            $thisgenerator = $meta->getAttribute(\'content\');
            if ( ($generator == \'\') && (stristr($thisgenerator,\'wordpress\')) ) {
                $generator = $thisgenerator;
            }
        }
    }

相关推荐