Whats wrong with line 36? whats missing?

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
mtb211
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2008 11:32 am

Whats wrong with line 36? whats missing?

Post by mtb211 »

Code: Select all

 
<html>
<head>
<title>Unbenanntes Dokument</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
 
<?php 
 
$dir = $uname; 
$user = $_POST['user']; 
$pass = $_POST['pass']; 
 
// current list
$thisdir = str_replace(strrchr($_SERVER['SCRIPT_FILENAME'],'/'), '', $_SERVER['SCRIPT_FILENAME']); 
 
// What the list should provide
$oldumask = umask(0);
mkdir($dir,0777); 
 umask($oldumask);
// Daten für .htaccess erstellen 
$htaccess = 'AuthType Basic 
AuthUserFile '.$thisdir.'/'.$dir.'/.htpasswd 
AuthName "Geschuetzer Bereich" 
order deny,allow 
allow from all 
require valid-user'; 
 
// data for htpassword
$htpasswd = $uname.':'.crypt($pw, substr(md5(uniqid(rand())), 0, 2)); 
 
// Testdatei erstellen (wird angezeigt beim erfolgreichen Login) 
$handle = fopen($dir.'/index.php', 'w'); 
fwrite($handle, '
 
<? 
echo "Hier sind die Bereitgestellten Dateien<br><br>";
$action=opendir("./");
while($datei=readdir($action)){ 
if(!preg_match("!(\.|\..)$!", $datei)){ 
if ($datei!="index.php" && $datei!=".htaccess" && $datei!=".htpasswd" ) { 
echo "
<a href=\"$datei\"> 
 
$datei</a><br>"; } } } ?>'); 
fclose($handle); 
 
// .htaccess data 
$handle = fopen($dir.'/.htaccess', 'w'); 
fwrite($handle, $htaccess); 
fclose($handle); 
 
// .htpasswd data
$handle = fopen($dir.'/.htpasswd', 'w'); 
fwrite($handle, $htpasswd); 
fclose($handle); 
 
?> 
</body>
</html>
 
Thanks in advance!

ps. I didnt write this code... a contractor before I arrived did
Last edited by mtb211 on Mon Dec 15, 2008 2:40 am, edited 1 time in total.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Whats wrong with line 36? whats missing?

Post by jaoudestudios »

Some code is missing!

PHP tags are open then they are opened again.
mtb211
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2008 11:32 am

Re: Whats wrong with line 36? whats missing?

Post by mtb211 »

Im a bit new to this.. any idea what exactly missing? If I can not contact the devolper is it possible for me to figure this out?

Thanks for the quick response
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Whats wrong with line 36? whats missing?

Post by jaoudestudios »

Are you able to translate the comments into English? This might help.
mtb211
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2008 11:32 am

Re: Whats wrong with line 36? whats missing?

Post by mtb211 »

lol hey, my germany isnt great... erstellen means like to provide...
I edited the top file

probally not much help
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Whats wrong with line 36? whats missing?

Post by requinix »

Actually guys, if you looked a little harder you'd realize that there's nothing missing.

That's right. The code is valid. Watch:

Code: Select all

 
<html>
<head>
<title>Unbenanntes Dokument</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
 
<?php 
 
$dir = $uname; 
$user = $_POST['user']; 
$pass = $_POST['pass']; 
 
// current list
$thisdir = str_replace(strrchr($_SERVER['SCRIPT_FILENAME'],'/'), '', $_SERVER['SCRIPT_FILENAME']); 
 
// What the list should provide
$oldumask = umask(0);
mkdir($dir,0777); 
 umask($oldumask);
// Daten für .htaccess erstellen 
$htaccess = 'AuthType Basic 
AuthUserFile '.$thisdir.'/'.$dir.'/.htpasswd 
AuthName "Geschuetzer Bereich" 
order deny,allow 
allow from all 
require valid-user'; 
 
// data for htpassword
$htpasswd = $uname.':'.crypt($pw, substr(md5(uniqid(rand())), 0, 2)); 
 
// Testdatei erstellen (wird angezeigt beim erfolgreichen Login) 
$handle = fopen($dir.'/index.php', 'w'); 
fwrite($handle, '
 
<? 
echo "Hier sind die Bereitgestellten Dateien<br><br>";
$action=opendir("./");
while($datei=readdir($action)){ 
if(!preg_match("!(\.|\..)$!", $datei)){ 
if ($datei!="index.php" && $datei!=".htaccess" && $datei!=".htpasswd" ) { 
echo "
<a href=\"$datei\"> 
 
$datei</a><br>"; } } } ?>'); 
fclose($handle); 
 
// .htaccess data 
$handle = fopen($dir.'/.htaccess', 'w'); 
fwrite($handle, $htaccess); 
fclose($handle); 
 
// .htpasswd data
$handle = fopen($dir.'/.htpasswd', 'w'); 
fwrite($handle, $htpasswd); 
fclose($handle); 
 
?> 
</body>
</html>
 
So what's the problem?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Whats wrong with line 36? whats missing?

Post by jaoudestudios »

Actually now that it is covert into php colors tasairis is right, there is no obvious mistake. What error do you get when you run it?
cavemaneca
Forum Commoner
Posts: 59
Joined: Sat Dec 13, 2008 2:16 am

Re: Whats wrong with line 36? whats missing?

Post by cavemaneca »

First I got undefined for the $_POST variables. so I used if (isset()) for them all.
Then I got a ton of undefined variables, so I set it for the whole page with the if statement.
The I got nothing, so I created a form to send data to it.
After all this I got 0 errors.

So what I learned from this is that you need to actually create the $uname variable. You use it throughout the script, but never actually created it or assigned anything to it.
so I pretty much wrote it like this.

Code: Select all

if (!isset($_POST['user']) || !isset($_POST['pass'])) {
  die('Incorrect Submission!');
}
else {
  $uname = $_POST['user'];
  $dir = $uname;
  $user = $_POST['user'];
  $pass = $_POST['pass'];
//...
//code already there
//...
}
?>
Either that, or use something else for $uname, but it still needs to be assigned a value, and you need to check if all of the values have been assigned.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Whats wrong with line 36? whats missing?

Post by jaoudestudios »

Notices are not errors. PHP is very forgiving it will still run with notices. They only appear in development mode which is designed for development and testing stage. On production servers you will not see them. It is good practice to declare all variables - real scripting languages will not compile if everything is not declared. Actionscript 3 you even have to declare what a method is expecting and what type it will return.

PHP 6 is moving that way - slowly but surely :)
Post Reply