PDA

View Full Version : Need Advice! Help!


Phanataz
11-11-2001, 08:07 PM
Say for example that when I made that Stealth IQ quiz, I wanted it so that when someone clicked on the "Submit for grading" button, their ranking and their name would be recorded onto the web server in either a text file or something for easy maniuplation.

I'm not going to do this. However, I really would like to know how this concept works for the future.

Would I need like CGI or something?

MadMikey
11-11-2001, 09:50 PM
Depends on the OS of your web server. For UNIX servers, you could use PHP, PERL or shell scripts. For WindowsNT, you'd probably use VBScripts / ASP.

Using your page, when clicking on the submit button, the URL passed to the next page looks something like this:

/phanataz/stealthiq.html?q1=...&correct=25&wrong=2&blank=0

This URL will automatically create the variables $correct, $wrong and $blank for the script to use.

Here's what you could do in PHP:


<?php

// The variables created by the URL are as follows:
// $correct with a value of 25
// $wrong with a value of 2
// $blank with a value of 0

// Just for fun, let's grab the user's IP address and store that as well
// That way you know who has done the quiz
$ip = getenv($REMOTE_ADDR);

// Open a file for appending only. Use the full path to the file.
$fp = fopen("/path/to/file/data.txt", "a");

// Build a comma delimited string of the data we want to write
$data = "$ip,$correct,$wrong,$blank\n";

// the string now looks something like "1.2.3.4,25,2,0\n"
// the '\n' is a new-line character for UNIX systems

// Now we'll write the data to the text file
fwrite($fp, $data);

// Now close the file
fclose($fp);

?>


This will create a simple, comma delimited file that you can use in any application that can read CSV files, like MS Excel or MS Access.

Let me know if you have any other questions.

Phanataz
11-11-2001, 11:06 PM
MotionlessMikey -

Scary part is the fact that was making sense hehe

I signed up for a free hosting service, but they don't use PHP..but can use perl and CGI. I know some perl, because I took a unix course in college.

So in theory, i could write a perl script file, similar to the PHP script that you demostrated. So when they clicked on that "submit for grading" button.. the URL
"/phanataz/stealthiq.html?q1=...&correct=25&wrong=2&blank=0 " would be passed to the free hosting server like this?

"http://www.freehostingserver.com/phanataz/myscript.pl?/phanataz/stealthiq.html?q1=...&correct=25&wrong=2&blank=0 "

Or am I just taking a stab in the dark here? I'm just trying to get a concept of how this works..

MadMikey
11-11-2001, 11:40 PM
Since your using perl, you would want to change the action in your HTML form to call your perl script when they click submit.

The perl script needs to be in a directory that allows you to execute scripts (ie, cgi-bin)

Change your form tag to something like this:

<form type="GET" name="f" action="/cgi-bin/results.pl">

The names of your input boxes will automagically be appended to the URL as variables. So the resulting URL would look like:

http://www.server.com/cgi-bin/results.pl?correct=25&wrong=2&blank=0

Your perl script will need to echo out the HTML that needs to be displayed in the browser. Here's how to do the above with a perl script:


#!/usr/local/bin/perl -w

# Get the user's IP address
$ip = $ENV(REMOTE_ADDR);

# Open the file. The >> means append to the file. FP is a file handle.
open (FP, ">> data.txt");

# Build our CSV string
$data = "$ip,$correct,$wrong,$blank\n";

# Write it to the file
print FP $data;

# Close the file
close(FP);

# Echo something to the browser so that the user knows their input was received
print "<html>\n;";
print "<body>\n;";
print "<center><h3>Thank-you for your input!<br>\n";
print "You got $correct right, $wrong wrong<br>\n";
print "and did not answer $blank questions.</h3></center>\n";
print "</body>\n";
print "</html>\n";


I've always heard it said, that if you can't do it in perl, then it can't be done. Personally, I can't do much in perl. I sat down to start teaching myself and found PHP instead.

Phanataz
11-12-2001, 12:10 AM
now youre just making it too easy.. hehe Okay as soon as I can get into that free hosting site I will try that.. i signed up for www.netfirms.com...but I can't FTP into my site at the moment. I have a better understanding of what to do, and I'll use your perl script to see if it will work with my quiz.. just for kicks.


umm..the part where you use print to print something back to the browser.. how does that work? Is it printing something back to the visitor's browser because the "submit" button called the perl script? i thought printf printed something to the screen..not a browser.. or is that html file your making?

uh oh my head just exploded...

Lara
11-12-2001, 01:36 PM
Think of the perl script like a page.

Your link leads to the perl script, but it needs to display something. Ordinarily a link would go to an html page, which the webserver would send. You'd then see the page.

So, if you link to a perl script to do something, it (the perl script) needs to send something back to say it's done it.

printf is a 'formatted' print. It's a leftover from C.

Printing when done in perl basically prints to STDOUT (which is normally a screen yes). When executed as a cgi though, it basically prints to 'what it's connected to' which in this case is a browser (vicariously through the web server).

So, anything you print in a perl cgi would be echo'd back to the browser. You do have to effectively 'print' the format of an html page though.

And urm.. I think you'd need this before the first print aswell :

print "Content-type: text/html\n\n";

otherwise it doesn't know what it's recieving - you could for instance 'print' the contents of an MP3, and sortof 'stream' it to the browser. In which case you'd use a different type like :'Content-type: Xaudio: MP3'

Lara.

Phanataz
11-12-2001, 02:35 PM
Woah thats cool! Thinking of all kinds of uses for perl and cgi now hehe

Grr.. i still can't upload to my new CGI account on netfirms.com

Does anyone know of a free CGI hosting server so that I can practice scripts on?

-tekg0d-
11-12-2001, 05:29 PM
try them...
http://www.mycgiserver.com/

-tekg0d-
11-12-2001, 05:31 PM
or this place has a grip of stuff for free...
http://www.freewebmasterhelp.com/

Phanataz
11-12-2001, 07:38 PM
Thanks Tekky.. i will try them out.

Netfirms.com is giving me trouble.. when I try to log in via FTP i get:

451 Login Failed, can't set startup dir to your home directory:No such file or directory.

I don't have any other trouble with any other FTP sites. I've explained this to them, but they tell me it's my fault some how.

Sure sounds to me like they aren't doing something quite right..hrmmm?


Also, Tekky. i just tried out MyCGIServer..they don't support PHP, Perl, or other scripting languages :(

-tekg0d-
11-12-2001, 08:07 PM
they support perl & php at least thats what the site says...
hmm

Phanataz
11-13-2001, 02:12 AM
Okay.. i was able to send the perl script to my account on netfirms.com.

I chmod'ed the permissions to 755 of the perl file. And it is running.. the only trouble is that it doesnt seem to be accepting the paramters of the # of right and wrong answers. There is a data.txt file that is produced and it just has:
,,,

I also had to change the line:
$ip = $ENV(REMOTE_ADDR);

to

$ip = $ENV{'REMOTE_ADDR'};

Other wise it wouldn't compile... what am i doing wrong?

Oh..and im using the command line you suggested:
http://www.server.com/cgi-bin/results.pl?correct=25&wrong=2&blank=0

Do i have to define the variables or something>?

MadMikey
11-13-2001, 10:43 AM
One thing that might be causing an issue is the fact that your validating the answers with javascript which is a client side process. If you changed the <form> tag to show something like <form action="myscript.pl">, then let the first part of your perl script validate the answers, you might get what your looking for.

If you just submitted the answers to your perl script, the perl script could calculate $correct, $wrong and $blank.

When you click Submit, the URL would be something like:

/script.pl?q1=value&q2=value&q3=value.....

The perl script would:

1) calculate $correct, $wrong and $blank
2) write those values to a file
3) display something for the user.

Sorry if this is confusing, it's pretty early in the morning still.

Phanataz
11-13-2001, 01:54 PM
Well that is possible, but it seems like the perl script isn't accepting any parameters from the URL. for instance.. if i manually type in the URL:

pd1.netfirms.com/go.pl?correct=25&wrong=2&blank=0

into the browser (and not even using the javascript in the stealthiq html file) it still doesn't work. It's like it's not remembering what the values of $correct,$wrong,$blank are.
$IP works though. So now in my data.txt file i get:
24.x.x.x,,,

So i know its somewhat working...

Could it because the perl script is hosted on a different site than the stealthiq html file which is hosted on @home?

I've chmod'ed the file and the cgi-bin directory to 755..

MadMikey
11-13-2001, 06:29 PM
You're right! I totally forgot that perl doesn't handle the variables from the URL as easily as php. They are all stored in a single environment variable. I believe it's $ENV('QUERY_STRING'), but I'm not certain.

I know there are some pretty standard routines to take the URL and break it up into and array so that you can use it.

Form data handling is one of the main reasons I went with PHP over perl, but it IS just a personal prefernece. Like I said before, perl is quite a bit outside my area of experience.

You could always bust out Teach Yourself PERL in 24 hours (http://shop.barnesandnoble.com/booksearch/isbnInquiry.asp?isbn=0672317737). I have a copy, but must admit, I only made it about 4 hours.

Phanataz
11-13-2001, 07:46 PM
MotionlessMikey -

This is what i have been working on:

#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;


# Get the user's IP address
$ip = $ENV{'REMOTE_ADDR'};

# Open the file. The >> means append to the file. FP is a file handle.
open (FP, ">> data.txt");


# I guess url_param is supposed to read the URL passed to the script..
$correct = url_param ( 'correct' );
$wrong = url_param ( 'wrong' );
$blank = url_param ( 'blank' );

$data = "$ip,$correct,$wrong,$blank\n";

# Write it to the file

print FP $data;

# Close the file
close(FP);

# Echo something to the browser so that the user knows their input was received
print "Content-type: text/html\n\n";
print "<html>\n";
print "<body>\n";
print "<center><h3>Thank-you for your input!<br>\n";
print "You got $correct right, $wrong wrong<br>\n";
print "and did not answer $blank questions.</h3></center>\n";
print "</body>\n";
print "</html>\n";


Now I did try using:
$query_string = $ENV{'QUERY_STRING'};


And saving the value of $query_string into data.txt. That did work..except it doesnt save it in the format i want ..like:
Ip addy,25,2,0

but rather:
correct=25&wrong=2&blank=0

I couldnt figure out a way to break down the string. I didn't understand the SPLIT function (if thats what im supposed to use) .. The script above I havent tested yet because my Netfirms.com is having technical difficulties.

In case it doesnt... any ideas on how to break down the string:
correct=25&wrong=2&blank=0

into:
Ip addy,25,2,0

?

MadMikey
11-14-2001, 01:03 AM
Alright P, here it is, It's been tested a little bit, nothing thorough.


#!/usr/bin/perl -w

# This breaks up the URL into variable = value pairs. $$ is NOT a typo!
@values = split(/&/,$ENV{'QUERY_STRING'});
foreach $i (@values) {
($varname, $value) = split(/=/,$i);
$$varname = $value;
}

# Get the user's IP address
$ip = $ENV{'REMOTE_ADDR'};

# Open the file. The >> means append to the file. FP is a file handle.
open (FP, ">> data.txt");

# Write a CSV string to the file
print FP "$ip,$correct,$wrong,$blank\n";

# Close the file
close(FP);

# Echo something to the browser so that the user knows their input was received
print "Content-type: text/html\n\n";
print "<head>\n";
print "</head>\n";
print "<html>\n";
print "<body>\n";
print "<center><h3>Thank-you for your input from $ip!<br>\n";
print "You got $correct right, $wrong wrong<br>\n";
print "and did not answer $blank questions.</h3></center>\n";
print "</body>\n";
print "</html>\n";


Also, I had to create the data.txt file, using the command:

touch data.txt

and chmod it to 777. The user that the apache web server is running as (usually httpd or apache) needs to be able to write to it, to 777 works.

Hitting refresh over and over will write multiple lines to the text file. The output looked similar to this:


127.0.0.1,25,2,0
127.0.0.1,25,2,0
127.0.0.1,25,2,0
127.0.0.1,25,2,0


It was a nice little puzzle, and has peaked my interest in perl ... a little.

What's next? :D

Phanataz
11-14-2001, 01:59 AM
MotivationalMikey,

Ahh... okay so thats how you use the split command.


Lemme see if i got this:
------------------------------------------
@values = split(/&/,$ENV{'QUERY_STRING'});

split works by including the place you want to break up the line by putting the character in /'s right? After the comma you have the line you want to break up...which is the URL..?

-------------------------------------------

foreach $i (@values) {
($varname, $value) = split(/=/,$i);
$$varname = $value;
}

Here you are saying..for each parameter in the URL:
/cgi-bin/results.pl?correct=25&wrong=2&blank=0
..get the value of it. I guess it knows to create the variables:
$correct, $wrong, and $blank..

$i would equal 3 since there are 3 parameters..
----------------------------------------------

Well do I have it? hehe

I'll try out this script as soon as I can upload it and run it. I really need a better CGI host. The one im using keeps having technicial problems.

Thank you MM! I learned alot so far and i'll let you know how this goes..