1

I've made a little script that checks for last wordpress version of a site, for some its ok if the website shows the info about version, but there is a problem where if there are more name="generator" it gets the last line.

Any ideas guys? Thanks.

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

?>
4
  • You mean the version of another site right?
    – Laxmana
    Commented Feb 24, 2017 at 13:03
  • Yes, i have like over 30 and i dont want to check all of them manually.
    – Alex Grecu
    Commented Feb 24, 2017 at 13:04
  • you could check the readme.html instead
    – majick
    Commented Feb 24, 2017 at 14:00
  • @majick i usually delete that file after install.
    – Alex Grecu
    Commented Feb 24, 2017 at 14:07

1 Answer 1

1

Try checking the attribute contents before setting the value:

$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;
            }
        }
    }
1
  • Thanks man! (i didnt used your fix). There was a problem with loading speed so i had to figure it out. Finally i've created a file in each wp site and i've generated just the meta tag and it worked very well with my code.
    – Alex Grecu
    Commented Feb 28, 2017 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.