Avatar of ellandrd
ellandrd
Flag for Ireland asked on

Update HTACCESS file using PHP

I want to update my .htaccess file using PHP.

I have a section with comments and rules which I would like to read, update and then write back to the htaccess

So far I get read the contents, but all the contents get read.

How can i start reading the file contents at my comment, update the comment and write back to the htaccess?

My code so far:

$htaccess = $_SERVER['DOCUMENT_ROOT'].'/.htaccess';
             
       $content = file_get_contents($htaccess);
              
                $start = '# Restrict START #';
                  
                  $end = '# Restrict END #';
      
                  preg_match('/$start(.*)$end/s',$content,$matches);
                  
                  echo '<pre>';
                  print_r($matches);
                  echo '</pre>';
          

My .htaccess file contents:


# Restrict START #
order deny,allow
deny from all
allow from 127.0.0.1 # Localhost
# Restrict END #


My matches array is empty though
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

You can use explode(PHP_EOL, file_get_contents($htaccess)); to create an array of the lines in the file, then iterate over the array.
ellandrd

ASKER
Will this still be a good solution if the htaccess has other rules.  currently it has 346 lines of rules.
ellandrd

ASKER
I've tried using explode(PHP_EOL, file_get_contents($htaccess)); but my array stops after 143 lines.

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ray Paseur

http://www.laprbass.com/RAY_temp_ellandrd.php

Now tell us what sort of thing you want to change in the .htaccess file and I'll show you how to incorporate the changes and write it back.

Best, ~Ray
<?php // RAY_temp_ellandrd.php
error_reporting(E_ALL);
echo "<pre>";

// THIS STRING SIMULATES READING THE HTACCESS FILE
$htaccess = <<<HTACCESS
# Restrict START #
order deny,allow
deny from all
allow from 127.0.0.1 # Localhost
# Restrict END #
HTACCESS;

// SHOW THE STRING
echo htmlentities($htaccess);

// MAKE IT INTO AN ARRAY
$arr = explode(PHP_EOL, $htaccess);

// SHOW THE ARRAY
print_r($arr);

Open in new window

Ray Paseur

my array stops

What's the URL of your .htaccess file?  Or can you copy it and paste it into the code snippet?  I'd like to work with the real data.  Thanks, ~Ray
ellandrd

ASKER
>>Now tell us what sort of thing you want to change in the .htaccess file and I'll show you how to incorporate the changes and write it back.

I'm trying to update the:

allow from x.x.x.x #comment

I have a DB table containing IP addresses and comments.  I want to remove the .htaccess rules between # Restrict START # and # Restrict END # and insert new ones.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ellandrd

ASKER
I've attached my htaccess file (renamed it .txt)


htaccess.txt
Ray Paseur

to remove the .htaccess rules between # Restrict START # and # Restrict END # and insert new ones.
That seems to be fairly easy if I understand it correctly.  Back in a moment...
Ray Paseur

stops after 143 lines?

What is this thing about at line 142 in the file?

#   <script src="application.js?20100608">
Your help has saved me hundreds of hours of internet surfing.
fblack61
ellandrd

ASKER
Thats a comment in the expire headers/cache section of the htaccess file.
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ray Paseur

Sidebar note - I think that <script> tag, which is unclosed, is why you think that file_get_contents() cuts off.  It does not actually cut off, but your browser will have trouble rendering the string.  You can use view source to get a better picture of what is really going on.  HTH, ~Ray
Sandeep Kothari

@ellandrd :

the code you posted is perfect the only mistake was in preg_match you put php variables directly...

the only change needed is this line... replace the preg match line with this one and all is fine...

                  preg_match('/'.$start.'(.*)'.$end.'/s',$content,$matches);

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

preg_match('/'.$start.'(.*)'.$end.'/s',$content,$matches);

or maybe...

preg_match("/$start(.*)$end/s",$content,$matches);

Either way it might be useful to use preg_quote()
Sandeep Kothari

Ray.... you are reposting the same thing...  this is say 3rd time you are doing it !... anyways!
Ray Paseur

@kshna: The point of my post is that if ellandrd actually used single quotes around the PHP variables in the regular expression, a change to double quotes might do the job.  There are usually many ways to achieve the same objective, which in this case is variable substitution.  The reason I mentioned preg_quote() is given here.
http://www.php.net/manual/en/function.preg-quote.php

To quote, "useful if you have a run-time string that you need to match" and it looks to me that ellandrd may one day have a run-time string instead of a single variable assignment.

Best to all, over and out, ~Ray
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Terry Woods

Altering Ray's code (which is the same as kshna's) to include the preg_quote (very important), and to suggest some supporting code:
$start = '# Restrict START #';
$end = '# Restrict END #';
preg_match('/^(.*'.preg_quote($start,'/').')(.*?)('.preg_quote($end,'/').'.*)$/s',$content,$matches);

$htaccess_part1 = $matches[1];
$htaccess_part2 = $matches[2];  #This is the part you want to update
$htaccess_part3 = $matches[3];

<update $htaccess_part2>

file_put_contents($htaccess, $htaccess_part1.$htaccess_part2.$htaccess_part3);

Open in new window

ellandrd

ASKER
Thanks for all the input guys.

I couldn't get preg_quot or preg_match working?  Not sure if it was my htaccess comments or other rules like you suggested, that was causing issues?

Anyways, I did get it working using this solution by Ray.

BIG THANK YOU DUDE!

Ellandrd
Ray Paseur

Thanks for the points.  You might want to consider using this:
http://php.net/manual/en/function.flock.php
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.