Jump to content

Stopping direct script access...


Guest Xanza

Recommended Posts

Guest Xanza

I decided to use cookies, cause well... I've never worked with them before - but of course I'm getting some errors.

 

 

index.php

<?php

$url = $_GET['url'];
if($url == "files"){
   include('files.php');
}

$Password = 'passwd';

if(isset($_POST['submit_pwd'])){
      $pass = isset($_POST['passwd']) ? $_POST['passwd'] : '';
        setcookie('logged_in', '1');
      if($pass != $Password) {
         showForm("Wrong password");
         exit();
      }
   } else {
      showForm();
      exit();
   }
function showForm($error = "LOGIN"){

?>

<html>
<head>
   <title>I don't think so!</title>
</head>
<body>
    <div id="main">
      <div class="caption"><?php echo $error; ?></div>
      <form action="index.php?url=files" method="post" name="pwd">
        Password:
        <table>
          <tr><td><input class="text" name="passwd" type="password"/></td></tr>
          <tr><td align="center"><br/>
             <input class="text" type="submit" name="submit_pwd" value="Login"/>
          </td></tr>
        </table>
      </form>
   </div>
</body>
<?php
}
?>

 

 

files.php

<?php

if(!$_COOKIE['logged_in'] == '1'){
    echo "Try logging in first.";
} else {
if($handle = opendir('.')){
   while(false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".."){
          	$thelist .= '<a href="'.$file.'">'.$file.'</a><br/ >';
          }
       }
  closedir($handle);
  }

echo <<<HTML
<center>
<P>$thelist</p>
</center>
HTML;
}

?>

 

 

 

When I test the script, and login with the correct password, I get this error:

 

Try logging in first. ;)

Warning: Cannot modify header information - headers already sent by (output started at /www/domain.com/files/files.php:4) in /www/domain.com/files/index.php on line 12

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.