0

I am currently working an auto-content-generator script's sitemap. I got to know that google accept sitemap in simple text file that contains one URL per line.

so I created a file named 1.txt and wrote a script to add current page URL to 1.txt when a user visits.

test.php is:

$file = 'assets/sitemap/1.txt';
$url = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]."\n";
$file = fopen($file, 'a');
fwrite($file, $url);
fclose( $file );

This script writes the page URLto 1.txt every time someone hits the page. But the problem is, it creates too much duplicate links. So I want to add a filter to not add a string (URL in this case) if it already exists.

After surfing a while, I got a solution here (second snippet) that is resource friendly: PHP check if file contains a string

I made the following modification but it is not working (not adding anything at all):

$file = 'assets/sitemap/1.txt';
$url = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]."\n";
if(exec('grep '.escapeshellarg($url).' assets/sitemap/1.txt')) {}
else{
    $file = fopen($file, 'a');
    fwrite($file, $url);
    fclose( $file );
}
Community
  • 1
  • 1
Kubloy
  • 5
  • 3

1 Answers1

1

This is hopefully easier to understand:

$file = 'assets/sitemap/1.txt';
$url  = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]."\n";

$text = file_get_contents($file);

if(strpos($text, $url) === false) {
    file_put_contents($file, $url, FILE_APPEND);
}
  • Read the file contents into a string $text using file_get_contents()
  • Check if $url is in the string $text using strpos()
  • If $url is not in the string $text, append the $url to the file using file_put_contents()

To count the total lines, you can start using file() to load the file lines into an array. Then check if the $url is in the array using in_array():

$lines = file($file);
$count = count($lines); // count the lines

if(!in_array($url, $text)) {
    file_put_contents($file, $url, FILE_APPEND);
    $count++; // if added, add 1 to count
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87