Jump to content

Insert HTML Code for all the pages in a directory


sachithd

Recommended Posts

Can any one explain me whether its possible to insert an HTML code for all the pages in a directory with out modifying all the files.eg. Execute external file automatically when some one view a page.I need some thing like free web hosting servers do. How do they add their ads to all my html pages which I have uploaded onto their server.

Link to comment
Share on other sites

There is also the Apache Directives which will prepend and postpend stuff. But you need server access to modify that.
Thanks for your replies. I have access to the server. Can you explain how can this be done. If I included PHP or ASP include do I have to modify my code in each and every html page?
Link to comment
Share on other sites

If I included PHP or ASP include do I have to modify my code in each and every html page?
Not necessarily. You could have one PHP page that will include the HTML pages. This however would mean that the URL will look something likehttp://example.com/index.php?page=index.htmlThat is, instead of having just a path to the file, you'll also have a query string that contains a query string variable with a corresponding query string value, which in the above example will point to the file to include.
Link to comment
Share on other sites

To demonstrate what Boen_Robot said (in PHP):

<?phpinclude("header.php"); //Include a header file$page = $_GET['page']; //Get the page variable from URLinclude($page); //Inlcude the pageinclude("footer.php"); //Include a footer file?>

See - hardly any work needed :)

Link to comment
Share on other sites

That's assuming that the pages are not complete pages, and that he wants to add something to the beginning or the end. If he wants to add something in the middle, then he would need to read the document and determine the point at which he wants to insert (he did specify he wants to insert code, not prepend or append) the code.

Link to comment
Share on other sites

Thanks for the replies. The thing is my old web site is frame based. there are more than 500 pages in that web site. I want to add a link to my new web site on all the pages of my old web site.If you search for a query in google it doesn't appear correctly because it is a frame based web site. It shows only the relavant page. ( with out navigation menu etc.)So i'd like to add a URL to all the pages so even people find my old web site on google they have access to my new web site from the URL.

Link to comment
Share on other sites

Well, this is the problem with making 500 static pages. It's not easy to update. Software like Dreamweaver has a template feature where you can create pages based on a template, make a change to the template and it will update all the pages that use it, but you would have had to have planned on that from the beginning. Adding HTML to 500 pages as an afterthought usually involves editing the majority of the pages. If you want to add the HTML code in the same place on every page, where the HTML of the existing pages is the same (a div with a certain id, or some text that is on all the pages, or some bookmark where you want to insert), then you can write a script that will open all the pages, find the place in each page where the code should be inserted, and insert it. Show an example of what you want the HTML to look like before and after the change.

Link to comment
Share on other sites

Well, this is the problem with making 500 static pages. It's not easy to update. Software like Dreamweaver has a template feature where you can create pages based on a template, make a change to the template and it will update all the pages that use it, but you would have had to have planned on that from the beginning. Adding HTML to 500 pages as an afterthought usually involves editing the majority of the pages. If you want to add the HTML code in the same place on every page, where the HTML of the existing pages is the same (a div with a certain id, or some text that is on all the pages, or some bookmark where you want to insert), then you can write a script that will open all the pages, find the place in each page where the code should be inserted, and insert it. Show an example of what you want the HTML to look like before and after the change.
Thank you very much for your reply. What sort of a script do I have to write? I need just a link (My New Web site www.mynewwebsite.com ) on top of each page. My web server supports Apache Directives. Is it posible to insert the code as mentioned above. I tried that. But it didn't work. How do I create a .htaccess file?
Link to comment
Share on other sites

You could try this (but it would only work if your <body> tag had no attributes, if it did then Regular Expressions would be needed, and I hate regular expressions :) ):

<?php$page = file_get_contents($_GET['page']); //Get the page specified in the URL$extraText = "<a href=\"http://www.mynewwebsite.com/\">My New Web Site</a><br />"; //Replace with required HTML$page = str_replace("<body>","<body>$extraText\n", $page); //Replace the textecho $page; //Echo it?>

So, to access about.html from this page (called, for instance, index.php) with the added text you would type "index.php?page=about.html"

Link to comment
Share on other sites

You could try this (but it would only work if your <body> tag had no attributes, if it did then Regular Expressions would be needed, and I hate regular expressions :) ):
<?php$page = file_get_contents($_GET['page']); //Get the page specified in the URL$extraText = "<a href=\"http://www.mynewwebsite.com/\">My New Web Site</a><br />"; //Replace with required HTML$page = str_replace("<body>","<body>$extraText\n", $page); //Replace the textecho $page; //Echo it?>

So, to access about.html from this page (called, for instance, index.php) with the added text you would type "index.php?page=about.html"

Hi Synook, Thanks for your reply. Sorry to ask this stupid question. Where do I put this code in? What are regular expressions.? I guess I should create a index.php.I tried this ,. I think it didn't work becuase in my pages the body tag has atrributes. What can I do?
Link to comment
Share on other sites

Well, if all your pages have the same attributes in the body tag (exactly as in same names and values) then you could just replace the <body> in

$page = str_replace("<body>","<body>$extraText\n", $page); //Replace the text

with your body tag, e.g.

$page = str_replace("<body onload=\"display()\">","<body onload=\"display()\">$extraText\n", $page); //Replace the text

Umm... regular expressions? http://au2.php.net/manual/en/function.preg-replace.php but don't ask me :)

Link to comment
Share on other sites

First, let me mention that Word is a terrible HTML generator, and the fact that Word 11 was used to create all of these pages might make this a whole lot harder then it has to be. I mean look at all this useless code:

<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <div align=center>  <table class=MsoNormalTable border=0 cellpadding=0 width="100%"   style='width:100.0%;mso-cellspacing:1.5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>   <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>	<td colspan=3 style='padding:.75pt .75pt .75pt .75pt'>	<p class=MsoNormal align=center style='text-align:center'><span	style='font-family:Arial'> <o:p></o:p></span></p>	</td>   </tr>

What does all that do? Oddly enough, the point of all that code is to create ... a space<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'> <td valign=top style='padding:.75pt .75pt .75pt .75pt'> <div align=center> <table class=MsoNormalTable border=0 cellpadding=0 width="100%" style='width:100.0%;mso-cellspacing:1.5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'> <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'> <td colspan=3 style='padding:.75pt .75pt .75pt .75pt'> <p class=MsoNormal align=center style='text-align:center'><span style='font-family:Arial'> <o:p></o:p></span></p> </td> </tr>So, your pages could probably be 10 times smaller if it weren't for all the Word-added cruft, and you could be saving money on your bandwidth and storage fees. Anyway.The top frame has this piece of code to show the new address:

<table class=MsoNormalTable border=0 cellpadding=0 width=755 style='width:566.25pt; mso-cellspacing:1.5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'> <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:#FF6600;mso-bidi-font-weight:normal'>Best Prices  for Barcode Products & Solutions !</span></i><span style='font-size:10.0pt'><o:p></o:p></span></p>  </td> </tr> <tr style='mso-yfti-irow:1;mso-yfti-lastrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:green;mso-bidi-font-weight:normal'>Visit our  new e commerce web site <a href="http://www.barcode-uk.com"  title="BARCODE TECHNOLOGIES NEW WEB STORE" target="_new">www.barcode-uk.com</a><o:p></o:p></span></i></p>  </td> </tr></table>

If you want to add that code to the top of the page right after the body and the initial opening tags, you could do something like this:

<?php$html = file_get_contents("page1.html");$old_text = <<<EOT<body lang=EN-US link=blue vlink=blue style='tab-interval:36.0pt'><div class=Section1><div align=center>EOT;$new_text = <<<EOT<table class=MsoNormalTable border=0 cellpadding=0 width=755 style='width:566.25pt; mso-cellspacing:1.5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'> <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:#FF6600;mso-bidi-font-weight:normal'>Best Prices  for Barcode Products & Solutions !</span></i><span style='font-size:10.0pt'><o:p></o:p></span></p>  </td> </tr> <tr style='mso-yfti-irow:1;mso-yfti-lastrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:green;mso-bidi-font-weight:normal'>Visit our  new e commerce web site <a href="http://www.barcode-uk.com"  title="BARCODE TECHNOLOGIES NEW WEB STORE" target="_new">www.barcode-uk.com</a><o:p></o:p></span></i></p>  </td> </tr></table>EOT;$html = str_replace($old_text, $old_text . "\n" . $new_text, $html);file_put_contents("page1.html", $html);?>

That is the code to add the new text first thing on the page for a specific page ("page1.html"). It will read the current page, add the code, and save the new page. There is some extra code that you would need to read the names of all files in a folder and loop through them to do this to each file, but this is the code to do the actual replacement. You can see some examples of reading the files in a folder here:http://www.php.net/manual/en/function.scandir.phpAlso, keep in mind that in order to do this you need to have PHP running on the server. You would write a script containing the code to read all files in your web folder and do the replacement for each one, and in order to execute that script you would need PHP to be installed and configured on the web server.

Link to comment
Share on other sites

Hi... Thank you very much once again for your reply.I'm still not sure where do I have to add the code you've given. and I need the URL not only on the top frame but all the pages in the directory. My sever supports PHP and how do I run a script to add the URL on all the pages. As I've mentioned before Google find some pages in my old web site but it shows only one page. ( without frames) So I'd like to add the URL to all the pages in the directory.

Link to comment
Share on other sites

Yes, I understand that. The PHP code there will add some text to a single page. You can expand on that code to make it read all the files in a certain directory and do that for every file in the directory. This would be a new file, you don't add this PHP code to any existing file. You just run it once and it would add the code to all files. You'll want to make a backup of all the files first in case something goes wrong. This includes the extra code:

<?phpfunction replace_files($dir){  $filelist = scandir($dir);  for ($i = 0; $i < count($filelist); $i++)  {	if (is_dir($dir . $filelist[$i]) && $filelist[$i] != "." && $filelist[$i] != "..")	  replace_files($dir . "/" . $filelist[$i]);	else	  replace_text($dir . "/" . $filelist[$i]);  }}function replace_text($filename){  $html = file_get_contents($filename);    $old_text = <<<EOT<body lang=EN-US link=blue vlink=blue style='tab-interval:36.0pt'><div class=Section1><div align=center>EOT;  $new_text = <<<EOT<table class=MsoNormalTable border=0 cellpadding=0 width=755 style='width:566.25pt;mso-cellspacing:1.5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'><tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:#FF6600;mso-bidi-font-weight:normal'>Best Prices  for Barcode Products & Solutions !</span></i><span style='font-size:10.0pt'><o:p></o:p></span></p>  </td></tr><tr style='mso-yfti-irow:1;mso-yfti-lastrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:green;mso-bidi-font-weight:normal'>Visit our  new e commerce web site <a href="http://www.barcode-uk.com"  title="BARCODE TECHNOLOGIES NEW WEB STORE" target="_new">www.barcode-uk.com</a><o:p></o:p></span></i></p>  </td></tr></table>EOT;  $html = str_replace($old_text, $old_text . "\n" . $new_text, $html);    file_put_contents($filename, $html);}replace_files("/path/to/directory");?>

Link to comment
Share on other sites

Thank you for your reply. and I tried to run that code. But this is the result I get when I execute my page.Warning: Unknown(/footer.htm): failed to open stream: No such file or directory in Unknown on line 0Warning: Unknown(/footer.htm): failed to open stream: No such file or directory in Unknown on line 0Warning: (null)(): Failed opening '/footer.htm' for inclusion (include_path='.:/usr/local/lib/php') in Unknown on line 0I'm not familier with PHP but I can try to work something if you show me the path.

Link to comment
Share on other sites

The code wasn't working right, but those error messages are not from the code I posted. Try this version:

<?phpif(!function_exists('scandir')) {	function scandir($dir, $sortorder = 0) {		if(is_dir($dir) && $dirlist = @opendir($dir)) {			while(($file = readdir($dirlist)) !== false) {				$files[] = $file;			}			closedir($dirlist);			($sortorder == 0) ? asort($files) : rsort($files); // arsort was replaced with rsort			return $files;		} else return false;	}}define('FILE_APPEND', 1);function file_put_contents($n, $d, $flag = false) {	$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';	$f = @fopen($n, $mode);	if ($f === false) {		return 0;	} else {		if (is_array($d)) $d = implode($d);		$bytes_written = fwrite($f, $d);		fclose($f);		return $bytes_written;	}}function replace_files($dir){  $filelist = scandir($dir);  for ($i = 0; $i < count($filelist); $i++)  {	if (is_dir($dir . "/" . $filelist[$i]) && $filelist[$i] != "." && $filelist[$i] != "..")	  replace_files($dir . "/" . $filelist[$i]);	elseif (!is_dir($filelist[$i]) && $filelist[$i] != "." && $filelist[$i] != "..")	  replace_text($dir . "/" . $filelist[$i]);  }}function replace_text($filename){  if (!is_dir($filename))  {	echo "replace {$filename}<br>";	$html = file_get_contents($filename);	$old_text = <<<EOT<body lang=EN-US link=blue vlink=blue style='tab-interval:36.0pt'><div class=Section1><div align=center>EOT;	$new_text = <<<EOT<table class=MsoNormalTable border=0 cellpadding=0 width=755 style='width:566.25pt;mso-cellspacing:1.5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'><tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:#FF6600;mso-bidi-font-weight:normal'>Best Prices  for Barcode Products & Solutions !</span></i><span style='font-size:10.0pt'><o:p></o:p></span></p>  </td></tr><tr style='mso-yfti-irow:1;mso-yfti-lastrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:green;mso-bidi-font-weight:normal'>Visit our  new e commerce web site <a href="http://www.barcode-uk.com"  title="BARCODE TECHNOLOGIES NEW WEB STORE" target="_new">www.barcode-uk.com</a><o:p></o:p></span></i></p>  </td></tr></table>EOT;	$html = str_replace($old_text, $old_text . "\n" . $new_text, $html);	file_put_contents($filename, $html);  }}replace_files("/path/to/replace");?>

Make sure to back up the files first.

Link to comment
Share on other sites

hi.. THank you very much for your reply. It gives the same error. This is how I executed the scripthttp://www.barcode-technologies.co.uk/run.phpCan you please explain what does this code do..?Does this replace the body tag?if the body tag is different on other pages what will hapen?there are different types of files in the directory. images , php, htm, html. Can we write a script which inserts the code rather than replacing?PSI deleted the htaccess file. then the error is replace /path/to/replace/Warning: file_get_contents(/path/to/replace/): failed to open stream: No such file or directory in /home/9629/barcode/www.barcode-technologies.co.uk/public_html/run.php on line 47Please advicePS.I changed the path. /path/to/replace/ to blank. then it starts to give so many errors. You can see the errors if you run the above URL.

Link to comment
Share on other sites

Good god, are you telling this to replace all files in the root directory? The path that you give needs to be the path to your HTML files. It needs to be the path to the root directory of your web site, not the root directory of the entire computer. Also, you probably should not have deleted the htaccess file, hopefully you made a backup of it first. I thought if I wrote this:replace_files("/path/to/replace");You would realize that you need to fill in the actual path there, I doubt that there is a folder on your server called "/path/to/replace". You need to change that to be the actual path where you want to replace files.

Can you please explain what does this code do..?
It replaces this:
<body lang=EN-US link=blue vlink=blue style='tab-interval:36.0pt'><div class=Section1><div align=center>

with this:

<body lang=EN-US link=blue vlink=blue style='tab-interval:36.0pt'><div class=Section1><div align=center><table class=MsoNormalTable border=0 cellpadding=0 width=755 style='width:566.25pt;mso-cellspacing:1.5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'><tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:#FF6600;mso-bidi-font-weight:normal'>Best Prices  for Barcode Products & Solutions !</span></i><span style='font-size:10.0pt'><o:p></o:p></span></p>  </td></tr><tr style='mso-yfti-irow:1;mso-yfti-lastrow:yes'>  <td valign=top style='padding:.75pt .75pt .75pt .75pt'>  <p class=glidetext align=center style='text-align:center'><i><span  style='font-size:11.0pt;color:green;mso-bidi-font-weight:normal'>Visit our  new e commerce web site <a href="http://www.barcode-uk.com"  title="BARCODE TECHNOLOGIES NEW WEB STORE" target="_new">www.barcode-uk.com</a><o:p></o:p></span></i></p>  </td></tr></table>

Does this replace the body tag?
No, it adds code after it.
if the body tag is different on other pages what will hapen?
It won't add or replace anything, it looks for a specific piece of code (including the body tag) to add the new code after.
there are different types of files in the directory. images , php, htm, html.
Is that a question?
Can we write a script which inserts the code rather than replacing?
This doesn't replace.
Link to comment
Share on other sites

Thanks for your reply. I changed the path to "/public_html". But it still gives an error. May be i'm wrong. How do I give the path.is there a way of adding your code for all the html, php, htm files in the directory just after the body tag. Even if the body tag is different.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...