HiddenChampion
HiddenChampion

Reputation: 79

Check with PHP if file contains this text

The php function should check if the file index.php contains the CMS link. I tried following but it's not working:

    <?php
    $file = file_get_contents("./index.php");
    if (strpos($file, "http://www.wordpress.com") !== false) {
        echo "Found";
    }
    else
    {
    echo "Not found";
    }
    ?>

I am pretty new to PHP. I have not found the answer using search.

Upvotes: 0

Views: 7838

Answers (2)

Baba
Baba

Reputation: 95121

file_get_contents — Reads entire file into a string running the following :

 $file = file_get_contents("./index.php");

Would would result in RAW PHP CODE Instead of rendered HTML version and http://www.wordpress.com might even be from database or any other resource

Use use the full HTTP path instead

 $file = file_get_contents("http://www.xxxxx.com/index.php");

Example

If you have a.php file

<?php 
    echo "XXX" ; 
?>

If you run

 var_dump(file_get_contents("a.php"));

Output

string '    <?php 

        echo "XXX" ; 

    ?>

' (length=31)

and

   var_dump(file_get_contents("http://localhost/a.php"));

Output

  string '  XXX' (length=4)

Upvotes: 1

Derek
Derek

Reputation: 4751

<?php
$file = file_get_contents("./index.php");
if (preg_match("/http\:\/\/www\.wordpress\.com/", $file)) {
    echo "Found";
}
else {
    echo "Not found";
}
?>

Upvotes: 1

Related Questions