Will PHP work for me?

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
Aleister
Forum Newbie
Posts: 20
Joined: Wed Apr 07, 2004 5:43 pm

Will PHP work for me?

Post by Aleister »

I have a large number of simple html files. I was trying to figure out how I can use php to have a list of these files on a page, and when one is clicked on, it opens the file up in my 'template'. Instead of creating a full page for each file, it sounds like PHP would do the job nicely.

The problem is I cannot find any tutorials anywhere that cover something this simple with PHP. I just keep finding articles on writing your own functions, or basic tutorials that show how to put the current date/time on the page etc..

Anyone have any suggestions or know of a good site I could check out? Thanks.
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

Hey, shouldn't be a problem!

To get a list of the files, you should have php iterate through your directory like so:

Code: Select all

<?php

$dir = "what/ever/you/want/";
if ($handle = opendir($dir)) { 
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") { 
            echo nl2br ( $file . "\n" ); 
        } 
    } 
    closedir($handle); 
}
?>
Ok, that's the list sorted, now you want links. When the script is displaying the filename, make it produce a link in the form script.php?file_to_include=blah.html

eg, change echo nl2br ( $file . "\n" );

to

Code: Select all

<?php
echo nl2br ( "<a href='yourscript.php?file_to_include=" . $file . "' >" . $file . "\n" ); 

?>
in yourscript.php, have a section which includes the file:

Code: Select all

<?php

if ( isset ( $_GET["file_to_include"] ) {

include ( $_GET["file_to_include"] ); // You probably should make sure the file exists first before including it, also need to check that the file is allowed to be included, so as to prevent a security risk

} else {

echo "No file to include";

}
?>
So anyway, hope this helps, I havn't described it very well, and the code is just throne together untested, so no guarentee! Also, make sure to test for any security holes, eg making sure that directories above your website cannot be included if the user changes the website url. Possibly you could ensure that the referer to the page was always your listing page, that could help.

David
Aleister
Forum Newbie
Posts: 20
Joined: Wed Apr 07, 2004 5:43 pm

Post by Aleister »

Thanks for the info.. I don't know anything about PHP yet so I don't know if I should dive into it quite yet.. I had not even thought about security etc.. I am thinking of just using something like PHPNuke for what I need.. I will sit down and learn to use PHP 'manually' eventually.. I just have a lot of things i'm learning currently as it is :)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I had not even thought about security etc.. I am thinking of just using something like PHPNuke
Sorry, can't resist ... but 'security' and 'phpNuke' shouldn't be used in the same sentence, it's security history isn't too great :o
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

markl999 wrote: Sorry, can't resist ... but 'security' and 'phpNuke' shouldn't be used in the same sentence, it's security history isn't too great :o
Only in the same way that the wonderful thing called the oxymoron allows Microsoft and Works to be used in the same sentence. :)
Aleister
Forum Newbie
Posts: 20
Joined: Wed Apr 07, 2004 5:43 pm

Post by Aleister »

Perhaps, but PHP Nuke is going to take a lot less of php coding for a lot more features. I have heard it is not the most secure either, but more secure than my current attempts would be :)

I do intend to learn it at some point as I mentioned, that day is just not today :)
Aleister
Forum Newbie
Posts: 20
Joined: Wed Apr 07, 2004 5:43 pm

Post by Aleister »

Well looking over it again, a system like PHP Nuke is not going to work at all for the kind of site I have.. I am going back to my original plan of just using a bit of php for the lyrics thing...

Now I need to go about making it secure! :P
Post Reply