Val
Val

Reputation: 17522

htaccess assisting a php file

I have many pages my problem stands that I need to open all my php files and add a line of code at the top of each php page.

<?php 
if(!defined('no_d_a')){die('What The ... how did you get here :) ');} /* no direct access */ 
// rest of existing code here //
?>

Question is can I instead do this on a .htaccess file because I am feeling abit lazy :)

TIA

Upvotes: 0

Views: 377

Answers (2)

powtac
powtac

Reputation: 41040

There is a PHP ini setting called: auto_prepend_file, where you can set the name of a file wich will be included into every PHP script at the top!

You can now set this setting in your .htaccess like this:

php_value auto_prepend_file prepend.php

Check this good tutorial

Upvotes: 0

CarpeNoctumDC
CarpeNoctumDC

Reputation: 1760

I think what your wanting is a way to prepend the files with htaccess? If so here ya go:

Save this as 'prepend.php'

<?php
  if(!defined('no_d_a')){} /* no direct access */ 
?> 

in your .htaccess add:

<FilesMatch "\.(php)$">
php_value auto_prepend_file "/home/*******/public_html/prepend.php"
</FilesMatch>

it will prepend all .php files with 'prepend.php'

You may want to have some conditional clauses.. but that should get you started..


You can also append files with 'auto_append_file' instead of 'auto_prepend_file'

Upvotes: 3

Related Questions