Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

Can you remove stuff from .htaccess using php?

Asked Modified Viewed 2,907 times
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
asked
Fusioneer

I was wondering, I know you can write to .htacces using php but can you remove something from .htacces using php?

Example lets say I have added an ip to be denied in .htaccess, using php is it possible to remove entires?

Thanks
0 replies

13 posts

P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

See also http://php.net/manual/en/function.preg-quote.php
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

Yep that works. Thanks for your help.
0 replies
J
JoiNNN
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
answered
Veteran Member

That's basically what will do. Don't worry the content will still be there, assuming that you did the replacement/addition/removal of a particular line properly.
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

OK thanks...

Also see...

create_file("file.php", $output);


?That wont replace my file with a new one will it?
0 replies
J
JoiNNN
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
answered
Veteran Member

That should do. It might happen that you are left with blank new lines, to get rid of them too try
"#".$data['something']."(\n|".PHP_EOL.")?"#si"
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

Ah maybe your onto something there. Let me try something out and I'll see if it works for what I'm doing can...

$output = preg_replace("#".$data['something']."#i", "", $input);


do it like that coz it will be a specific thing it needs to remove when submit on $_POST
0 replies
J
JoiNNN
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
answered
Veteran Member

[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]$input = file_get_contents("file.php"wink;
$output = preg_replace("#what_to_find#i", "replacement", $input);
create_file("file.php", $output);[/syntaxhighlighter]
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

Example code please?
0 replies
P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

Read, remove the bad string, write.
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

I'll be using fwrite or similar to write the ip to .htaccess but what can I use to then remove it from .htaccess?
0 replies
J
JoiNNN
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
answered
Veteran Member

I remember Prestashop did some rewriting/editing in .htaccess when enabling/changing seo urls or other settings like compression and stuff, so it's possible.
You can do something like getting the file's content > put it in a variable > search and replace/remove what you need > rewrite the file.

Here, I use this function to write .php files in a script that helps translate/create locale files. Not tested under *nix.
[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]function create_file($ofile, $content) {
$oFile = fopen($ofile, 'w'wink;
if (flock($oFile, LOCK_EX)) {
fwrite($oFile, $content);
flock($oFile, LOCK_UN);
} else {
echo "<div class='admin-message'>Ooops! Something went wrong while atempting to write the file.<br />Please try again.</div>";
}
//Done writing, close file
fclose($oFile);
}[/syntaxhighlighter]
I could help with regex if you give more details about what are trying to achieve.
Edited by JoiNNN on 23-01-2013 03:44,
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

Yeh I did a bit searching myself on goggle first before posting this thread but did not find anything. Seems a bit weird you can write to a file using php but you cant remove.
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

From what I've checked or found is a lot of people are having issues with that.

I supposed you could create a list of the contents using PHP, then edit what you want on the list, then rewrite it. Best I could think of for now.

Not the correct answer you need, but some input!
0 replies

Category Forum

General Discussion

Labels

None yet

Statistics

  • Views 0 views
  • Posts 13 posts
  • Votes 0 votes
  • Topic users 4 members

4 participants

C
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
P
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
J
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
K
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet