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
smksaAsked:
Who is Participating?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.