Avatar billede robertk Nybegynder
13. oktober 2004 - 12:09 Der er 26 kommentarer og
1 løsning

Protect af php sider, kun adgang via login

Hej,

Jeg har et lidt tricky problem, med et login script i PHP.
Det virker på følgende måde:

En index.php, med 3 logins, hvert login går til hver sin PHP side, eks. dir_index_01.php, dir_index_02.php, dir_index_03.php

Problemet er at man jo bare kan skrive stien til de 3 php sider som skulle være beskyttede..

min index.php:

<?php 
 
    $i = 0; 
    $users = array(

        //Opret brugere her
        "guest1" => array("015", "dir_index_01.php"), 
        "guest2" => array("025", "dir_index_02.php"),
        "guest3" => array("035", "dir_index_03.php")

      ); 
    if (isset($_POST['login']) && isset($_POST['psw'])) 
    { 
          $mypsw      = $_POST['psw']; 
          $mylogin = $_POST['login']; 
 
          if (isset($users[$mylogin])) 
          { 
              if ($mypsw==$users[$mylogin][0]) 
              { 
                    Header("Location: " . $users[$mylogin][1]); 
              } 
          } 
    } 
?>
<style type="text/css">
<!--
.unnamed1 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    color: #333333;
}
.form {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    color: #990000;
    border: 1px solid #999999;
}
-->
</style>
<form action="#" method="post">
  <table width="100%" height="100%"  border="0">
    <tr>
      <td><div align="center">
        <table width="300" height="240" border="0" bgcolor="#F9F9F9" class="form">
          <tr>
            <td><div align="center">
              <table width="217" border="0" align="center">
                <tr>
                  <td height="50%" colspan="2"> <span class="unnamed1"><strong>Welcome to the Member's Area - please Log In with your details below:</strong><br>
                    </span>
                     
                      <span class="unnamed1"><br>
                    </span></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Username:</span></td>
                  <td align="right"><input name="login" type="text" class="form" size="30"></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Password:</span></td>
                  <td align="right"><input name="psw" type="password" class="form" size="30"></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td align="right"><input name="submit" type="submit" class="form" value="  Login  "></td>
                </tr>
                <tr>
                  <td height="50%" colspan="2" class="unnamed1"> <br>
      If you require a password or if your password is incorrect, please e-mail <a href="mailto:rk@york-duck.dk">rk@york-duck.dk</a></td>
                </tr>
              </table>
            </div></td>
          </tr>
        </table>
      </div></td>
    </tr>
  </table>
</form>

Nogen der kan løse den ?
Avatar billede robertk Nybegynder
13. oktober 2004 - 12:12 #1
Måske man kunne bruge noget fra dte her?

<?php

// Define your username and password
$username = "test";
$password = "test";

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

?>

<h1>Login</h1>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>
    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

    <p><label for="txtpassword">Password:</label>
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

    <p><input type="submit" name="Submit" value="Login" /></p>

</form>

<?php

}
else {

?>

<p>This is the protected page. Your private content goes here.</p>

<?php

}

?>
Avatar billede sukos Juniormester
13. oktober 2004 - 14:31 #2
Brug sessions:

<?php
session_start();

    $i = 0;
    $users = array(

        //Opret brugere her
        "guest1" => array("015", "dir_index_01.php"),
        "guest2" => array("025", "dir_index_02.php"),
        "guest3" => array("035", "dir_index_03.php")

      );
    if (isset($_POST['login']) && isset($_POST['psw']))
    {
          $mypsw      = $_POST['psw'];
          $mylogin = $_POST['login'];

          if (isset($users[$mylogin]))
          {
              if ($mypsw==$users[$mylogin][0])
              {

                    $_SESSION["page"] = $users[$mylogin][1];
                    Header("Location: " . $users[$mylogin][1]);
              }
          }
    }
?>

På alle brugersider kan du så include en fil som det første, en fil som tjekker hvilken værdi session har:
<?php
session_start();

$filnavn = basename($_SERVER["PHP_SELF"]);

if(!isset($_SESSION["page"]) || $_SESSION["page"] != $filnavn) {
header("Location:login.php");
exit();
}
?>
Avatar billede sukos Juniormester
13. oktober 2004 - 14:35 #3
Eller, du kan vælge at include filen i samme document som loginform.

<?php
session_start();

    $i = 0;
    $users = array(

        //Opret brugere her
        "guest1" => array("015", "dir_index_01.php"),
        "guest2" => array("025", "dir_index_02.php"),
        "guest3" => array("035", "dir_index_03.php")

      );
    if (isset($_POST['login']) && isset($_POST['psw']))
    {
          $mypsw      = $_POST['psw'];
          $mylogin = $_POST['login'];

          if (isset($users[$mylogin]))
          {
              if ($mypsw==$users[$mylogin][0])
              {

                    $_SESSION["page"] = $users[$mylogin][1];
                    Header("Location:". $_SERVER["PHP_SELF"] ."");
              }
          }
    }


if(!isset($_SESSION["page"])) {
?>
<form action="#" method="post">
  <table width="100%" height="100%"  border="0">
    <tr>
      <td><div align="center">
        <table width="300" height="240" border="0" bgcolor="#F9F9F9" class="form">
          <tr>
            <td><div align="center">
              <table width="217" border="0" align="center">
                <tr>
                  <td height="50%" colspan="2"> <span class="unnamed1"><strong>Welcome to the Member's Area - please Log In with your details below:</strong><br>
                    </span>
                   
                      <span class="unnamed1"><br>
                    </span></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Username:</span></td>
                  <td align="right"><input name="login" type="text" class="form" size="30"></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Password:</span></td>
                  <td align="right"><input name="psw" type="password" class="form" size="30"></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td align="right"><input name="submit" type="submit" class="form" value="  Login  "></td>
                </tr>
                <tr>
                  <td height="50%" colspan="2" class="unnamed1"> <br>
      If you require a password or if your password is incorrect, please e-mail <a href="mailto:rk@york-duck.dk">rk@york-duck.dk</a></td>
                </tr>
              </table>
            </div></td>
          </tr>
        </table>
      </div></td>
    </tr>
  </table>
</form>

<?php
}else{
include($_SESSION["page"]);
}
?>
Avatar billede robertk Nybegynder
13. oktober 2004 - 16:11 #4
Hai sukos,

Tak for svaret, lige pt syntes jeg ikke jeg kan få det til at spille.. er det fordi den fil de linket til ligger i en undermappe ?
Avatar billede robertk Nybegynder
13. oktober 2004 - 16:19 #5
altså de 3 filer man nu logger ind til, alt efter login, de ligger i 3 undermapper..
Avatar billede sukos Juniormester
13. oktober 2004 - 18:26 #6
Hvis du skriver
include($_SESSION["page"]);
skal filen ligge i samme dir.

Du kan evt. også bruge
include($_SERVER["DOCUMENT_ROOT"] ."/dir/includes/". $_SESSION["page"]);

Det bedste vil være at lægge dem uden for roden af dit domæne, så man ikke kan hente dem via adresselinien i browseren.

Så skal du definere den fulde sti:

include("/var/host/dirnavn/".$_SESSION["page"]);

Stier m.m. kan du se hvis du loader flg. i din browser:

<?php
phpinfo();
?>
Avatar billede robertk Nybegynder
14. oktober 2004 - 13:29 #7
Deeeet spiller bare det skidt!!! kanon! tak for det "sukos"..
kom med et svar så får du points!
Avatar billede sukos Juniormester
14. oktober 2004 - 14:05 #8
Ok, men lige et spm.
Hvor ligger dine filer?
Kan de tilgås hvis man f.eks. skriver
ditdomain.dk/filer/dir_index_01.php
i adresselinien?
Og hvis man kan, hvad gør du så for at undgå man kan se indholdet?
Avatar billede robertk Nybegynder
14. oktober 2004 - 14:54 #9
Nope hvis man skriver stien, viser den bare ingen side! men skriver man stien på selve filerne, kan man godt hente dem, men det gør ikke SÅ meget, det er ikke TOP hemmeligt, men bare der er en smule sikkerhed, det er typisk annoncer, og hvem faaen gider hente en annonce til søndagsavisen, f.eks? LOL og anyhow skal man jo først gætte et langt filnavn..
Avatar billede sukos Juniormester
14. oktober 2004 - 15:01 #10
Ok, men du kan bruge basename($_SERVER["PHP_SELF"]) til at tjekke om det er filnavnet selv, eller filen som includer.
Er det den første, så intet output, men er det "main" fil, så er det ok.

<?php
if(basename($_SERVER["PHP_SELF"]) == "mainfil.php") {
?>

indhold af include fil

<?php
}
?>

Takker for point! :O)
Avatar billede robertk Nybegynder
14. oktober 2004 - 15:35 #11
Oki jeg sku lidt forvirret nu.. heh kan du sætte det sammen ?
Avatar billede robertk Nybegynder
14. oktober 2004 - 15:36 #12
Det hele skal ligge på www.york-duck.dk/download de 3 logins skal linkt til:

dir_index_01.php
dir_index_02.php
dir_index_03.php

Som ligger i hver deres undermappe

down1
down2
down3


<?php
session_start();

    $i = 0;
    $users = array(

        //Opret brugere her
        "ken" => array("malec", "dir_index_01.php"),
        "guest1" => array("015", "dir_index_02.php"),
        "guest2" => array("025", "dir_index_03.php")

      );
    if (isset($_POST['login']) && isset($_POST['psw']))
    {
          $mypsw      = $_POST['psw'];
          $mylogin = $_POST['login'];

          if (isset($users[$mylogin]))
          {
              if ($mypsw==$users[$mylogin][0])
              {

                    $_SESSION["page"] = $users[$mylogin][1];
                    Header("Location: " . $users[$mylogin][1]);
              }
          }
    }
?>
<style type="text/css">
<!--
.unnamed1 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    color: #333333;
}
.form {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    color: #990000;
    border: 1px solid #999999;
}
-->
</style>
<form action="#" method="post">
  <table width="100%" height="100%"  border="0">
    <tr>
      <td><div align="center">
        <table width="290" height="292" border="0" bgcolor="#F9F9F9" class="form">
          <tr>
            <td><div align="center">
              <table width="217" border="0" align="center">
                <tr>
                  <td height="50%" colspan="2"> <p align="center"><img src="logo.jpg" width="169" height="43"></p>
                    <p><span class="unnamed1"><strong><br>
                      Welcome to the Member's Area - please Log In with your details below:</strong><br>
                        </span>

                      <span class="unnamed1"><br>
                          </span></p></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Username:</span></td>
                  <td align="right"><input name="login" type="text" class="form" size="30"></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Password:</span></td>
                  <td align="right"><input name="psw" type="password" class="form" size="30"></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td align="right"><br>
                    <input name="submit" type="submit" class="form" value="  Login  "></td></tr>
                <tr>
                  <td height="50%" colspan="2" class="unnamed1"> <br>
      If you require a password or if your password is incorrect, please e-mail <a href="mailto:rk@york-duck.dk">rk@york-duck.dk</a></td>
                </tr>
              </table>
            </div></td>
          </tr>
        </table>
      </div></td>
    </tr>
  </table>
</form>
Avatar billede sukos Juniormester
14. oktober 2004 - 15:47 #13
hmm, dette:

<?php
}else{
include($_SESSION["page"]);
}
?>

byttes ud med:


<?php
}else{
$filnavn = basename($_SERVER["PHP_SELF"]);
include("check_fil.php");
}
?>


check_fil.php er flg:

<?php
$path = $_SERVER["DOCUMENT_ROOT"] ."/sti/til/includes/";
if(isset($filnavn) && basename($_SERVER["PHP_SELF"]) == $filnavn) {
    if(file_exists($path . $_SESSION["page"])) {
    include($path . $_SESSION["page"]);
    }else{
    echo"ERROR! Kontakt venligst admin!";
    }
}
?>

hmmmmmmm
Avatar billede sukos Juniormester
14. oktober 2004 - 15:48 #14
Nåhh, shit, så er det jo lidt anderledes!
Avatar billede robertk Nybegynder
14. oktober 2004 - 15:50 #15
Så skal det ikke være sådan ? :)
Avatar billede sukos Juniormester
14. oktober 2004 - 15:50 #16
På hver af de filer man sendes videre til starter du med:

<?php
session_start();

if(isset($_SESSION["page"]) && $_SESSION["page"] == basename($_SERVER["PHP_SELF"])) {
?>
indhold af fil

<?php
}
?>
Avatar billede robertk Nybegynder
14. oktober 2004 - 15:59 #17
yep done !
Avatar billede robertk Nybegynder
14. oktober 2004 - 16:03 #18
Men sal jeg stadig bruge det her i index:

include($_SERVER["DOCUMENT_ROOT"] ."/dir/includes/". $_SESSION["page"]);
Avatar billede sukos Juniormester
14. oktober 2004 - 16:16 #19
Nåh, den kode af din side, du viser tilsidst, sender du jo brugeren videre til en ny fil!
I den fil man sendes til, bruger du mit sidste forslag.
Hvis du includer en fil, på samme side som login form, så brug det første forslag. Med stien korrekt defineret selvf.! :O)
Avatar billede robertk Nybegynder
15. oktober 2004 - 09:58 #20
Ahh super.. ja for jeg linker til 3 sider i 3 mapper, det er directory Index filer (lister filerne i mappen) jeg forsøger mig lige igen :-)

Tak for hjælpen
Avatar billede robertk Nybegynder
15. oktober 2004 - 14:23 #21
Oki onece again - tager den lige for "hans" *G*

/////////////////////// Min index:

<?php
session_start();

    $i = 0;
    $users = array(

        //Opret brugere her
        "ken" => array("malec", "dir_index_01.php"),
        "guest1" => array("015", "dir_index_02.php"),
        "guest2" => array("025", "dir_index_03.php")

      );
    if (isset($_POST['login']) && isset($_POST['psw']))
    {
          $mypsw      = $_POST['psw'];
          $mylogin = $_POST['login'];

          if (isset($users[$mylogin]))
          {
              if ($mypsw==$users[$mylogin][0])
              {

                    $_SESSION["page"] = $users[$mylogin][1];
                    Header("Location: " . $users[$mylogin][1]);
              }
          }
    }
?>
<style type="text/css">
<!--
.unnamed1 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    color: #333333;
}
.form {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    color: #990000;
    border: 1px solid #999999;
}
-->
</style>
<form action="#" method="post">
  <table width="100%" height="100%"  border="0">
    <tr>
      <td><div align="center">
        <table width="290" height="292" border="0" bgcolor="#F9F9F9" class="form">
          <tr>
            <td><div align="center">
              <table width="217" border="0" align="center">
                <tr>
                  <td height="50%" colspan="2"> <p align="center"><img src="logo.jpg" width="169" height="43"></p>
                    <p><span class="unnamed1"><strong><br>
                      Welcome to the Member's Area - please Log In with your details below:</strong><br>
                        </span>

                      <span class="unnamed1"><br>
                          </span></p></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Username:</span></td>
                  <td align="right"><input name="login" type="text" class="form" size="30"></td>
                </tr>
                <tr>
                  <td><span class="unnamed1">Password:</span></td>
                  <td align="right"><input name="psw" type="password" class="form" size="30"></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td align="right"><br>
                    <input name="submit" type="submit" class="form" value="  Login  "></td></tr>
                <tr>
                  <td height="50%" colspan="2" class="unnamed1"> <br>
      If you require a password or if your password is incorrect, please e-mail <a href="mailto:rk@york-duck.dk">rk@york-duck.dk</a></td>
                </tr>
              </table>
            </div></td>
          </tr>
        </table>
      </div></td>
    </tr>
  </table>
</form>

//////////////////////////////////// Min fil jeg linket til:

<?php
session_start();

if(isset($_SESSION["page"]) && $_SESSION["page"] == basename($_SERVER["PHP_SELF"])) {
?>

<!--

DIRECTORYLISTER v0.6a (with icon support mod)

©2003-2004 Gabe Dunne
gdunne@quilime.com

You can use this without license, but I'd
love to see it in use. It has many
upgrades to be made. Download the
source here:

http://quilime.com/?p=opensourcePHP

-->
<?
/*
    VERSION HISTORY

    Updates to version 0.6
    -----------------------------------------------------------------------------------------
    Minor security fixes
    Ability to display full local file system
    Recognize files with more than one . in them. (test.test.test.test.txt, for example) (Kevin Haughton)
    Ability to remove excess path from the displayed path. e.g /var/www/html/mystuff can now be displayed as just /mystuff (Kevin Haughton)

    Added an Icon Mod available for download at: http://quilime.com/?p=opensourcePHP
    -----------------------------------------------------------------------------------------

    Updates to version 0.5
    -----------------------------------------------------------------------------------------
    Global Exceptions (Exclude any file or dir named something specific)
    Unique Exceptions (Exclude one single file/dir)
    Exception Classes (Exclude certain file formats)
    -----------------------------------------------------------------------------------------

    Updates that probably won't happen, but may be available in the future as a mod.
    -----------------------------------------------------------------------------------------
    File Uploading
    Password-protected directories
    Statistics .log file (download counts, directory views, list of IP's)
    -----------------------------------------------------------------------------------------

    © 2003, 2004 gdunne @ http://quilime.com

*/


// for adding mods, make sure the 'config' line below is at line 50.
// config ////////////////////////
                                //
                                //
                                // Define exception files and folders.
                                //
$iconpath = '_dirlistericons/';    // Path to the icon folder ('_dirlistericons/').
                                // If you do not want to use icons, you can simply just not have an icon folder
                                // or you can set this variable to NULL or '';
                                //    Examples:
                                //    "./somedir/index.php"        // unique exception (hides the single file: ./somedir/index.php )
                                //    "./some/other/folder/"         // unique exception (hides the single folder: ./some/other/folder/ )
                                //    ".htaccess"                    // global exception (hides all .htaccess's)
                                //    "somedir/"                     // global exception (hides all somedir/'s)
                                //
$exceptions = array(
"dir_index_01.php",
"dir_index_02.php",
"dir_index_03.php",
"index.php",
"logo.jpg",
".htaccess",
".htpasswd",
$iconpath,                    // hiding the icon folder
".".$_SERVER['PHP_SELF'],    // hiding this file
"/"                            // hide / for good measure
);
                                // define a specific file extension that you always want invisible
                                //
                                // Examples:
                                // "php"    // excludes all *.php files
                                // "desc"    // excludes all *.desc files
                                //
                                //
$exceptionClasses = array(
    //"php",
    //"phtml"
    );

$calcExceptions =     FALSE;         // Calculate file/folder exceptions into the total size? (FALSE is default)
$rowNumbers =         TRUE;        // Show row numbers?    (TRUE is default)

$displayFullRoot =     FALSE;        // Display full local file system path in title? (FALSE is default)
$rootdir='/var/www/html';            // use $rootdir to hide the local file path so that that displayed path is a dir name instead of ./
                                // $rootdir is ignored if $displayFullRoot is set to TRUE.
                                //
                                // Example (unix)
                                // Files are stored in /var/www/html/mystuff, this script operates from /mystuff as ./
                                // the page displays /mystuff instead of ./ when $rootdir="/var/www/html";
                                //
                                // Example (win)
                                // Files are stored in X:\Apache2\htdocs, this script operates from /htdocs as ./
                                // the page displays /htdocs instead of ./ when $rootdir="X:\Apache2";
//////////////////////////////////

// path error handling //
$root = "./"; // script root dir reference
$path = (isset($_GET['path'])) ? $_GET['path'] : $root; // get path from 'get'

// security modding
if ($path == '/') $path = $root; // hijack the / to stop real root access, need to hijack everything else somehow.
if(!is_dir(".".str_replace('.','',$path))) $path = $root; // hijack everything else that isnt below the cwd.

// if someone tries to go up a parent directory (..), refer to root instead
$dirPieces = explode("/", $path);
for ($i=0; $i<sizeof($dirPieces); $i++) { if ($dirPieces[$i] == ".." || $dirPieces[$i] == "...") { $path = $root; break; } }

if (substr($path, -1) != "/") $path .= "/"; // if path does not have a trailing slash, add one
$path = preg_replace("/\/\//", "/", $path); // if path has two slashes in succession, eliminate one


// directory recursion //

// Open the folder

$dir_handle = @opendir($path);
if(!$dir_handle) { $dir_handle = @opendir($root); $path=$root; } // if folder isn't there, go back to root

// get file extension
function GetExt($ext) {
    $ext = strtolower($ext);
    switch($ext) {
    case "avi";  $type = "Video File"; break;
    case "bat";  $type = "Batch File"; break;
    case "css";  $type = "Cascading Style Sheet"; break;
    case "exe";  $type = "Executable"; break;
    case "fla";  $type = "Flash File"; break;
    case "swf";  $type = "SWF Flash File"; break;
    case "gif";  $type = "GIF Image"; break;
    case "dir";  $type = "Director File"; break;
    case "html"; $type = "HTML File"; break;
    case "htm";  $type = "HTM File"; break;
    case "inc";    $type = "Include File"; break;
    case "jpg";  $type = "JPEG Image"; break;
    case "mov";  $type = "Quicktime File"; break;
    case "mp3";  $type = "MP3 File"; break;
    case "mp4";  $type = "MPEG 4"; break;
    case "msg";  $type = "Email Message"; break;
    case "pdf";  $type = "PDF Acrobat File"; break;
    case "psd";  $type = "Photoshop File"; break;
    case "php";  $type = "PHP File"; break;
    case "ppt";  $type = "PowerPoint Presentation"; break;
    case "txt";  $type = "Text Document"; break;
    case "wma";  $type = "Windows Media Audio"; break;
    case "xls";  $type = "Excel File"; break;
    case "zip";  $type = "Zip File"; break;
    case "ttf";  $type = "True Type Font"; break;
    case "sql";  $type = "SQL File"; break;
    case "rpm";  $type = "RedHat Package Manager File"; break;
    case "doc";  $type = "Word Document"; break;
    case "png";  $type = "PNG Image"; break;
    case "jpeg"; $type = "JPEG Image"; break;
    case "psp";  $type = "Paint Shop Pro Image"; break;
    case "obd";  $type = "Microsoft Binder"; break;
    case "nrg";  $type = "Nero Project"; break;
    case "iso";  $type = "ISO Image File"; break;
    case "bmp";  $type = "Windows Bit Map"; break;
    case "ini";  $type = "INI File"; break;
    case "inf";  $type = "INF File"; break;
    case "hlp";  $type = "Help File"; break;
    case "reg";  $type = "Registry File"; break;
    case "log";  $type = "Log File"; break;
    case "chm";  $type = "Compiled html"; break;
    default: $type = "Unknown Type";
    }
    return $type;
}

// format size
function formatSize($size) {
    $kb = 1024;        // Kilobyte
    $mb = 1024 * $kb;  // Megabyte
    $gb = 1024 * $mb;  // Gigabyte
    $tb = 1024 * $gb;  // Terabyte
    if($size==0) {             return "(empty)"; }
    if($size < $kb) {         return $size." Bytes"; }
    else if($size < $mb) {     return round($size/$kb,2)." kb"; }
    else if($size < $gb) {     return round($size/$mb,2)." mb"; }
    else if($size < $tb) {     return round($size/$gb,2)." gb"; }
    else {                     return round($size/$tb,2)." tb"; }
}

// get amount of files, dirs, and formatted size
function getSize($dir) {
    global $exceptions, $exceptionClasses, $calcExceptions;

    $files = 0;
    $dirs = 0;
    $memory = 0;
    $hidden = 0;
    if ($dir_handle = @opendir($dir)) {
      while ($file = readdir($dir_handle)) {
          if($file != "." && $file != "..") {
            $dir .= "/";
            $dir = preg_replace("/\/\//", "/", $dir); // error check: makes sure there are no double slashes
            if (is_dir($dir.$file)) { $dir.$file .= "/"; } // add a slash to directories



              if(@is_dir($dir.$file)) {
                  if (!$calcExceptions) { if (in_array($dir, $exceptions) || in_array($file, $exceptions) || in_array($dir.$file, $exceptions)) continue; } // skip if exception
                  $sizeBuild = getSize($dir.$file);
                  $files +=  $sizeBuild[0];
                  $dirs +=  $sizeBuild[1];
                  $memory +=  $sizeBuild[2];
                  $dirs++;
              } else {

                    if (!$calcExceptions) { $fileExt = ereg_replace("^.+\\.([^.]+)$", "\\1", $file); if (in_array($file, $exceptions)  || in_array($fileExt, $exceptionClasses) || in_array($dir.$file, $exceptions)) continue; } // skip if exception

                  $memory += @filesize($dir.$file);
                  $files++;
              }
          }
      }
    closedir($dir_handle);
    }
    $size[0] = $files;
    $size[1] = $dirs;
    $size[2] = $memory;
    $size[3] = $hidden;
    return $size;
}


$i=0; // var used to increment $files array
$j=0; // var used to increment $dirs array
while ($file = readdir($dir_handle)) { // Loop through everything in the selected $path
    if($file != "." && $file != "..") {

    if (is_dir($path.$file)) { $path.$file .= "/"; } // add a slash to directories

    $fileExt = ereg_replace("^.+\\.([^.]+)$", "\\1", $file);

        if(in_array($fileExt, $exceptionClasses) || in_array($path.$file, $exceptions) || in_array($file, $exceptions) || in_array($path, $exceptions)) continue;

            if (is_dir($path.$file)) { // if file is a directory, add to the $dirs array, increment the counter, and continue
                $file = preg_replace("/\//", "", $file);
                $dirs[$j] = $file;
                $j++;
                continue;
            }

            $files[$i] = $file; // if file, add to the $files array and increment the counter
            $i++;
        }
}

closedir($dir_handle); // close directory

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<? echo $_SERVER['PHP_SELF']; ?>

<html>
<head>

<style type="text/css" media="screen" title="default">

/* text styling */
* { font-family: Arial, Helvetica, sans-serif; font-size: 11px; }

body { margin: 30px; }

/* container */
#main { margin: 20px 0px 20px 0px; }

h1 { font-family: Georgia, "Times New Roman", Times, serif; font-size: 18px; letter-spacing: 3px;
font-style:italic; margin: 0px 0px 20px 0px; display:inline; font-weight:normal; }

h2 { font-weight:normal; font-size: 11px; margin-left: 15px; display:inline; }

/* fields */
.filenumber { width:10px; padding: 0px 10px 0px 10px; border-left:1px solid #ddd; color:#777; text-align:center; }
.filename { }
.filesize { text-align:right; padding:0px 20px 0px 10px; }
.filetype { width:160px; }
.modified { width:100px; border-right:1px solid #ddd; padding: 0px 10px 0px 0px; }

/* file links */
a { color: #ee0000; text-decoration: none; }
a:hover { color: #440000; text-decoration: underline; }

/* folder link */
.folder a { font-weight:bold; }

/* parent folder link*/
.parentFolderRow a { color:#000; }

/* file table */
table { width:100%; }

/* rows */
.border { border-top: 1px solid #ddd; }
.folderRow { background-color:#eeeeee; }
.rowColor1 { background-color: #f9f9f9; }
.rowColor2 { }

/* row hover effect */
.rowHover { background-color: #e6e6e6; cursor: pointer; }

.monospace { font-family: "Lucida Console", Courier, Mono; font-size: 10px; }

</style>

</head>
<body>



<div id="main">
<?  if ($displayFullRoot) { $displaypath=getcwd(); } // if displayFullRoot is true, display... the full root
    else {
        if (realpath($rootdir))  { // if $rootdir exists, remove it from the full local path and just show the current dir
            if("$path" == './') $displaypath = str_replace($rootdir,'',getcwd()); // if we're in the root, just show the root dir
            else $displaypath=str_replace($rootdir,'',realpath($path)); // else show the subfolders
        } else {
            $displaypath = $path; // if $rootdir doesn't exist, show the literal path variable instead of anything fancy
        }
    }
?>

    <h1><?=$displaypath?></h1>
    <? $total = getSize($path); // total size of stuff ?><h2>(<?=$total[1]?> folders, <?=$total[0]?> files)</h2>

    <table border="0" cellspacing="0" cellpadding="0" width="100%">

    <!--time right now-->
    <tr>
        <!--empty columns-->
        <td colspan="4">&nbsp;</td>
        <!--current date/time-->
        <td style="color:red;height:20px;border-right:0px;" class="modified" title="Current Date, Time"><?=date("m/d/y, h:ia");?></td>
    </tr>

    <?
    //if in subdirectory, print a button row to the parent directory
    if ($path != $root) {
    ?>

    <tr class="folderRow parentFolderRow" onmouseover="className='parentFolderRow folderRow rowHover'" onmouseout="className='folderRow parentFolderRow'" onclick="java script:window.location='?path=<?=dirname($path)?>/'" title="Parent Folder">
        <!--file number-->
        <td class="filenumber monospace border">&laquo;</td>
        <!-- file name -->
        <td class="folder border"><a href="?path=<?=dirname($path)?>/">Parent Folder</a></td>
        <!-- file size -->
        <td class="filesize border">&nbsp;</td>
        <!-- file type -->
        <td class="filetype border"><?=(isset($iconpath)?"<img src=\"".$iconpath."dir.gif\">&nbsp;":"")?>&nbsp;</td>
        <!-- file last modified -->
        <td class="modified border">&nbsp;</td>
    </tr>

    <?

    } //end if

    if (isset($dirs)) {

        sort($dirs);

        // echo directories
        for ($i=0; $i < sizeof($dirs); $i++) {

        // total size of stuff
        $total = getSize($path.$dirs[$i]);

        ?>

        <tr class="folderRow" onmouseover="className='row rowHover'" onmouseout="className='folderRow'" onclick="java script:window.location='?path=<?=$path.$dirs[$i]?>/'" title="Folder: <?=$dirs[$i]?>, <?=formatSize($total[2]);?>">
            <!--file number-->
            <td class="filenumber monospace border">&raquo;</td>
            <!-- file name -->
            <td class="folder border"><a href="?path=<?=$path.$dirs[$i]?>/"><?=$dirs[$i]?></a> (<?=$total[1]?> folders, <?=$total[0]?> files)</td>
            <!-- file size -->
            <td class="filesize border"><? echo formatSize($total[2]); ?></td>
            <!-- file type -->
            <td class="filetype border"><?=(isset($iconpath)?"<img src=\"".$iconpath."dir.gif\">&nbsp;":"")?><strong>File Folder</strong></td>
            <!-- file last modified -->
            <td class="modified border"><?=date("m/d/y, h:ia", filemtime($path.$dirs[$i]))?></td>
        </tr>

        <?

        } // end for
    } // end if

    if (isset($files)) {

        sort($files);

        // echo files
        for ($i=0; $i < sizeof($files); $i++){

        // alternate color for table each time through the loop
            if ($i%2) $alt = "1";
            else $alt = "2";
        ?>

        <tr class="row rowColor<?=$alt?>" onmouseover="className='row rowHover'" onmouseout="className='row rowColor<?=$alt?>'" onclick="java script:window.location='<?=$path.$files[$i]?>'" title="File: <?=$files[$i]?>, <?=number_format((filesize(trim($path.$files[$i]))/1024), 2)?> kb">
            <!--file number-->
            <td class="filenumber monospace border"><span <?=($rowNumbers?"":"style=\"visibility:hidden;\"")?>><? if (($i+1) < 10) { echo "0"; } echo $i+1; ?></span></td>
            <!-- file name -->
            <td class="filename border"><a href="<?=$path.$files[$i]?>"><?=$files[$i]?></a></td>
            <!-- file size -->
            <td class="filesize border"><? echo formatSize(filesize(trim($path.$files[$i]))); ?></td>
            <!-- file type -->
            <td class="filetype border"><?  $fileExt = explode('.', $files[$i]); if(isset($iconpath)) { if(is_file("$iconpath".strtolower(trim(end($fileExt))).".gif")) { echo "<img src=\"".$iconpath.strtolower(trim(end($fileExt))).".gif\"> "; } else { echo "<img src=\"".$iconpath."unknown.gif\"> "; } } echo GetExt(end($fileExt)); ?></td>
            <!-- file last modified -->
            <td class="modified border"><?=date("m/d/y, h:ia", filemtime($path.$files[$i]))?></td>
        </tr>

        <?
        } // end for loop
    // end if
    }

    // if there are files or directories, put out a total. If not, then put an empty table
    if (isset($files) || isset($dirs)) {
    ?>

    <!--total-->
    <tr>
        <!--empty columns-->
        <td class="border" colspan="2">&nbsp;</td>
        <!--total size-->
        <td style="color:red;height:20px;border-right:0px;" title="Total" class="filesize border"><? $total = getSize($path); if (formatSize($total[2]) != "(empty)") {echo formatSize($total[2]);} //echo formatSize($totalSize); ?></td>
        <!--empty column-->
        <td class="border" colspan="2">&nbsp;</td>
    </tr>

    <?
    } else {
    ?>

    <!--bottom border-->
    <tr>
        <td class="border" colspan="5">&nbsp;</td>
    </tr>

    <?
    } // end else
    ?>
    </table>

</div><!-- // end #main -->

</body>
</html>

<?php
}
?>

Hvad er det jeg mangler ?
Avatar billede sukos Juniormester
15. oktober 2004 - 14:37 #22
?
I netscape kan jeg komme på:
http://york-duck.dk/york-duck/download/dir_index_02.php
men ikke med IE ? Der må være noget mere kode end det du viser?
Avatar billede sukos Juniormester
15. oktober 2004 - 14:38 #23
Logger jeg ind med IE, så kan den godt vises
Avatar billede robertk Nybegynder
15. oktober 2004 - 17:15 #24
nej det er det er det hele ?
Avatar billede robertk Nybegynder
15. oktober 2004 - 17:15 #25
Hvis jeg trykke rpå linket kommer jeg ikke ind.. ?
Avatar billede sukos Juniormester
15. oktober 2004 - 22:19 #26
Hvad står der i din .htaccess?
Avatar billede robertk Nybegynder
18. oktober 2004 - 12:56 #27
.htaccess - jeg har ikke lavet nogen .htaccess?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester