How to add line using in .htaccess file using PERL ?

Hi,

I would like to request an assistant.

May i know how to add line in .htaccess file using PERL if the line is NOT ALREADY EXIST in the file ?

Eg:

The filename is /home/$username/public_html/.htaccess

I would like to add the following line in it :

suPHP_ConfigPath /home/$username/public_html
<Files php.ini>
order allow,deny
deny from all
</Files>

But the adding is only IF the above line is not already in the .htaccess .

Appreciates of any assistant.

Thank you.



LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TobiasCommented:
Dear,

The code provided should do the work.

Usage : nameofthescript.pl /home/$username/public_html/.htaccess suPHP_ConfigPath

Best Regards

#!/usr/bin/perl

if(-e $ARGV[0])
{

}
else
{
    print 'File does not exist.\n';
    exit;
}

open(INPUT,$ARGV[0]) or die 'Cannot open file: $!\n';

$exist_in_file=0;

while(<INPUT>){

    if (my ($found) = m/($ARGV[1])/o) {
	  $exist_in_file=1;
    }

}

close INPUT;


if ($exist_in_file == 1) {
print "found $ARGV[1] in $ARGV[0]\n";
#Do nothing because the content exist in the file
}
else
	{
	print "not found $ARGV[1] in $ARGV[0]";
	#Put the content in the file (at the end)
	print "...open the file for write\n";
	open(OUTPUT, ">>$ARGV[0]");
	print OUTPUT "\nsuPHP_ConfigPath /home/";
	print OUTPUT '$username';
	print OUTPUT "/public_html\n";
	print OUTPUT "<Files php.ini>\n";
	print OUTPUT "order allow,deny\n";
	print OUTPUT "deny from all\n";
	print OUTPUT "</Files>\n";
	close (OUTPUT); 
	}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.