how could i do this?: i want to add a bit of tracking php/html code to a bunch of webpages (just before the closing body tag) but i don’t want to tamper with the pages themselves at all. any ideas? thanks.
If the webpages were built doing this:
<?php
include 'header.php';
?>
// loads a content
<?php
include 'footer.php';
?>
Then it would be easy to add some code to the footer, you’d also be able to add code to the header. I’d consider dealing with this issue once and for all.
Otherwise you have got to get into a big search and replace operation, there are specialized tools much better suited for doing that.
thanks. no i don’t have such code (header, footer) in these pages.
one way i thought of, i have no idea if this would work because of missing knowledge on my part but i reckon something like it might work, was using auto-prepend-file and auto_append_file (http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file http://www.php.net/manual/en/ini.core.php#ini.auto-append-file) – setting those in .htaccess – and in the prepended code set all output to be captured rather than outputting it (e.g. using ob_start() ), then in the appended code somehow modify the captured output (is it possible to perform string operations on the output captured from ob_start? – if so a backwards find and replace for ‘</body>’ would do it) and then output the modified output.
Well, I can remember getting into roughly the same situation not long after starting out.
I just copied all the files back to my PC (then backed them all up), downloaded a prog called “Search and Replace” or similar, then on some test copies at first, I would replace <body> tags with say, <? include ‘footer.php’; ?>
Then copy them back to the live server.
But you should perhaps take the opportunity to take a good hard look at those files, and where you keep seeing duplicated PHP code, post samples here and someone will suggest other methods of removing that duplication thus maybe making life a bit easier for you in the future.
Even being able to simply open one file to add, say, Google Analytics code to the foot of all pages is an immensely powerful thing.
Perhaps you could not merely replace the closing </body> tag, but all the visible footer and associated foot of page navigation too. A good “Search and Replace” tool will handle this for you. If you are feeling brave you can do it on your live server (if it is *nix) using basic command line tools.
Just back everything up first.