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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI BEGINNER< MODIFY SCRIPT

Status
Not open for further replies.

scorpion975

IS-IT--Management
Jul 26, 2004
10
US
How can i modify this script so that each time a user logs in it will display a diff, page it is set up now, to view main.txt after logging in, all members are stored in members.txt folder.
Any thoughts:

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

require "cookie.lib";

&GetCookies('Custom-CGI_pass','Custom-CGI_id');
# check to see if cookies are present
if ($Cookies{'Custom-CGI_pass'} eq "" || $Cookies{'Custom-CGI_id'} eq "") {
print "Location: login.cgi\n\n";
exit;
}
&get_input;
# this is the menu
if ($in{'action'} eq "") {
&mainpage;
exit;
}
if ($in{'action'} eq "logout") {
&loggout;
}
else {
&mainpage;
exit;
}

##############
# End Menu selection!
##############
# if everything is a ok, then print logged in page
sub mainpage {
open (FILE,"main.txt");
$mainpage = '';
while (<FILE>)
{
$mainpage .= $_;
}
close (FILE);
print "Content-type: text/html\n\n";
print "<!-- Thank you for signing up for Course. -->\n";
print "<!-- We hope you come back and take more courses online. -->\n";
print $mainpage;
}

####### this is the log out section

sub loggout {

# this is a simple operation, to delete cookies (ie. loggout member)
# all you hav to do is set the cookies to a bull ("") value.
&SetCookies('Custom-CGI_id',"");
&SetCookies('Custom-CGI_pass',"");
&get_template;
print "Content-type: text/html\n\n";
print $header;
print "<body bgcolor=\"#E2DEB8\">\n";
print "<h1>Logged Out</h1>\n";
print "You have been successfully logged out, you can log back in by clicking \n";
print "<a href=\"login.cgi\">here</a>.\n";
print $footer;
exit;
}
 
scorpion975,

Login.cgi is teh script to modify, also the members.txt file to show what page to display

Code:
scorpion975|password|main.txt
PaulTEG|password|paul.txt
ChrisHunt|password|Chris.htm
when checking for the password, also take up the directed page
Code:
($user,$password,$location)=split /\|/, $_;
 if ($user eq $screenuser) && ($password eq $screenpass) {
    $redirect_page=$location;
 }

HTH
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
My admin page that sets up the users, i fill it in, would i have to change that page too:

Next question my members.txt is derived from my admin page, what additional coding do i have to do, and in addition where do i add this into it:
($user,$password,$location)=split /\|/, $_;
if ($user eq $screenuser) && ($password eq $screenpass) {
$redirect_page=$location;
}

ADMIN CODING
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
require '../cookie.lib';
require '../common.cgi';
&get_input;
&GetCookies('admin_pass','admin_id');
if ($Cookies{'admin_pass'} eq "" || $Cookies{'admin_pass'} eq "") {

if ($in{'action'} eq '') {
&login;
} elsif ($in{'action'} eq 'login') {
&check_pass;
print "Location: admin.cgi?cookies=turned_off\n\n";
exit;
}

} else {

if ($in{'cookies'} eq "turned_off") {
print "Location: admin.cgi\n\n";
exit;
}
if ($in{'action'} eq "") {
&editmembers;
exit;
}
if ($in{'action'} eq "delete_mem") {
&delete_mem;
}
if ($in{'action'} eq "delete_user_confirm") {
&delete_user_confirm;
}
if ($in{'action'} eq "mail_selected_display") {
&mail_selected_display;
}
if ($in{'action'} eq "mass_mail1") {
&mail_selected_display;
}
if ($in{'action'} eq "send_mail") {
if ($demo ne "on") {
&send_mail;
&mail_sent;
exit;
} else {
&mail_sent;
exit;
}
}
}
exit;

######## The Subroutines ########


sub login {
print "Content-type: text/html\n\n";
print $header;
print "<body bgcolor=\"#E2DEB8\">\n";
print "<h1><TITLE>TRAINING CENTER</TITLE><body bgcolor=E2DEB8>Please Login</h1>\n";
print "Please login with your username and password.<P>\n";
# this checks if cookies have been set.
if ($in{'cookies'} eq "turned_off") {
print "<b><font color=\"#FF0000\">Error:</font></b> Cookies seem to be disabled. You cannot login until, \n";
print "these are turned on.\n";
}
print "<form action=\"$ENV{'SCRIPT_NAME'}\" method=\"POST\">\n";
print " <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#000000\" width=\"400\"> \n";
print " <tr> \n";
print " <td> \n";
print " <table cellpadding=8 border=0 width=\"100%\">\n";
print " <tr><td bgcolor=\"#E2DEB8\">\n";
print " <table cellpadding=8 border=0 width=\"100%\">\n";
print " <tr><td >\n";
print " User Name: \n";
print " </td><td>\n";
print " <input type=text name=username size=28 value=\"";
if ($demo eq "on") {
print "demo";
}
print "\">\n";
print " </td></tr>\n";
print " <tr bgcolor=\"#E2DEB8\"><td>\n";
print " Password:\n";
print " </td><td>\n";
print " <input type=password name=password size=28 value=\"";
if ($demo eq "on") {
print "demo";
}
print "\">\n";
print " </td></tr>\n";
print " </table>\n";
print " </td></tr>\n";
print " <tr><td bgcolor=\"#E2DEB8\">\n";
print " <input type=hidden name=action value=login>\n";
print " <input type=submit value=\"Submit Login\">\n";
print " <input type=\"reset\" value=\"Reset\">\n";
print " </td></tr>\n";
print " </table>\n";
print " </td></tr>\n";
print " </table>\n";
print ' </form>';

print $footer;
}


sub editmembers {

print "Content-type: text/html\n\n";
print $header;
print "<body bgcolor=\"#E2DEB8\">\n";
print "<h1>Admin Menu</h1><P>\n";
print "This is the admin area for Cookie Login System. You email selected, all or \n";
print "an individual member.<P><center><form action=\"$ENV{'SCRIPT_NAME'}\" method =\"POST\">\n";
print "<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#000000\">\n";
print " <tr>\n";
print " <td>\n";
print " <table width=\"400\" border=\"0\" cellspacing=\"1\" cellpadding=\"6\">\n";
print " <tr bgcolor=\"#E2DEB8\"> \n";
print " <td>Member Name</td>\n";
print " <td> \n";
print " <div align=\"center\">Email</div>\n";
print " </td>\n";
print " </tr>\n";

open(CF, "$setup_base_dir/members.txt");
# get all th info in the file
@all = <CF>;
close(CF);
@all = sort(@all);
# loop for number of elements
for($i = 0; $i <= @all; $i++){
# split (explode) each line
($mem_name,$mem_password,$mem__encrypted_password,$mem_email) = split(':',$all[$i]);
if ($mem_name ne "") {
# check to see if submited info matches existing info
$mem_email =~ s/\n//g;
print " <tr><td bgcolor=\"#E2DEB8\"> \n";
print " <b>$mem_name</b>\n";
print " </td><td bgcolor=\"#E2DEB8\">\n";
print " <input type=\"checkbox\" name=\"email_$i\" value=\"$mem_email\">\n";
print " </td></tr>\n";
$total++;
}
}

if ($total eq "") {
print " <tr bgcolor=\"#E2DEB8\"><td colspan=\"2\" align=\"center\"><b>No Members</b></td></tr>\n";
}
print " <tr bgcolor=\"#E2DEB8\"> \n";
print " <td colspan=\"2\">\n";
print " <input type=\"hidden\" name=\"total\" value=\"$total\">\n";
print " <input type=\"hidden\" name=\"action\" value=\"mail_selected_display\">\n";
print " <input type=submit value=\"Mail Selected\"></td>\n";
print " </tr>\n";
print " </table>\n";
print " </td>\n";
print " </tr>\n";
print "</table></form>\n";
print "<p>\&nbsp;</p>\n";
print "<table width=\"450\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#E2DEB8\">\n";
print " <tr> \n";
print " <td colspan=\"3\">\n";
print " <table width=\"450\" border=\"0\" cellspacing=\"1\" cellpadding=\"6\">\n";
print " <tr bgcolor=\"#E2DEB8\" valign=\"bottom\"> \n";
print " <td> \n";
print " <form action=\"$ENV{'SCRIPT_NAME'}\" method =\"POST\">\n";
print " <select name=\"member\">\n";

open(CF, "$setup_base_dir/members.txt");
# get all th info in the file
@all = <CF>;
close(CF);
# sort the array
@all = sort(@all);
# now store sorted array
open (SIGNUP, ">$setup_base_dir/members.txt");
# loop for number of elements
for($i = 0; $i <= @all; $i++){
# split (explode) each line
($mem_name,$mem_password,$mem__encrypted_password,$mem_email) = split(':',$all[$i]);
if ($mem_name ne "") {
# check to see if submited info matches existing info
$mem_email =~ s/\n//g;
print " <option value=\"$mem_name\">$mem_name</option>\n";
$total++;
# update member file with sorted array
print SIGNUP ("@all[$i]");
}
}
# close member file
close (SIGNUP);
if ($total == "") {
print " <option value=\"\">\&gt; No Mumbers</option>\n";
}

print " </select>\n";
print " <input type=\"hidden\" name=\"action\" value=\"delete_mem\">\n";
print " <input type=submit value=\"Delete Member\">\n";
print " </form>\n";
print " </td>\n";
print " <td> \n";
print " <form action=\"$ENV{'SCRIPT_NAME'}\" method =\"POST\">\n";
print " <input type=\"hidden\" name=\"action\" value=\"mass_mail1\">\n";
print " <input type=\"submit\" value=\"Email all members\">\n";
print " </form>\n";
print " </td>\n";
print " </tr>\n";
print " </table>\n";
print " </td>\n";
print " </tr>\n";
print "</table></center>\n";
@name_array = sort(@name_array);
for($i = 0; $i <= $#name_array; $i++){
print "My name is: <b>@name_array[$i]</b><br>\n";
}
print $footer;
exit;
}


sub check_pass {

if ($in{'username'} ne "$admin_login" || $in{'password'} ne "$admin_password") {
&login_fail;
} else {
$crypted = crypt("$in{'password'}",'£mHgF*&');
$crypted =~ s/[^a-z0-9\_]//g;
&SetCookies('admin_id',$in{'username'});
&SetCookies('admin_pass',$crypted);
}
}


sub login_fail {
print "Content-type: text/html\n\n";
print $header;
print "<body bgcolor=\"#E2DEB8\">\n";
print "<h2>Error</h2>\n";
print "<P>Sorry that was the wrong login.\n";
print $footer;
exit;

}


sub delete_mem {

print "Content-type: text/html\n\n";
print $header;
print "<body bgcolor=\"#E2DEB8\">\n";
print "<h1>Delete member</h1>\n";
print "Once you delete a user, there is <b>no</b> turning back. Please be sure \n";
print "you're deleting the right user before you hit that delete button!<P>\n";
print " <FORM ACTION=\"$ENV{'SCRIPT_NAME'}\" METHOD=\"POST\">\n";
print " <input type=hidden name=action value=delete_user_confirm>\n";
print "<P> Delete member, <b>$in{'member'}</b><P>\n";
print "<input type=checkbox name=member value=$in{'member'}>\n";
print "Yes Delete member <b>$in{'member'}</b>.";
print " <input type=submit value=\"Confirm Delete\"></form>\n";
print "<P><i>You must check the box to delete the member</i>.\n";
print $footer;

}

sub delete_user_confirm {

if ($in{'member'} eq "") {
print "Content-type: text/html\n\n";
print $header;
print "<body bgcolor=\"#E2DEB8\">\n";
print "<h1>Error</h1>\n";
print "You must check the checkbox to confirm deleting the member.\n";
print $footer;
exit;
}

open(CF, "$setup_base_dir/members.txt");
# get all th info in the file
@all = <CF>;
close(CF);
# loop for number of elements

open (SIGNUP, ">$setup_base_dir/members.txt");
for($i = 0; $i <= @all; $i++){
# split (explode) each line
($mem_name,$mem_password,$mem__encrypted_password,$mem_email) = split(':',$all[$i]);
if ($mem_name ne "$in{'member'}" && $all[$i] ne "") {
print SIGNUP ("$all[$i]");
}
}
close (SIGNUP);

print "Content-type: text/html\n\n";
print $header;
print "<h1>Member Deleted</h1>\n";
print "Member has been successfully deleted. Press 'Retun' to go back to the main menu.\n";
print "<FORM ACTION=\"$ENV{'SCRIPT_NAME'}\" METHOD=\"POST\">\n";
print "<input type=submit value=\" Return \">\n";
print "</form>";
print $footer;
}



sub mail_selected_display {

print "Content-type: text/html\n\n";
print $header;
#print "You are send an email to <b>$in{'total'}</b> members<P>\n";

print "<FORM ACTION=\"$ENV{'SCRIPT_NAME'}\" METHOD=\"POST\">\n";
if ($in{'action'} eq "mass_mail1") {
print "<input type=\"hidden\" name=\"email\" value=\"all\">\n";
print "<h1>Email All Members</h1>\n";
} else {
print "<h1>Email Selected</h1>\n";
}

$count = $in{'total'};
$v = 0;
for ($i=0;$i<=$count;$i++)
{
if ($in{"email_$i"} ne "") {
print "<input type=\"hidden\" name=\"email_$i\" value=\"$in{\"email_$i\"}\">\n";
$v++;
}
}
print "From: <br><input type=text name=from size=45 value=\"$setup_email_admin\"><P>\n";
print "Subject: <BR><input type=text name=subject size=45>\n";
print "<BR><BR>Body of Message<BR>\n";
print "<TEXTAREA NAME=body ROWS=14 COLS=50></TEXTAREA>\n";
print "<input type=hidden name=action value=\"send_mail\"><br>\n";
print "<input type=hidden name=total value=\"$v\">\n";
print "<input type=submit value=\"Send this message\">\n";
print "</form>";
print $footer;
}

sub send_mail {
if ($in{'email'} eq "all") {

open(CF, "$setup_base_dir/members.txt");
# get all th info in the file
@all = <CF>;
close(CF);
# loop for number of elements
open (SIGNUP, "$setup_base_dir/members.txt") or die "Unable to open $setup_base_dir"."members.txt";
for($i = 0; $i <= $#all; $i++){
# split (explode) each line
($mem_name,$mem_password,$mem__encrypted_password,$mem_email) = split(':',$all[$i]);

open(MAIL, "|$setup_sendmail -t") || die;
print MAIL "To: $mem_email \n";
print MAIL "From: $setup_email_admin\n";
print MAIL "Return-Path: $setup_email_admin\n";
print MAIL "Subject: $in{'subject'}\n\n";
print MAIL "$in{'body'}\n";
print MAIL "\n\n";
print MAIL "============================================================\n";
print MAIL " ACCESS TEAM\n";
close (MAIL);

}
close (SIGNUP);

}
else {

for ($i=0;$i<=$in{'total'};$i++)
{
if ($in{"email_$i"} ne "") {
open(MAIL, "|$setup_sendmail -t") || die;
print MAIL "To: $mem_email \n";
print MAIL "From: $setup_email_admin\n";
print MAIL "Return-Path: $setup_email_admin\n";
print MAIL "Subject: $in{'subject'}\n\n";
print MAIL "$in{'body'}\n";
print MAIL "\n\n";
print MAIL "============================================================\n";
print MAIL " ONLINE ACCESS TEAM\n";
close (MAIL);
}
}
}

}
sub mail_sent {

print "Content-type: text/html\n\n";
print $header;
print "<body bgcolor=\"#E2DEB8\">\n";
print "<h1>Email Sent</h1>\n";
print "Your email has been successfully sent. Press 'Retun' to go back to the main menu.\n";
if ($demo eq "on") {
print "<P><i>Email disabled in demo.</i><P>\n";
}
print "<FORM ACTION=\"$ENV{'SCRIPT_NAME'}\" METHOD=\"POST\">\n";
print "<input type=submit value=\" Return \">\n";
print "</form>";
print $footer;
exit;
}
 
I think you should try and work some of it out for yourself.

When you hit trouble, holler back

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
How'd you get on?
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top