Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm making a perl script and on my 1

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
I'm making a perl script and on my script there is a control panel and I'm making a part to add administrators. When I submit it completely overwrites the file instead of adding them.

Heres what I got.

Form part:


<TR bgcolor=&quot;#ffffff&quot;>
<td width=50% valign=top>
<FONT SIZE=&quot;2&quot; FACE=&quot;Verdana, Arial&quot;><B>Add Administrator</B><br></FONT>
<FONT SIZE=&quot;1&quot; FACE=&quot;Verdana, Arial&quot; COLOR=&quot;#000080&quot;>You can add more administrators that you would like to access the control panel. Simply enter a username and passwrd for them</FONT>
</td>
<td width=50% valign=top>
<FONT SIZE=&quot;2&quot; FACE=&quot;Verdana, Arial&quot;>Username:</FONT> <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Username&quot; VALUE=&quot;$vars_admin{'Username'}&quot; SIZE=40 MAXLENGTH=250>
<BR>
<FONT SIZE=&quot;2&quot; FACE=&quot;Verdana, Arial&quot;>Password:</FONT> <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Password&quot; VALUE=&quot;$vars_admin{'Password'}&quot; SIZE=40 MAXLENGTH=250>
</td></tr>


Setting variable sub:


sub set_vars_admin {

%vars_admin = %in;

# write to file!

open (FILE, &quot;>/home/puremadnezz/cgi-bin/test/variables/vars_admin.cgi&quot;) or die(&quot;Unable to open vars_admin.cgi file for writing.&quot;);
flock(FILE,LOCK_EX);
print FILE qq!\%admin_profile = (\n!;
print FILE qq!&quot;$vars_admin{'Username'}&quot; => &quot;$vars_admin{'Password'}&quot;,\n!;
print FILE qq!);\n!;
print FILE qq!1;\n!;
flock(FILE,LOCK_UN);
close (FILE);
chmod(0777, &quot;/home/puremadnezz/cgi-bin/test/variables/vars_admin.cgi&quot;);
print &quot;<font size=\&quot;-1\&quot; color=\&quot;#000000\&quot; face=\&quot;Arial, Helvetica, sans-serif\&quot;><center>Administrator(s) updated!</center></font>&quot;;

}


What the variabl file looks like:


%admin_profile = (
&quot;Username&quot; => &quot;Password&quot;,
&quot;Username&quot; => &quot;Password&quot;,
&quot;Username&quot; => &quot;Password&quot;,
);
1;


Please tell me what I am doing wrong. Thank you. ;)
 
You need to change the > in the open statement to >> so it will append new entries to the end of the file. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thank you for the reply. It kind of works.

However, when I submit and go to my FTP and view the file it looks somthing like:

%admin_profile = (
&quot;Username&quot; => &quot;Password&quot;,
&quot;Username&quot; => &quot;Password&quot;,
);
1;
&quot;Username&quot; => &quot;Password&quot;,


My setting variable part now looks like:

sub set_vars_admin {

%vars_admin = %in;

# write to file!

open (FILE, &quot;>>/home/puremadnezz/cgi-bin/test/variables/vars_admin.cgi&quot;) or die(&quot;Unable to open vars_admin.cgi file for writing.&quot;);
flock(FILE,LOCK_EX);
print FILE qq!&quot;$vars_admin{'Username'}&quot; => &quot;$vars_admin{'Password'}&quot;,\n!;
flock(FILE,LOCK_UN);
close (FILE);
chmod(0777, &quot;/home/puremadnezz/cgi-bin/test/variables/vars_admin.cgi&quot;);
print &quot;<font size=\&quot;-1\&quot; color=\&quot;#000000\&quot; face=\&quot;Arial, Helvetica, sans-serif\&quot;><center>Administrator(s) updated!</center></font>&quot;;

}


How can I make the...

);
1;


...always stay at the bottom of the file? :)
 
The only way to do what you want is to completely recreate the entire file each time. Since it appears the file is just some perl to create a hash of usernames and passwords, you can just require it in order to load the existing values. Add your new username and password to the hash. Then when you want to write it, open it with > so it will rewrite, and loop thru the hash, writing all the key and value pairs before you output the ending.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Can you tell me how I can do that? (Sorry, I'm sorta new at perl scripting. :-))
 
near the top of your script:
Code:
require &quot;/home/puremadnezz/cgi-bin/test/variables/vars_admin.cgi&quot;;
This will load the existing file and execute it, so the hash should now be defined. Use the values from your form to create a new entry in the hash. Open your file with > and print the first line or two (the constant stuff). Then to output each entry:
Code:
foreach my $username (keys %vars_admin) {
   print FILE qq!&quot;$username&quot; => &quot;$vars_admin{$username}&quot;,\n!;
Then print the closing constant stuff. Is that enough for you to figure it out?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yes.

Thank you very much for all your help. I really appeciate it.

Would you like a copy of my finished script?

BTW, I'm calling it Who Wants to be a Billionaire? :-D
 
You're quite welcome. Sure, I'd like to see the finished product. But if you have any more problems be sure to post them here so other people can benefit too.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I'm sorry to bug you again... but I cant get it to work. :-(

I'm using it like:


foreach my $vars_admin{'username'} (keys %vars_admin) {
print FILE qq!&quot;$vars_admin{'username'}&quot; => &quot;$vars_admin{$username}&quot;,\n!;


Do you think you can make a SMALL little test script on how I'm supposed to do it? :-s

Thank you again. ;-)
 
You don't need to reference $vars_admin{'username'} in the foreach statement. The &quot;(keys %vars_admin)&quot; part will give you a list of all the keys, and the &quot;foreach my $username&quot; part will loop through that list of keys, assigning each one in turn to the temporary variable $username. Then you use that variable as the key to access each of the corresponding values in your hash when you write them out. The way you have the print statement coded it will print the value twice rather than the key and the value. Take another look at the sample code I gave above:
Code:
foreach my $username (keys %vars_admin) {
   print FILE qq!&quot;$username&quot; => &quot;$vars_admin{$username}&quot;,\n!;
$username will get each of the key values of %vars_admin. Then the print statement will print that key value (the username) and the corresponding value from the hash.

I'll try to put together some code to do this and post it back here in an hour or so.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
OK, here's some code you can use. There is a small problem with your form too, you don't need a value clause on the input fields. You want them to be blank when they start.

Also, the hash you are trying to create is %admin_profile, but you're referring to %vars_admin in the place where you rewrite it. I think this is (and the value clauses above) are from some confusion about how you get the values from the form into your program. My sample uses the CGI library module, and shows how to get the values.

You can try this sample form and code out at:
It will display the form, update the file defining the hash, and display what that file looks like after the update.

Note that the code below does not check to make sure the user entered a value for both fields, not does it check to make sure the username isn't already in use. You should be able to add these. Also it doesn't sort the usernames into any particular order. This doesn't matter with a hash, but it makes it a little hard to find a particular one when you're reading the file by eye. Note also that I changed the print statement slightly, so that it puts apostrophes around the usernames and passwords, just to be clean.

Here's the sample form html:
Code:
<HTML>
<HEAD>
<TITLE>Hash Update</TITLE>
</HEAD>
<BODY>
<form name=&quot;myform&quot; method=&quot;put&quot; action=&quot;/cgi-local/hashupd.pl&quot;>
Username: <input type=&quot;text&quot; name=&quot;Username&quot;><br>
Password: <input type=&quot;text&quot; name=&quot;Password&quot;><br>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;>
</BODY>
</HTML>
Here's what the input file (testhash.pl) initally looks like (it may look different once people start trying out the url above):
Code:
%admin_profile = (
'dragon' => 'stuff',
'sam' => 'green',
'kevin' => 'who',
'tracy' => 'xyzzy',
);
1;
And finally, here's the sample program that updates the above file and displays it:
Code:
#!/usr/local/bin/perl

use CGI;
$q = new CGI;

# Print the content-type http header
print $q->header;

# Get the existing values
require &quot;testhash.pl&quot;;

# Get the parameter values from the form
$new_username = $q->param('Username');
$new_password = $q->param('Password');

# Set the new values in the hash
$admin_profile{$new_username} = $new_password;

# Open the hash file and recreate it
open (FILE, &quot;>testhash.pl&quot;);
flock(FILE, LOCK_EX);
print FILE qq!\%admin_profile = (\n!;
foreach my $username (keys %admin_profile) {
	print FILE qq!'$username' => '$admin_profile{$username}',\n!;
}
print FILE qq!);\n1;\n!;
close FILE;

# Show the resulting file
print <<END;
<html>
<head>
<title>Hash Update Output</title>
</head>
<body>
<pre>
Here's what your file now looks like:<br><br>
END

open(FILE, &quot;<testhash.pl&quot;);
@file = <FILE>;
close FILE;

print @file;

print <<END;
</pre>
</body>
</html>
END

# That's all folks!
exit 0;

1;
Have fun with it!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
OOPS, that should have been method=&quot;POST&quot; instead of method=&quot;put&quot; in the html above. It still worked that way, but only because it was defaulting to &quot;get&quot; when it didn't recognize &quot;put&quot;. Why they used &quot;get&quot; and &quot;post&quot; instead of &quot;get&quot; and &quot;put&quot; is beyone me.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Once again, thank you! :-)

Now it'll be easier to create the rest of the control panel. ;-)
 
Also.

Do I have your permission to edit your example a bit?
 
UGH! It doesn't seem to work. :-(

Please help. Ive sent you an email with my ftp server info. Thanks
 
The example is free, you can do whatever you want with it. Just don't blame me if it doesn't work afterwards :-).

My PC is dead (I'm using my boss's) so I can't get the email. Try resending the email to tsdryden@yahoo.com. I can get that thru this browser. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top