Jump to content


Photo

help with function.


  • Please log in to reply
4 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 August 2006 - 12:17 AM

I already have a script that does what I want but I now want to clean it up abit. So I thought I would write some functions and call them in my script. Well write now I am just testing it. The problem that I am having is that the uername is not being passed to the function.


Here is what I am testing so far.
<?php
$username = "test";
function newUser() {
   //creates a directory for masters.conf and zone files
   mkdir('C:/windows/system32/dns/etc/users/' . $username . '');
   if (!mkdir) {
       echo "unable to create the needed directory";
   }
   //creates a masters.conf in the new directory
   $filename = 'C:/windows/system32/dns/etc/users/' . $username . '/masters.conf';
   $handle = fopen($filename, 'x+'); 
   $content = "#Begin Masters";
   fwrite($handle, $content);
   fclose($handle);
   
   //adds a new line in the main masters.conf
   $file = 'C:/windows/system32/dns/etc/masters.conf';
   $fp = fopen($file, "r+b");
   $layout = 'include "' . $username . '/masters.conf";';
   
   while (!feof($fp)) {
      $line = trim(fgets($fp, 1024));
	  if ($line == '#Begin Masters') {
	      $fpos = ftell($fp);
		  $rest = fread($fp, filesize($file));
		  
		  fseek($fp, $fpos, SEEK_SET);
		  fwrite($fp, "\n\n");
		  fwrite($fp, $layout);
		  fwrite($fp, $rest);
	 }
  }
  fclose($fp);	 	  
}
newuser();
if(!newuser()) {
   echo "The function did not work";
}else{
   echo "The directory $username was created sucessfuly";
}         
?>

So basicly I want to pass the usersname to the function.

Any suggestions as to how to do this or a better way would be great.

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 05 August 2006 - 01:23 AM

Change
function newUser() {
To
function newUser($username) {


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 August 2006 - 01:31 AM

I tried this

<?php
$username = "test";
function newUser() {
   //creates a directory for masters.conf and zone files
   mkdir('C:/windows/system32/dns/etc/users/' . $GLOBALS['username'] . '');
   if (!mkdir) {
       echo "unable to create the needed directory";
   }
   //creates a masters.conf in the new directory
   $filename = 'C:/windows/system32/dns/etc/users/' . $GLOBALS['username'] . '/masters.conf';
   $handle = fopen($filename, 'x+'); 
   $content = "#Begin Masters";
   fwrite($handle, $content);
   fclose($handle);
   
   //adds a new line in the main masters.conf
   $file = 'C:/windows/system32/dns/etc/masters.conf';
   $fp = fopen($file, "r+b");
   $layout = 'include "' . $GLOBALS['username'] . '/masters.conf";';
   
   while (!feof($fp)) {
      $line = trim(fgets($fp, 1024));
	  if ($line == '#Begin Masters') {
	      $fpos = ftell($fp);
		  $rest = fread($fp, filesize($file));
		  
		  fseek($fp, $fpos, SEEK_SET);
		  fwrite($fp, "\n\n");
		  fwrite($fp, $layout);
		  fwrite($fp, $rest);
	 }
  }
  fclose($fp);	 	  
}
newuser();
if(!newuser()) {
   echo "The function did not work";
}else{
   echo "The directory $username was created sucessfuly";
}         
?>

It creates the directory just fine but it repeats the
$layout = 'include "' . $GLOBALS['username'] . '/masters.conf";';
twice on the same line like this.

#Begin Masters
include "test/masters.conf";

include "test/masters.conf";

when I try to run this it creates everything that it is supposed to but it still returns the following errors.

Warning: mkdir() [function.mkdir]: File exists in function_test.php on line 5

Warning: fopen(C:/windows/system32/dns/etc/users/test/masters.conf) [function.fopen]: failed to open stream: File exists in function_test.php on line 11

Warning: fwrite(): supplied argument is not a valid stream resource in function_test.php on line 13

Warning: fclose(): supplied argument is not a valid stream resource in function_test.php on line 14
The function did not work

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 05 August 2006 - 01:45 AM

well, why not make $username global?

$username = "test";
function newUser()
{
global $username;
...
}

#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 August 2006 - 01:55 AM

I got the globals issue figured out but now I have another issue.

I changed

newuser();
if(!newuser()) {
   echo "The function did not work";
}else{
   echo "The directory $username was created sucessfuly";
}


To


$test = newUser();
if(!$test) {
   echo "The function did not work";
}else{
   echo "The directory $username was created sucessfuly";
}

I did this because I realized that the first one was calling the function twice. I no longer get the all the error messages but it still echos The function did not work. Everything works as it should but I still get that message. I guess that I could just omit this part of the script. I would rather know why it is doing this?

Any suggestions would be great.

Thanks for the help,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users