A little bit of help please.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Goffie
Forum Newbie
Posts: 2
Joined: Wed Jul 24, 2002 5:53 pm
Location: Chilliwack, BC, Canada
Contact:

A little bit of help please.

Post by Goffie »

Hello,
I don't know much about php at all but I know the server that I'm running my website on supports it. I was wondering if it would be easy to use php to take a text file and implement it into my html pages.
I want the php code to be able to take it off the file server and then place it into my html so that updating a sidebar won't involve updating 20 different pages but instead just 1 txt document. Is this possible and how would I go about doing it?
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

Sure. Just place <!--sidebar--> in your HTML pages, and then from PHP replace <!--sidebar--> with the contents of the text file.

The more common approach, however, is to convert your HTML files to PHP and just do include('sidebar.inc')

If you don't know how to do this, let me know.
Goffie
Forum Newbie
Posts: 2
Joined: Wed Jul 24, 2002 5:53 pm
Location: Chilliwack, BC, Canada
Contact:

Post by Goffie »

Sadly I don't. I'm not exactly sure if that would do what I want but I think I can explain this better if I was to word it this way.

I have sidebar.txt, that file contacts <a href="blahblahblah.html"> Blah </a> and so on.
Then on the index.html page in a cell on the main table I want to inset all the contents of sidebar.txt, if possible with just one command that brings it from the source.

Now if this <!--sidebar--> does that where does it know to load the text file from?
pb2ya
Forum Commoner
Posts: 42
Joined: Sat Jul 06, 2002 5:20 pm
Location: ATL
Contact:

ok here

Post by pb2ya »

the code is this:

Code: Select all

<?php
require('page.html');
?>


-or-


use iframes if you dont know php.
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

The only way to do it, I think, is to set all your HTML files to use PHP, and place include('sidebar.txt') where you want the sidebar.

Or you could have a "central" PHP file that sends out all the HTML files. You would use it like this index.php?file=mypage.htm. You can go this route by placing the following in the index.php file:

Code: Select all

<?

// This prevents things like "index.php?page=../../etc/passwd"
$html_files = array('mypage.htm', 'mypage2.htm');

if (!in_array($_GET&#1111;'page'], $html_files))
	die("Invalid page $_GET&#1111;page]");


// First get the HTML page
$fp = fopen($_GET&#1111;'page'], 'r');

$data = fread($fp, filesize($_GET&#1111;'page']));

fclose($fp);


// Then get the sidebar
$fp = fopen("sidebar.txt", 'r');

$sidebar_data = fread($fp, filesize("sidebar.txt"));

fclose($fp);


// Finally, insert the sizebar
print str_replace('<!--sidebar-->', $sidebar_data, $data);

?>
Post Reply