Connecting Tech Pros Worldwide Help | Site Map

Internet logging using c# & perl cgi script

  #1  
Old June 11th, 2009, 10:28 AM
Member
 
Join Date: Apr 2007
Posts: 75
Hi Guys,
Sorry for duplicate posting as this Question refers to both c# and perl cgi script on the net, so please help.

Here is the original post
Internet logging using c# & perl cgi script

which contains the perl cgi script and c# code i am using.


Please help - any help will be highly appreciated.
  #2  
Old June 11th, 2009, 05:19 PM
Member
 
Join Date: Jun 2009
Posts: 43

re: Internet logging using c# & perl cgi script


There's nothing in the Perl script that would cause it to duplicate the log entries so I'm not sure what you're wanting from us, unless you're wanting us to help you cleanup the very poorly written script.
  #3  
Old June 11th, 2009, 06:43 PM
Member
 
Join Date: Apr 2007
Posts: 75

re: Internet logging using c# & perl cgi script


Any help to clean and make it better would be highly appreciated. Also can we return some text to the browser so that it shows some status from the script?

Sorry i am very new at perl. i am more experienced with .Net but the server doesnt support it:(
  #4  
Old June 12th, 2009, 03:21 PM
Member
 
Join Date: Jun 2009
Posts: 43

re: Internet logging using c# & perl cgi script


The only thing your Perl script does is write a log entry of the passed in cgi params each time it's called. I don't see why you're not doing that in the c# program.

I cleaned up the script and left out the parts that didn't really do anything.
Expand|Select|Wrap|Line Numbers
  1. #!c:/Perl/bin/Perl.exe 
  2.  
  3. use strict;
  4. use warnings;
  5. use POSIX 'strftime';
  6. use CGI ':standard';
  7. use Fcntl ':flock';
  8.  
  9. my $counterfile = 'D:/output.txt';
  10. open my $FILE, '>>', $counterfile or die "Can't open '$counterfile' file $!";
  11. flock($FILE, LOCK_EX); # blocking write lock
  12.  
  13. my @fields;
  14. $fields[0]  = param('username');
  15. $fields[1]  = param('contract');
  16. $fields[2]  = param('status');
  17. $fields[3]  = param('file'};
  18. $fields[4]  = $ENV{'REMOTE_ADDR'};
  19. $fields[5]  = strftime("%m/%d/%y %H:%M:%S", gmtime(time));
  20. $fields[6]  = $ENV{'HTTP_USER_AGENT'}
  21. $fields[7]  = param('iver');
  22. $fields[8]  = param('sys');
  23. $fields[9]  = param('sysloc'};
  24. $fields[10] = param('lang');
  25. $fields[11] = param('OpSys');
  26.  
  27. print $FILE join(',', @fields), "\n";
  28.  
  29. flock(FILE, LOCK_UN);
  30. close $FILE;
I don't know c# but I assume that it captures the STDOUT stream from the Perl script. So, if you need to pass back anything to the c# program, just print to the STDOUT handle, which is the default when using the Perl print function.

The die statement in the open call sends its output to the STDERR handle.
  #5  
Old June 16th, 2009, 05:43 AM
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,543

re: Internet logging using c# & perl cgi script


My first bit of advice is to include the following into every Perl script:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
You will find that there are a number of people here who won't touch your script unless you have these in there. By using them, you will be able to correct a lot of the simple coding mistakes that beginners make and allow us to better tackle the real issue(s) at hand.

As for your Perl script (this is before looking at Ron's version, I don't see anything in your script that says "I am CGI" other than your reference of the ENV hash. To output to the screen you will really need to use the CGI module as Ron has done.

There are plenty of tutorials on CGI in Perl on the internet, you just have to Google them. I would also highly recommend you get a copy of "Learning Perl'" from O'Reilly. It is a wonderful introduction into the world of Perl and I always keep a copy within reach.

Regards,

Jeff
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet logging using c# & perl cgi script alag20 answers 0 June 11th, 2009 10:22 AM