I need help to create simple php | changing url inside file_get_contents everytime the php file being accessed
First load
<?php
$homepage = file_get_contents('http://www.example.com/page=1');
echo $homepage;
?>
everytime the page load the url change (in this case the page number + 1)
Second load
<?php
$homepage = file_get_contents('http://www.example.com/page=2');
echo $homepage;
?>
Third load
<?php
$homepage = file_get_contents('http://www.example.com/page=3');
echo $homepage;
?>
and so forth
Anyone can help me?
You can achieve this by maintaining the counter in a txt
file. Create a a directory (name counter as per my example) and an empty file c.txt before running this process. We can do a check if file does not exist, then create a file. But in your case it seems to be called more frequently, so can avoid extra processing here.
Here's the code snippet,
// File where counter will be maintained. Change the path to the one desired
$filename = "/root/Desktop/counter/c.txt";
$handle = fopen($filename, "r"); // Open the file in read mode
$counter = fread($handle, filesize($filename)); //Read the file
fclose($handle); // Close file object
// Increment the counter if available else set it to 1
$count = !empty($counter) ? (int)$counter+1 : 1;
$homepage = file_get_contents("http://www.example.com/page=$count");
echo $homepage;
$handle = fopen($filename, "w"); // Open the file in write truncate mode
fwrite($handle, $count); // Store the counter in the file
fclose($handle); // Close file object