PERL User Info
hi...i have this:
#! /usr/local/bin/perl
echo Content-type: text/plain
echo
echo CGI/info gathering report
echo
echo argc is $#. argv is "$*".
echo
echo SERVER_SOFTWARE = $SERVER_SOFTWARE
echo SERVER_NAME = $SERVER_NAME
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_PORT = $SERVER_PORT
echo REQUEST_METHOD = $REQUEST_METHOD
echo HTTP_ACCEPT = $HTTP_ACCEPT
echo PATH_INFO = $PATH_INFO
echo PATH_TRANSLATED = $PATH_TRANSLATED
echo SCRIPT_NAME = $SCRIPT_NAME
echo QUERY_STRING = $QUERY_STRING
echo REMOTE_HOST = $REMOTE_HOST
echo REMOTE_ADDR = $REMOTE_ADDR
echo REMOTE_USER = $REMOTE_USER
echo CONTENT_TYPE = $CONTENT_TYPE
echo CONTENT_LENGTH = $CONTENT_LENGTH
and i would like to automatically run this program in the background of my website...how would i do that...also, how would i get it to post the results in a flat .txt file?
thanks
Wil posted this at 08:39 — 23rd April 2002.
They have: 601 posts
Joined: Nov 2001
Kyle
Hm. You want to try and snoop on your visitors, huh? You really are making life difficult on yourself - why define all the CGI env. vars? Why not just loop through them all, aka:
#!/usr/local/bin/perl
print "Content-type: text/plain\n\n";
print "<tt>\n";
foreach $key (sort keys(%ENV)) {
print "$key = $ENV{$key}<p>";
}
Getting this to run in the background would be simple, but it wouldn't record anything, as it needs to gather information from the user. The only way I can think of making this work like you want to is to get people to actually click into this script - you can do it transparent, as it can just be a redirect script to another page, but unknowingly to them while they are redirected they are also 'recorded'.
Cheers
- wil
kb posted this at 00:11 — 24th April 2002.
He has: 1,380 posts
Joined: Feb 2002
ok...thanks!
so, instead of using index.html as my main page, use home.html, and have the index.html redirect to the home.html automatically...but i dont understand the code you gave me(i'm not good with PERL)...what do i have to config so it auto stores somewhere? and how do i get it to be transparent if i am auto redirecting?
Wil posted this at 10:04 — 24th April 2002.
They have: 601 posts
Joined: Nov 2001
Do you want to store this information into a database or a flat file?
kb posted this at 01:32 — 25th April 2002.
He has: 1,380 posts
Joined: Feb 2002
flat-file (.txt)
Wil posted this at 08:37 — 25th April 2002.
They have: 601 posts
Joined: Nov 2001
Something like this should be sufficent for you. Put this in your main folder as index.cgi and then enter the URL you want them to be redirected to in $url.
Hope this helps!
#!/usr/bin/perl
use CGI;
my $query = CGI->new();
# where is our database?
my $db = "/path/to/db.txt";
# loop through env. variables and print them into database.
open (DB,">>$db") or die $!;
foreach $key (sort keys(%ENV)) {
print DB ("$key = $ENV{$key}\n");
}
print DB ("\n");
close DB;
# we've done snooping, let's redirect to another page.
my $url = "http://www.lets.redirect.them.here/";
print $query->redirect("$url");
- wil
kb posted this at 01:08 — 26th April 2002.
He has: 1,380 posts
Joined: Feb 2002
ok thanks...i will try it (prob by mon) and let you guys know how it works
kb posted this at 01:11 — 26th April 2002.
He has: 1,380 posts
Joined: Feb 2002
i have yet one more question (sorry...i am not very good at this):
how would i get them to go to index.cgi?...
would i make an index.html and then somehow run index.cgi? please help me on how to run this
Suzanne posted this at 01:29 — 26th April 2002.
She has: 5,507 posts
Joined: Feb 2000
if you have index.cgi, you don't need index.html. Usually. Usually the server set up is to search for an index page, with a set of extentions -- .htm, .html, .cgi, et cetera.
kb posted this at 01:31 — 26th April 2002.
He has: 1,380 posts
Joined: Feb 2002
ok thanks...
so i just have to have an index. cgi...no index.html
and then the script would run, collect data, and transfer pages?
Mark Hensler posted this at 05:20 — 26th April 2002.
He has: 4,048 posts
Joined: Aug 2000
If all goes well, yes. If not, let us know...
Wil posted this at 15:17 — 27th April 2002.
They have: 601 posts
Joined: Nov 2001
This also assumes that your host allows you to run CGI applications from outside a designated 'cgi bin' - usually the /cgi-bin/ folder.
kb posted this at 21:29 — 27th April 2002.
He has: 1,380 posts
Joined: Feb 2002
i believe that i am allowed to run outside a cgi-bin folder
Wil posted this at 12:00 — 28th April 2002.
They have: 601 posts
Joined: Nov 2001
Then you should be able to follow Suzanne's instructions without any hickups If you do have any more questions; please let us know.
kb posted this at 21:22 — 29th April 2002.
He has: 1,380 posts
Joined: Feb 2002
hey guys...it didnt work...i got this error:
Premature end of script headers: /data1/hm/1eyefilms/index.cgi
so what happened? it is chmoded 777
you can check out the actual site at http://1eyefilms.hypermart.net
and the script at http://1eyefilms.hypermart.net/index.cgi
Wil posted this at 08:47 — 30th April 2002.
They have: 601 posts
Joined: Nov 2001
Strange. This works fine on my server. This is the code again; remember to upload this in ASCII mode.
#!/usr/local/bin/perl
use CGI;
my $query = CGI->new();
# where is our database?
my $db = "/home/fba/dev.fbagroup.co.uk/envvar/db.txt";
# loop through env. variables and print them into database.
open (DB,">>$db") or die $!;
foreach $key (sort keys(%ENV)) {
print DB ("$key = $ENV{$key}\n");
}
print DB ("\n");
close DB;
# we've done snooping, let's redirect to another page.
my $url = "http://dev.fbagroup.co.uk/envvar/other";
print $query->redirect("$url");
And just in case you don't believe me, here's the URL on my server:
http://clients.fbagroup.co.uk/envvar/
Click on the 'redir.cgi' - which is the code above, which will redirect you to a page that doesn't exist.
Then click back on your browser and look at the db.txt file to view the database of info it has logged.
Hope this helps.
- wil
kb posted this at 01:29 — 1st May 2002.
He has: 1,380 posts
Joined: Feb 2002
it doesnt work for me...maybe its my browser...i'm using AOL 7.0 to view it...i also tried IE...that didnt work either
i DID upload it in ASCII...heres the code i used:
#!/usr/bin/perl
use CGI;
my $query = CGI->new();
# where is our database?
my $db = "/home/hypermart.net/1eyefilms/restricted/memberdata.txt";
# loop through env. variables and print them into database.
open (DB,">>$db") or die $!;
foreach $key (sort keys(%ENV)) {
print DB ("$key = $ENV{$key}\n");
}
print DB ("\n");
close DB;
# we've done snooping, let's redirect to another page.
my $url = "http://1eyefilms.hypermart.net/home.html/";
print $query->redirect("$url");
did i do something wrong?
Suzanne posted this at 01:42 — 1st May 2002.
She has: 5,507 posts
Joined: Feb 2000
No trailing forward slash on the url, first of all.
What are the errors you're seeing?
Wil posted this at 08:33 — 1st May 2002.
They have: 601 posts
Joined: Nov 2001
Try creating the following file (a blank file)
"/home/hypermart.net/1eyefilms/restricted/memberdata.txt";
And then uploading it to there, and then CHMODing it to 777 to see if that helps things.
Also, is your path to perl correct for Hypermart? It's not /usr/local/bin/perl on hypermart, is it?
There is an error log feature on hypermart when you reach a 500 page - try logging in and see where the error or at least what line number it's on.
If you're having difficulties again, you can email me on wil@stephens your login details and I'll take a look for you!
Cheers
- wil
kb posted this at 01:00 — 3rd May 2002.
He has: 1,380 posts
Joined: Feb 2002
i fixed it...still doesnt work...it says line 12
Suzanne posted this at 01:40 — 3rd May 2002.
She has: 5,507 posts
Joined: Feb 2000
Your path to the flat file db is wrong, then.
scriptsolutions.com has a free app called PerlDiver. Install it and then run it to find the true path.
Suzanne posted this at 01:44 — 3rd May 2002.
She has: 5,507 posts
Joined: Feb 2000
you'll have to save the cgi as a text file for people to view it.
you might find the tutorials are wdvl.com of interest -- they have all levels and walk through some common applications.
Suzanne posted this at 01:46 — 3rd May 2002.
She has: 5,507 posts
Joined: Feb 2000
Weird, that response should have been with your other thread. !!
Or not -- did you delete a post? Ack! I'm going crazy! lol...
Wil posted this at 10:06 — 3rd May 2002.
They have: 601 posts
Joined: Nov 2001
It says what at line 12? For some reason it can't open your database file. Make sure
a) the file exisits.
b) your path is correct.
c) you've CHMOD the file to 777 (for testing purposes, anyway).
- wil
kb posted this at 22:38 — 4th May 2002.
He has: 1,380 posts
Joined: Feb 2002
ok...i did that and i got these errors in the log:
No such file or directory at /home//index.cgi line 12.
syntax error at /home/cgi-bin/login.pl line 20, near "):"
syntax error at /home/cgi-bin/login.pl line 27, near "}"
Execution of /home/cgi-bin/login.pl aborted due to compilation errors.
locate: not found
locate: not found
Scalar found where operator expected at /home//index.cgi line 12, near ")$!"
(Missing operator before $!?)
syntax error at /home//index.cgi line 12, near ")$!"
Execution of /home//index.cgi aborted due to compilation errors
and the code is
#!/usr/local/bin/perl
use CGI;
my $query = CGI->new();
# where is our database?
my $db = "/home/restricted/memberdata.txt";
# loop through env. variables and print them into database.
open (DB,">>$db")$!;
foreach $key (sort keys(%ENV)) {
print DB ("$key = $ENV{$key}\n");
}
print DB ("\n");
close DB;
# we've done snooping, let's redirect to another page.
my $url = "http://1eyefilms.hypermart.net/home.html";
print $query->redirect("$url");
Wil posted this at 12:42 — 5th May 2002.
They have: 601 posts
Joined: Nov 2001
OK. Look at the code you posted. Now look at the code I posted above? Spotted the error yet?
You have:
open (DB,">>$db")$!;
I had:
open (DB,">>$db") or die $!;
- wil
kb posted this at 19:02 — 5th May 2002.
He has: 1,380 posts
Joined: Feb 2002
will that make a difference?
Mark Hensler posted this at 19:18 — 5th May 2002.
He has: 4,048 posts
Joined: Aug 2000
Yes, it will. Wont it Wil?
kb posted this at 02:24 — 6th May 2002.
He has: 1,380 posts
Joined: Feb 2002
i finally got it...you guys forgot a ; after open (DB,">>$db") or die $!;
and i took out the or die part...thanks guys!(you can delete this thread now)
Wil posted this at 09:23 — 6th May 2002.
They have: 601 posts
Joined: Nov 2001
Hm. I didn't forget anything - scroll up to my original code posting, you just decided to do some editing
No need to delete the thread - this could be useful for other community members. Glad you got it working in the end!
- wil
Suzanne posted this at 17:11 — 6th May 2002.
She has: 5,507 posts
Joined: Feb 2000
For the future, $! is a variable in perl. In the case of the "or die $!", it returns the error for viewing if the function fails (dies).
littlest posted this at 10:18 — 7th May 2002.
They have: 4 posts
Joined: May 2002
I dont understand why nobody suggested using Server Side Includes (SSI)?
You have created the same script without fowarding the user anywhere. All you would have to include this in your files:
On your index or any page you wanted to record users. You may or may not have to change your files to .shtml files.
This would probally be better for the users depending on the speed of their connection. When using fowarding in perl it takes a sec to foward it, it isn't instant. If you use the server side includes, it will be recording the data while the user is viewing the page.
I would also recommed putting all the keys on one line, espcially if you start to get lots of visitors. I use a small script like that for one of my gallery sites and even tho it is all on one line, it is still crazy to read it in text db form.
Have fun learning to script, you can only get better!
Sarah :c)
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.