Very Strange PHP Behavior Please Help

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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Very Strange PHP Behavior Please Help

Post 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!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah that is not right, I wish it was it was that simple.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

any chance you have your output_buffering option set to ON in you php.ini file?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

that's dedication tim :D 8O :?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah it is set to on, didn't think about that, thank you.
Post Reply