Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need help with a strange project using CGI and Perl

    Hello everyone. I'm new to these forums and I'm in quite a bit of situation with a class I'm currently taking so I'm going to go ahead and explain what's being asked of us and what we're working with in hopes that I may be able to find the help or direction I need to complete this project.

    I'm taking a networking troubleshooting class where the instructor has assigned us a project that, in my opinion, has NOTHING to do with network TS.

    TDL: He's assigned us to work on a WAMP server using EasyPHP-5.3.5.0 where we have to build a fully functioning page. None of us have ever taken advanced web page classes or know anything about perl / cgi.

    The project itself is graded based on the following:

    Guestbook-perl (working)
    Guestbook-php (working)
    Login-php (working)
    Login-perl
    Logout-php (working)
    Logout-perl
    Session-php (working)
    Session-perl
    Manage Users-php (working)
    Manage Users-perl

    All of my PHP content is working as it was very easy to find tutorials and online references to getting PHP and MySQL to work together. However I've run into a huge snag concerning the perl and cgi scripts.

    I'm finding that perl in general for authentication isn't very popular and it's difficult to find working scripts that aren't relying on a back end database such as MySQL for authentication.

    The instructor is expecting Login-perl to work as such:

    Authentication is verified by reading from a flat text file with the information inside it upon which a cookie is created and stored on the machine. This cookie is used to allow access to the session-perl (nothing more than a blank page that just requires the cookie to be present to view) and the manage users-perl pages.

    The Manage Users page is nothing more than being able to add and remove users and their passwords from the flat text file. Both session-perl and manage users-perl must contain a script that checks for the cookie. If the cookie isn't present, the page redirects the user to the login-perl page.

    The logout-perl does nothing more than destroy the cookie. I've spent weeks searching around for information along these lines but the information is very scattered and mixed. Hopefully someone here can give me some idea of where to go or how to even get started on something like this O.O

    Again, I'm not a programmer and I really know nothing about perl or cgi. I can follow scripts somewhat as I read them but really have no knack for creating of my own (and is not expected of the class). We were simply advised to search around the net for already working scripts.

    I apologize for the long post but I felt the more information I could put out, the easier it would be for others to see what exactly it is that's expected of the project objective and how it's supposed to work.

    Thank you all for your time and hopefully I can make some progress on this soon

  • #2
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Login-perl and session perl pages are done. Anyone have any idea how I can destroy the cookie if I click the logout-perl link? Please let me know if you need to see my cookie-get.cgi script

    I'm gonna start working on the manage users page today. Adding users into the textfile shouldn't be too complicated. What I'm not certain about is how I'm going to remove them. My instructor suggested using an array that wipes the text file and re-writes it with the remaining data that wasn't removed.

    Any ideas on how I could accomplish this? Or at least get it started :\

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Muqq View Post
    Login-perl and session perl pages are done. Anyone have any idea how I can destroy the cookie if I click the logout-perl link? Please let me know if you need to see my cookie-get.cgi script
    try to set the 'expiration date' of the cookie in the past. See man page for CGI::Cookie, expire method if you use this module.

    best regards

  • Users who have thanked oesxyl for this post:

    Muqq (05-05-2011)

  • #4
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm running this on a WAMP server but I think I understand what you're getting at.

    $cookie = $query->cookie(-name=>'MY_COOKIE',
    -value=>'BEST_COOKIE=chocolatechip',
    -expires=>'-4h',
    -path=>'/');

    something like this? or some like -expires=>'now',
    based off the tutorials I've read

    I'm trying to figure out how get the script to run when clicking on the logout-perl.html link
    Last edited by Muqq; 05-05-2011 at 07:38 PM.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Muqq View Post
    I'm running this on a WAMP server but I think I understand what you're getting at.

    $cookie = $query->cookie(-name=>'MY_COOKIE',
    -value=>'BEST_COOKIE=chocolatechip',
    -expires=>'-4h',
    -path=>'/');

    something like this? or some like -expires=>'now',
    based off the tutorials I've read

    I'm trying to figure out how get the script to run when clicking on the logout-perl.html link
    i see two solutions:
    1. the link from the html page point to a perl script which do some logout stuff, including cookies expiration then redirect whatever you need.
    2. use javascript to expire the cookies using a onclick event for that link.

    best regards

  • #6
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I decided to try having a logout page that prompts for logout confirmation

    <td><center>Are you sure you want to logout of your perl session?</td></center>
    <td><center><input value="Yes" type="submit"><input value="No" type="reset"></td>
    with a form action of

    <form action="/cgi-bin/logout.cgi" method="get">
    Here is my logout.cgi file:
    #!../perl/bin/perl.exe -Tw
    use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
    use CGI qw(:standard);

    $query = new CGI;

    $cookie = $query->cookie(-name=>'MY_COOKIE',
    -value=>'BEST_COOKIE=chocolatechip',
    -expires=>'now',
    -path=>'/');


    print $query->header(-cookie=>$cookie);

    print $query->start_html('My cookie-set.cgi program');
    print $query->h3('The cookie has been destroyed');
    print "<INPUT TYPE=\"button\" VALUE=\"Go back to login\" onClick=\"location.href='../login-perl.html'\">";

    print $query->end_html;
    but I don't think it's working as intended because I am still able to view my session-perl page which has a java script that checks for the cookie. Currently using this java scrip

    <script type-"text/javascript">
    <!--
    function confirm_it()
    {
    if (document.cookie != '')
    (
    return ("Cookie exists");
    }
    else
    {
    window.history.back(-1);
    ;
    }}
    //-->
    </script>
    Which I think is fine. Problem is most likely in my logout.cgi coding

    I'm aware "no" is set as a reset in my table. I'll fix that later to redirect back to the index.html page

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Muqq View Post
    I decided to try having a logout page that prompts for logout confirmation



    with a form action of



    Here is my logout.cgi file:


    but I don't think it's working as intended because I am still able to view my session-perl page which has a java script that checks for the cookie. Currently using this java scrip



    Which I think is fine. Problem is most likely in my logout.cgi coding

    I'm aware "no" is set as a reset in my table. I'll fix that later to redirect back to the index.html page
    B -> A -> C
    if A is the current page, B the page from where you come and C the page where you go, any changes to A are not visible until you are on the page C.
    Also because http is stateless you need to pass things from one page to another.
    That means when you are on B you send the information to the next page but when you are on A you need first to get what B sent you and restore the 'state'.
    Same thing you need to to, like on the B page, when you want to pass information to C.
    to restore the cookies from last page you can use fetch from CGI::Cookie, then pass the cookies to the new CGI handler.
    I didn't try but i don't think that setting expiration to 'now' it work. Why don't you use '-1M' or something instead?

    best regards

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,915
    Thanks
    2
    Thanked 164 Times in 159 Posts
    It's probably to late for you to change directions, but CGI::Session would simplify the cookie process. It stores all of the cookie data on the server side and the only thing stored on the client is the session id.

    http://search.cpan.org/~markstos/CGI...CGI/Session.pm

    http://search.cpan.org/~markstos/CGI...on/Tutorial.pm

  • #9
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've got my sesssion, login and logout scripts working perfectly and I can add users into the text file. It was an error in my java script that was causing the previous issues I'd stated before

    Does anyone have any suggestions on how I can use an array to wipe a username and password from the file and rewrite the file with the remaining data?

    the txt format works as such:

    userassword
    theuser:thepassword

    the file name is called passwordperl.txt

    I need a way to view all the current users and their selective password and to be able to select to remove them. I was thinking a tickbox or drop down menu might work but I have no clue how to do this or where to even start.
    Last edited by Muqq; 05-06-2011 at 12:54 AM.

  • #10
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay I've found a script that can remove data, more specifically lines, from a file.

    use Fcntl qw(:flock :seek);

    $sitedata="websites.cgi";

    open(DAT, $sitedata) || die("Cannot Open File");
    @raw_data=<DAT>;
    close(DAT);

    splice(@raw_data,1,1);

    open(DAT,">$sitedata") || die("Cannot Open File");
    flock(DAT, LOCK_EX);
    seek(DAT, 0, SEEK_SET);
    print DAT @raw_data;
    close(DAT);
    Now comes the complicated portion of being able to find a way to display my current file data on the webpage and selectively choose which user / password (line) that I want to remove and find to get that line number to post into the script so it knows what to remove

  • #11
    Rah
    Rah is offline
    New to the CF scene
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It's Dennis from class.

    How did you solve the logout problem Muqq. Apparently I've gotten the login working but I can't form the session to work. Please let me know.

  • #12
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Muqq View Post
    Okay I've found a script that can remove data, more specifically lines, from a file.
    ....
    Now comes the complicated portion of being able to find a way to display my current file data on the webpage and selectively choose which user / password (line) that I want to remove and find to get that line number to post into the script so it knows what to remove
    Muqq,

    The code you found is flawed in that it suffers from the possibility of a race condition. The reading portion of the script must also use flock if you want to be sure that no changes to the file get lost or mangled.

    The following utilizes read/write mode to only open the file once and prevents any race conditions.

    Code:
    use Fcntl qw(:flock :seek);
    
    use strict;
    use warnings;
    
    my $sitedata = "websites.dat";
    
    open my $fh, '+>>', $sitedata or die "Can't open $sitedata: $!";
    flock $fh, LOCK_EX or die "Can't flock: $!";
    seek $fh, 0, SEEK_SET;
    my @raw_data = <$fh>;
    
    # Hover around 20 rows of random data
    if (@raw_data < rand 40) {
    	# Insert a row
    	my $pos = int rand(1 + @raw_data);
    	splice @raw_data, $pos, 0, localtime . "\n";
    	print "Inserting at $pos of $#raw_data\n";
    	
    } else {
    	# Remove a row
    	my $pos = int rand @raw_data;
    	splice @raw_data, $pos, 1;
    	print "Deleting at $pos of $#raw_data\n";
    
    }
    
    seek $fh, 0, SEEK_SET;
    truncate $fh, 0;
    print $fh @raw_data;
    close $fh;
    Regards,
    - Miller


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •