Page 1 of 1

Very Strange PHP Behavior Please Help

Posted: Wed May 04, 2005 4:40 pm
by Benjamin
This code is horribly inefficient and it’s a rough draft for something I am making on my machine but (really) it should work.

I am not getting any errors but I am having some incredibly weird issues, like being able to redirect after sending text and not getting any errors even though I set a session after sending data.

Can anyone give me a clue as to what the heck is going on? It’s probably really simple but I am stumped.

NOTE!!! I have apache set to pull one specific file regardless of the URL. The code below, determines whether or not to send an index page. The Apache setting is as follows.

Code: Select all

DirectoryIndex /sys.php
The sys.php code is as follows.

Code: Select all

this is a header killer
<?php
session_start();

function HideIt($WhatThis)
  {
  $Matched = false;
  $HideIt = array("index.php", "RECYCLER", "System Volume Information", "Thumbs.db", "Desktop.ini", "sys.php");
  $ShouldI = @str_replace($HideIt, "", $WhatThis);
  // see if there was any matches
  if ($WhatThis != $ShouldI) $Matched = true;
  return $Matched;
  }

$DirectoryToOpen = $_SERVER['REQUEST_URI'];  
?>
<html>
<head>
  <title>Welcome To System One</title>
</head>
<body>
<p><b>systemone<?php echo str_replace("%20", " ", $_SERVER['REQUEST_URI']); ?></b></p>
<pre>
<?php

// LOOP THROUGH ALL DIRECTORIES
if ($handle = opendir(str_replace("%20", " ", $DirectoryToOpen)))
  {
  while (false !== ($file = readdir($handle)))
    {
    if (HideIt($file) != true)
      {
      if (is_dir(str_replace("%20", " ", $DirectoryToOpen) . $file))
        {
          echo '<a href="http://localhost' . $DirectoryToOpen . $file . '">' . $file . "</a>\n";
        }
      } 
    }
  closedir($handle); 
  }
  
// LOOP THROUGH ALL FILES
if ($handle = opendir(str_replace("%20", " ", $DirectoryToOpen)))
  {
  while (false !== ($file = readdir($handle)))
    {
    // if there is an index.php file in the requested directory we stop everything and send that instead
    if (($file == "index.php") or ($file == "index.htm") or ($file == "index.html"))
      {
      echo "Location: http://localhost" . $DirectoryToOpen . $file;
      //while (@ob_end_clean());
      header("Location: http://localhost" . $DirectoryToOpen . $file);
      exit;
      }
    if (HideIt($file) != true)
      {
      if (!is_dir(str_replace("%20", " ", $DirectoryToOpen) . $file))
        {
        echo '<a href="http://localhost' . $DirectoryToOpen . $file . '">' . $file . "</a>\n";
        }
      } 
    }
  closedir($handle); 
  }
?>
</pre>
</body>
</html>
<!-- FILE ID sys.php -->
PHP.INI is as follows.

Code: Select all

error_reporting  =  E_ALL
The output from sys.php in a directory with no index page is

Code: Select all

this is a header killer
<html>
<head>
  <title>Welcome To System One</title>
</head>
<body>
<p><b>systemone/</b></p>
<pre>
<a href="http://localhost/complete">complete</a>
<a href="http://localhost/documents">documents</a>
<a href="http://localhost/Enterprise">Enterprise</a>
<a href="http://localhost/Games">Games</a>
<a href="http://localhost/manuals">manuals</a>
<a href="http://localhost/media">media</a>
<a href="http://localhost/menu">menu</a>
<a href="http://localhost/projects">projects</a>
<a href="http://localhost/Software">Software</a>
<a href="http://localhost/Virtual Cds">Virtual Cds</a>
<a href="http://localhost/Jason Webb.mov">Jason Webb.mov</a>
</pre>
</body>
</html>
<!-- FILE ID sys.php -->
In a directory with an index, I get the index file, and not even the first line of sys.php is sent. (this is a header killer)

Can someone please tell me what is going on with that? Even when I changed the apache server configuration to pull index.php as the default page, and renamed sys.php to index.php there were no errors, however, the only code that it output was as follows...

Code: Select all

&lt;a href=&quote;http://localhost/Jason Webb.mov&quote;&gt;Jason Webb.mov&lt;/a&gt;
&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!-- FILE ID index.php --&gt;
This just does not make any sense to me. HELP!

Posted: Wed May 04, 2005 10:10 pm
by timvw
You are opening a webpage... If you are lucky, (autoindexing enabled and no index available) apache (or your webserver) will generate an index for you.

as soon as there is an index file, your code will break. (but it's 5.09am and it' s a holiday and just home from a party, so it's possible i'm missing something...)

Posted: Wed May 04, 2005 10:42 pm
by Benjamin
Yeah that is not right, I wish it was it was that simple.

Posted: Thu May 05, 2005 3:40 am
by Wayne
any chance you have your output_buffering option set to ON in you php.ini file?

Posted: Thu May 05, 2005 3:43 am
by phpScott
that's dedication tim :D 8O :?

Posted: Thu May 05, 2005 1:59 pm
by Benjamin
Yeah it is set to on, didn't think about that, thank you.