I want to check if a line exists in a file so that I avoid the same line repetition. I have put a dev restriction to the site to only allow myself to view it, others are redirected to "unavailable.php" page
But I want to allow people to view site if they request a permission
In my case, I have a .htaccess
file
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REMOTE_HOST} !^34\.120\.121\.20 #this a random ip
RewriteCond %{REQUEST_URI} !path/to/first/exception\.php$ #first exception is 'unavailable.php'
RewriteCond %{REQUEST_URI} !path/to/another/exception\.php$ #the second exception is 'request_permission.php'
RewriteRule \.php$ /redirect/to/first/exception/ [L]
and in request_permission.php
, I have the following code:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$data = file('.htaccess');
$parts = explode(' ', $data[2]);
$parts_end = end($parts);
$parts_substred = substr($parts_end, 2);
$ip_addr = str_replace('\\', '', $parts_substred);
if ($ip_addr != $ip){
$new_ip = str_replace('.', '\\.', $_SERVER['REMOTE_ADDR']);
$new_string = str_replace($parts_end, "", $data[2]) . "!^".$new_ip;
$string = $data[0].$data[1].$data[2].$new_string.PHP_EOL.$data[3].$data[4].$data[5];
//file_put_contents(".htaccess", $string);
}
?>
Now everytime I visit request_permission.php
a new line, like this is being created: RewriteCond %{REMOTE_HOST} !^56\.80\.1\.15
(let's say this is my ip).
I want to check if the line with my IP address exists in htaccess, do that I won't duplicate it again.
I tried to use
strpos()
but it doesn't find my IP address even if that exists.
How should I do this?