Page 1 of 1

Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 7:11 am
by AGISB
Hi

a customer of mine wants a username checking script implemented on his existing html members area. My current solution is to parse html thru php and add an include with the script to the beginning. All working fine except for the problem that I only add this to the main index files. Smart people could be able to access the area by going to one of the other 400000!!!! HTML files.

You could imagine, that I don't really want to change 400000 HTML pages so I was wondering, if it is possible to use the .htaccess file to make all html pages execute that include script before it proceeds to the browser?

Does it work to route all .html pages to e.g. index.php and add the path and called file as a query string, execute the script and make a location call to the initial script?

Re: Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 7:36 am
by Apollo
The approach you suggest would probably be possible. But then again, having 400000 pages all as separate HTML seems a pretty retarded setup to me. So perhaps it's a good idea to start redesigning that in the first place :)

Re: Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 7:45 am
by AGISB
Well it is a adult paysite online since 1998 displaying about 4 Million pictures and movies. So the design is fine as it is :D just a pain in the ass when you have to change things like that. I will try and see how much burden this is on the servers. At least he is load balancing over 5 quad core cpus now :lol:

Re: Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 9:47 am
by Apollo
AGISB wrote:displaying about 4 Million pictures and movies. So the design is fine as it is :D
Well, if it's using a separate html for each picture (or gallery) or movie, then no, the design ain't fine,
just a pain in the ass when...
and there's why :)

However, if it turns out you do need to edit 4 zillion .html files, you can of course automate that. Any decent editor or some smart scripting should be able to do the job.

Re: Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 10:20 am
by jayshields
You should re-do the site architecture completely. Failing that - move all the HTML files to a non-public directory, and put an index.php file into the old HTML file directory which basically just includes one of the HTML files based on a GET parameter. Now you have one entry point and you can do whatever you want with logins, etc.

Re: Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 2:00 pm
by AGISB
The problem simply is, that he does not want to spend the money for a rework of the site. The site basically has grown to a monster as sections where added over the years. Nothing like a good structure is found. His problem is that he used an apache module called iprotect with his old machines. As he now moved to new machines he runs apache 2.2 and that module was not maintained sinse 2003 :roll: . So he needs a quick and dirty solution. I did the quick solution by just parsing the main index files thru php and added the ip checking but as I know the flaws I am thinking about a better solution. Thank god I don't have to care about SEO in the members section :lol: ...

Re: Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 3:24 pm
by Apollo
Hehe, sounds nice :)

Seriously, it ain't my business so I don't care, but he's really mistaken if he thinks he's saving money by letting his site run with a total crap setup like that. Getting the stuff redesigned properly won't have to be expensive at all, and will save SO much trouble (and hence time and money) later on. This way it's just gonna collapse sooner or later.

I don't know if this is really a serious business or just some fun / experimental thingy, but if it's business, he should definitely reconsider having his stuff fixed :)

Re: Execute PHP code before HTML pages

Posted: Mon Apr 06, 2009 3:36 pm
by Christopher
You can do this pretty easily with mod_rewrite and a simple rule like most Front Controllers use:

RewriteRule !\.[a-zA-Z0-9]{2,4}$ index.php

That will send everything that ends in .aa[aa] to index.html. You can check in $_SERVER for the actual path. It will leave existing directories with an index.html/index.php working as is. Or you can ahve RewriteRule !\.*$ index.php to send everything through index.php.

Re: Execute PHP code before HTML pages

Posted: Tue Apr 07, 2009 4:26 am
by AGISB
arborint wrote:You can do this pretty easily with mod_rewrite and a simple rule like most Front Controllers use:

RewriteRule !\.[a-zA-Z0-9]{2,4}$ index.php

That will send everything that ends in .aa[aa] to index.html. You can check in $_SERVER for the actual path. It will leave existing directories with an index.html/index.php working as is. Or you can ahve RewriteRule !\.*$ index.php to send everything through index.php.
Unfortunately those Rewrite Rules do nothing for me. For future reference following does work for me.

Code: Select all

 
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*).html$ index.php?$1.html [L]
 
I can get the info out of the query string and display the html page after I did what I wanted to do in index.php. The first line is to eliminate all dead links form executing. However you should still handle the possible direct index.php?notexisting.html call to avoid error messages ...