View Full Version : Sending a variable from one CGI file to another

09-10-2002, 10:03 PM
how would i take a variable from one file and send it to another file. Is there some type of memory that i can store it in and then get another program to call it?

Basicaly all i want to do is get a user name which was inputed through one cgi file and stored into a variable and send that user name to another file.


here is where it leads to:

# created by Mark Y. (DRT_Phobia)
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
my $theplace='addcontent.cgi';#the place that gets $user
my ($user,$pw,$reg);
my %drts='phobia'=>'mark','matt'=>'matt',
if (($pw eq '') || ($user eq '')){
print "Content-type: text/html\n\n";
print "<html><body>";
print "missing user name (and) or password";
}elsif ($pw eq $reg) {
print "Content-type: text/html\n\n";
print "<html><body>";
print "Logged on! <b>$user</b><br><br>";
print "<form action=$theplace method=post>";
print "<input type=submit value=Continue>";
print "</form>";

}else {
print "Content-type: text/html\n\n";
print "<html><body>";
print "deneyed $user";
}print "</body></html>";

09-10-2002, 11:08 PM
You could use cookies to store a var on the users computer. To send it to another program for example.

Mzzl, Chris

09-11-2002, 04:38 AM
i never thought of that. ill try it, but going into cookies just for one little variable :(

i did try a txt file too. I just want to fine the shortest way possible. There just has to be some kind of way to make a variable global from one CGI program to another...

ty Chris

09-11-2002, 11:38 PM
im new to this but i just though of a crazy idea...

u can create a lib file where u can have a sub with the desire variable...

and everytime u want to rewrite it... do the following...

open FILE <open(FILE, ">Global_Var.lib");

print FILE 'sub DUNNO {';
print FILE '$Var = ';
print FILE "$Var_On_Script";
print FILE ' } \n';


09-11-2002, 11:39 PM
i made some typos in the first line sorry :o

hope this could help ! :thumbsup:

09-12-2002, 02:42 AM

Well, You have two ways: you can create the file and write your variable into it or open a socket and send the variable to your other script (but it must be hosted on server) by get method.

thats it.

09-14-2002, 07:39 PM
Well, not sure but is there any reason, you don't URL encode it? You are just passing the name right?

just add the variable to the end of the call to your other CGI


Then use

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};

$buffer is whatever you posted. Sounds like some work, but no more than creating a file to hang around.


09-14-2002, 11:10 PM
i started playing with is before, but then i got lost and started to look for an alternative. ill just post the thing i made:

# created by Mark Y. (DRT_Phobia) and some people
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
my $user;
my $servernews='../docs/cs_news.html';
my $internetnews='http://localhost/cs_news.html';
my ($name,$value,$aline,$buffer,@alinesotxt);
my ($sec,$min,$hour,$mday,$mon,$year_off,$junkus,$atop,$year,$min2);
my ($subject,$content,$update_info);
#--------get the user name from the login.cgi------GET_USER_NAME
if ($ENV{'REQUEST_METHOD'} eq 'POST') { # <--idez
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); #should be name=phobia <--idez
chomp $buffer; # <--me
($name,$user)=split(/=/,$buffer); #$name=user,$value=phobia<--me
} else { # <--idez
$user=$ENV{'QUERY_STRING'};} # <----idez
if ($hour>12){
if ($min<10){$min2='0';}
#print "Content-type: text/html\n\n";
#print "<html><body>";
#print "</body></html>";
# ---------------------title-and-post-input-area--------------GET_POST
print "Content-type: text/html\n\n";
print "<html><body>";
print "$user $buffer";
print "
<br><br><br>$user $buffer<br>
<form action=$ method=post>
Title:<input type=text name=subject size=40
Post Information:<br><textarea name=content cols=65
rows=5 wrap></textarea><br><br><br>
<input type=submit value=submit><input type=reset value=reset>
print "</body></html>";
#-----take form input of form and sort the string given--FIND_VAR
foreach $aline(@alinesotxt){
($name,$value)=split(/=/,$aline); #
$value=~tr/+/ /; //#takes out '+' which is a space and replaces it with a space
$value=~s/<\!\-\-\.*\-\->//g; #takes out commnets
#---------open the html file and write to it-----------------------WRITE
# ---------------------> !!!! im not done this part yet !!!!
print NEWS"
<table border=0>
<TD bgcolor=990000 WIDTH=25%>
$mon-$mday-$year $hour:$min2$min$atop</TD>
<TD bgcolor=990000 WIDTH=65%>
<TD bgcolor=333333 WIDTH=10%>
<TR><TD colspan=3>
<hr color=black width=498>

im just stuck :confused:
for some reason its not sorting the user name from the file before this one.

it gets this-


i tryed to print it but that didnt work so i guess i gets nothing...