PHP to call CGI generated sitemap

I am new here, so I hope this is the correct place to post this.
I am using a script to automatically generate a sitemap. The sitemap gets generated from a Perl file that is being treated as a CGI script.
The file is called from a PHP include. I’ve used PHP includes before, so I do not know what the problem is.

The script by itself works fine when it is pointed to directly in my browser, like this:

<a href="../cgi-bin/tree/tree.pl">go to sitemap</a>

It shows the sitemap of the directory it is supposed to map, so I know it works. It generates a nested list.

But when I call it from a PHP webpage file using a PHP snippet (a virtual include) like this:
[php]<?php virtual ("../cgi-bin/tree/tree.pl"); ?>[/php]
the page is blank from that point in the code on down!

This is a very simple page I created to test it, with nothing else interfering.

The way is supposed to work is that the PHP snippit is supposed to embed the sitemap in an HTML (PHP) file.

I’ve tried using the word include instead of virtual & that causes an error.

I have been struggling with this, so I hope someone can help! I know other people have used this method but I do not know what they did differently.

if your php file is in the web root directory, remove the …/

also try providing the full path to your cgi file.

Thank you. But the PHP file is not in the root directory. Providing the full path to the CGI file does not do anything different. The CGI correctly works as a sitemap & show all the files in the correct folder, BUT only when I link directly to the CGI (PL) file), as I said. The Problem looks like it is the PHP code snippet, not the file path.

I have tried making the sitemap for my root directly only & the same issue occurs.

Could it possibly a platform problem , or a PHP version problem? I am on an older Mac, checking this in both Safari & Firefox, and my hosting company uses PHP version 5.2.17.
It is an OLD PERL script, but the script worked at school on their server when students used it. School’s out for the summer, tho’, so I cannot check it there, and I cannot see the students’ code since the web pages are PHP files.

Well it could be a lot of things… Hows that for an answer.

Almost all PERL scripts are old and have lots of security issues too.

Since, I didn’t help and don’t have much else to offer in the way of fixing what you have, how about Googling ‘PHP site map scripts’. I’m guessing there are dozens of free ones available that will need little or no tweaking to make work. Just a thought…

I have tried some, but I will try again.

Maybe somebody else can tell me in there is something wrong with using “virtual” for an include. Thanks.

Ok, I found a really simple one that works. But the site i got it from says nothing about excluding files or files in specific folders. I would like to not list files from specific folders, such as my includes folder (but it lists these because they are PHP files).

All my web pages on this site are PHP files, but there are some I do not want to show because they have other uses.

Anyone know how i can do that?

Let me know if it is better to post the code of the script here.

try adding something like this…

$exclude_list = array(".", “…”, “thisfolder”, “thatfile.txt”); // files you don’t want to show!

$dir_path = “.”; // or full path to base directory

and…

$items = array_diff(scandir($dir_path), $exclude_list);

foreach($items as $file) {
// Yadda yadda…
}

Your script may already have something like this…

Part of this makes sense to me but I don’t know where to put it!

Somewhere in the code is a routine to read the files. It may not match what I gave completely but the theory is the same.

So, array_diff(array1, array2) removes the items from array1 that are in array2. This leaves only the items left that you want. If this is a recursive function you can do this also.

It would be helpful to actually see the code we’re talking about…

Here’s the code for the file:

[code]

SiteMap Test body {font-family: verdana; line-height: 1.2 } /* Indent Styles */ .0 { text-indent: 0px; font-size: 12pt; font-weight: bold } .1 { text-indent: 20px; font-size: 11pt } .2 { text-indent: 40px; font-size: 10pt } .3 { text-indent: 50px; font-size: 8pt } .4 { text-indent: 60px; font-size: 8pt } .5 { text-indent: 70px; font-size: 8pt }
    <?php // starting directory. Dot means current directory $basedir = ".";

    // function to count depth of directory
    function getdepth($fn){
    return (($p = strpos($fn, “/”)) === false) ? 0 : (1 + getdepth(substr($fn, $p+1)));
    }

    // function to print a line of html for the indented hyperlink
    function printlink($fn){
    $indent = getdepth($fn); // get indent value
    echo “<li class=”$indent"><a href="$fn">"; //print url
    $handle = fopen($fn, “r”); //open web page file
    $filestr = fread($handle, 1024); //read top part of html
    fclose($handle); //clos web page file
    if (preg_match("/.+</title>/i",$filestr,$title)) { //get page title
    echo substr($title[0], 7, strpos($title[0], ‘/’)-8); //print title
    } else {
    echo “No title”;
    }
    echo “\n”; //finish html
    }

    // main function that scans the directory tree for web pages
    function listdir($basedir){
    if ($handle = @opendir($basedir)) {
    while (false !== ($fn = readdir($handle))){
    if ($fn != ‘.’ && $fn != ‘…’){ // ignore these
    $dir = $basedir."/".$fn;
    if (is_dir($dir)){
    listdir($dir); // recursive call to this function
    } else { //only consider .html etc. files
    if (preg_match("/[^./].+.(htm|html|php)$/",$dir,$fname)) {
    printlink($fname[0]); //generate the html code
    }
    }
    }
    }
    closedir($handle);
    }
    }
    // function call
    listdir($basedir); //this line starts the ball rolling
    ?>

[/code]

Thanks!

ok it looks like it is a recursive function, but not a problem…

this script is set to ignore the two most common items to be ignored when looping through a directory…

if ($fn != '.' && $fn != '..'){ // ignore these

if you have two or three other items you want to exclude you could tack on to these. It’s bad form to use too many if ands, I don’t know exactly what the ‘limit’ is but I’ve always used the motto ‘the less the better’. Two is clearly ok, it used often.

When you have a lot to exclude, build the exclude array as I said earlier then do this;

while (false !== ($fn = array_diff(readdir($handle), $exclude))){

And this line is no longer needed because . and … are now in your exclude array…

if ($fn != ‘.’ && $fn != ‘…’){ // ignore these

don’t forget to zap the corresponding curly bracket.

Thanks for your attempts to help me. I still did not understand how to do what you suggested …I guess I should have posted in the “beginners” forum.

I wound up finding an even simpler script which already shows excluded files in a very straightforward array, and got that to work well. :slight_smile:

I’ve given you it all…

In an earlier post I said (sorry I changed var names between posts);

Build a list of file you don’t want (note the . and …) - each item is between quotes, replace my two samples and add as many as needed, each separated by a comma (do not put a comma after the last item).

$exclude_list = array(".", "..", "thisfolder", "thatfile.txt"); // files you don't want to show!

Replace this line in your code;

while (false !== ($fn = readdir($handle))){

with this line;

while (false !== ($fn = array_diff(readdir($handle), $exclude_list))){

Also remove the next line which is now part of $exclude_list and not needed.

if ($fn != '.' && $fn != '..'){ // ignore these

Once that line is gone, you also need to remove the end }, not any }, but the one that corresponds to the one in the line you deleted. You’ll have to make sure all opening { has a } and the closing } does not include or exclude any lines of code it shouldn’t.

Sponsor our Newsletter | Privacy Policy | Terms of Service