?
Solved

Please help me with my cgi, i know it is something simple

Posted on 2003-03-18
3
Medium Priority
?
288 Views
Last Modified: 2013-12-25
Hello, I have successfully written a test cgi script to set a cookie and then write to may database.  That works fine, but when I integrate it with my globill cgi script, it doesn't want to write the cookie.  Please tell me why this is happening, it has to be something simple, expecially if the first script works.  The following is the globill script.  The only thing I need you to concentrate on is the script I integrated with and it supporting subroutine.  I in put comments to make it easier for you to find.


############################################
#The globill script the I integrated the
#test script with.
############################################

#!/usr/bin/perl
#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#' globill.cgi Version 4.0
#'
#'
#' Copyright 1997-2002 Globill.com LLC
#'
#' Modificatons to this script are not permitted without prior permission from Globill.com LLC. Globill.com LLC
#' does not make any promises or gaurantees about the operation, reliability or installion of this script.
#' By installing this script, the installer, user, and website owner, assume full responsibility for any
#' problems or difficulty related to the installing or use of this script.
#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#' VARIABLES TO MODIFY:
#'  $htpasswd                 = 'Full path to your htpasswd file';
#'  $myusername         = 'Your globill webmaster username';

$htpasswd                = '/home/xxxxx/xx/xx/xx/xxx/cgi-bin/globill/.htpasswd';
$myusername                = 'xxxxxx';


#optional extra vars, do not modify if unsure
$isdbfile                = 0;        #if using dbm
$ignorecrypts        = 0;        #do not encrypt passwords

$htpasswd2                = '';        #second pass file, can add more
$myusername2        = '';        #second webm username, can add more

#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#' BEGIN SCRIPT
#' parse input (POST)
$gloversion                = '4.1';
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$input);
foreach $item(@pairs)
{
   ($key,$value)=split (/=/,$item,2);
   $value=~tr/+/ /; $value=~ s/%(..)/pack("c",hex($1))/ge; $value=~ s/<!--(.|\n)*-->//g;
   $posts{$key}=$value;
}

#' parse input (GET)
$query = $ENV{'QUERY_STRING'};
@pairs= split (/&/,$query);
$qstring='';
foreach(@pairs){($key, $value)= split (/=/,$_);        $gets{$key} = $value;}

#' simple security checks here
@myuname = ($myusername);
push @myuname, $myusername2 if ($myusername2);
push @myuname, $myusername3 if ($myusername3);
push @myuname, $myusername4 if ($myusername4);
$found=0; foreach $un (@myuname) {$found=1 if (lc $un eq lc $posts{'wusername'});}
&Dead("Incorrect webm username") if (!$found);

#' preset vars
$do = $posts{'do'}; $username=$posts{'username'};$password=$posts{'password'};$emailAddress=$posts{'email'};$ip=$posts{'ip'};


@htpasswdall = ($htpasswd);
push @htpasswdall, $htpasswd2 if ($htpasswd2);
push @htpasswdall, $htpasswd3 if ($htpasswd3);
push @htpasswdall, $htpasswd4 if ($htpasswd4);



##################################################
#Here's where I integrated the script
##################################################
print "Content-type: text/html\n\n";

$test="test";
$cookie=&set_cookie("login",$test,"+24h","","www.xxxxx.com");
print "$cookie\n";


use DBI;
$username="test";
$password="test";
# replace  dbname,user,passwd with actual values

$dbh = DBI->connect("DBI:mysql:ineedteenboys_com","root","success") or die "Couldn\'t connect to database: ".$DBI::errstr;

$sth = $dbh->prepare("insert into intb_users (userName,password) values (?,?)") or die $DBI::errstr;
$sth->execute($username,$password) or die $DBI::errstr;
$sth->finish;


$dbh->disconnect;



##################################################
#Script Ends that i integrated.
##################################################











#' add a password to htpasswd
if ($do eq 'add')
{
    $password = &docrypt($password);
    foreach $htpass (@htpasswdall)
    {
                   $ipath='';
            if (!-e $htpass) {
                           @parts=split(/\//,$htpass);
                           pop @parts; $dir = join('/',@parts);
                           pop @parts; $dir2 = join('/',@parts);
                    &glocate() if ($username eq 'glotest' && !-e $dir2);

                    if (!$isdbfile) {
                                foreach $d (@parts){
                                        $ipath .= '/' . $d;
                                        if (!-e $ipath){mkdir($ipath,oct('0777'));}
                                }
                            &fileopen($htpass,'>'); &fileclose();
                                system("chmod 666 $htpass");
                        }
                }

            if (&fileopen($htpass,'>>'))
                {
                        $unameok = &OkUname("$username:$password");
                        print FILE "$username:$password\n" if (!$isdbfile && $unameok);
                        $FILE{$username} = $password if ($isdbfile && $unameok);
                        &fileclose();
                } else {&Dead("Cannot locate or open htpasswd >$htpass file");}
        }
}

#' remove a password from htpasswd
elsif ($do eq 'remove')
{
        foreach $htpass (@htpasswdall)
        {
                if (&fileopen($htpass,''))
                {
                        if ($isdbfile) {delete $FILE{$username};}
                        else
                        {
                                @newlines = ();
                                @lines = <FILE>;
                                &fileclose();
                                foreach $l (@lines) {
                                        chomp($l);
                                        next if (!&OkUname($l));
                                        push @newlines, $l if ($l !~ /^$username\:/i);
                                }
                                $newfile = join("\n",@newlines);
                                &Dead('Cannot backup pass file during remove!') if (!rename($htpass,$htpass . "glo"));
                                &fileopen($htpass,'>');
                                print FILE "$newfile\n";
                                system("chmod 666 $htpass");
                        }
                        &fileclose();
                } else {&Dead('Cannot rw locate or open htpasswd file for rewrite');}
        }
}
#' synchronize
elsif ($do eq 'sync' || $do eq 'fullsync')
{
        $fullsync = ($do eq 'fullsync');
        @syncu = split(/\|/,$username);
        @syncp = split(/\|/,$password);
        %newpasses=();
        for ($i=0;$i<@syncu;$i++){$newpasses{$syncu[$i]} = &docrypt($syncp[$i]);}

        foreach $htpass (@htpasswdall)
        {
                %FILE=(); $newfile='';
                system("cp $htpass $htpass" . 'glo');
                system("chmod 666 $htpass");
                @parts=split(/\//,$htpass); pop @parts; $dir = join('/',@parts);
                system("chmod 777 $dir");

                &fileopen($htpass,'');
                if (!$isdbfile && !$fullsync)
                {
                        @lines=<FILE>;
                        foreach $l (@lines) {chomp $l; ($uname,$pass) = split(/\:/,$l); $FILE{$uname}=$pass;}
                        &fileclose();
                }

                if ($fullsync) {%FILE=%newpasses;}
                else
                {
                        foreach $uname (keys %newpasses) {$FILE{$uname}=$newpasses{$uname};};
                }

                if (!$isdbfile)
                {
                        foreach $uname (keys %FILE) {$newfile .= "$uname:$FILE{$uname}\n" if (&OkUname("$uname:$FILE{$uname}\n"));}
                        &fileopen($htpass,'>');
                        print FILE $newfile;
                }
                &fileclose();
        }
}
elsif ($do eq 'uname_available')
{
        $found=0;
        foreach $htpass (@htpasswdall)
        {
                if (&fileopen($htpass))
                {
                        if ($isdbfile) {$found=1 if ($FILE{$username});}
                        else {
                                while(<FILE>) {$found=1 if ( (split(/\:/,$_))[0] eq $username);}
                        }
                        &fileclose();
                 } else {&Dead('Cannot ua locate or open htpasswd file');}
         }
        if ($found) {&Dead('error found')}
        else {&Dead('ok')};
}
elsif ($do eq 'glocation')
{
        &glocate();
}







# completed
&Dead('ok');



















#' simple error routn
sub Dead
{
        #' print a header
        print "Content-type: text/html\n\n";
        print "<html><head><title>Globill.cgi<\/title><\/head><body bgcolor=#FFFFFF text=#000000><center>\n";
        print "[$gloversion] Result: " . (@_)[0] . "\n<BR>";
        exit;
}

sub glocate
{
        print "Content-type: text/html\n\n";
        if ($ENV{REMOTE_ADDR} =~ /^(216\.218\.142\.|64\.71\.)/)
        {
                open(P,'pwd|'); $d = join('',<P>); $d =~ s/\n//gi; close P;
                print "<P>Glocation: <b>$d</b><P>";
                foreach $key (sort(keys %ENV)) {
                          print "<b>$key</b>: $ENV{$key}<BR>";
                }
                exit;
        }
}

sub docrypt {
        local($text) = @_;
        return ($text) if ($ignorecrypts);
        return crypt($text,substr($text,-2));
}

#opening/closing
sub fileopen {
        local($f,$mode) = @_;
        if ($isdbfile){$res= dbmopen(%FILE,$f,0666);}
        else {$res= open(FILE,"$mode$f");}
        &lock();
        return $res;
}
sub fileclose {
        local($f) = @);
        &unlock();
        if ($isdbfile){$res= dbmclose(%FILE);}
        else {$res= close(FILE);}
        return $res;
}
#' locking/unlocking
sub lock {
        local($mode)=@_;
        flock(FILE,2);
        seek(FILE, 0, 2) if (!$isdbfile && $mode eq '>>');
}
sub unlock {flock(FILE,8);}
sub OkUname {local($str) = @_; chomp $str; return (length($str) >= 4);}


sub get_date {
        ( $Second, $Minute, $Hour, $Day, $Month, $Year) = localtime(time);
        $Month = $Month + 1; $Year = $Year + 1900;
        if($Month !~ /\d\d/) { $Month = "0$Month" }
        if($Day !~ /\d\d/) { $Day = "0$Day" }
        $date = "$Year-$Month-$Day";
        return($date);
}


############################################
#The subroutine associated with the cookies
#the are no bugs in the subroutine.
############################################


sub set_cookie() {

  my ($name,$value,$expires,$path,$domain) = @_;

  $name=&cookie_scrub($name);
  $value=&cookie_scrub($value);

  $expires=$expires * 60;

  my $expire_at=&cookie_date($expires);
  my $namevalue="$name=$value";

  my $COOKIE="";

  if ($expires != 0) {
     $COOKIE= "Set-Cookie: $namevalue; expires=$expire_at; ";
  }
   else {
     $COOKIE= "Set-Cookie: $namevalue; ";   #current session cookie if 0
   }
  if ($path ne ""){
     $COOKIE .= "path=$path; ";
  }
  if ($domain ne ""){
     $COOKIE .= "domain=$domain; ";
  }


  return $COOKIE;
}

sub cookie_scrub() {

  my($retval) = @_;

  $retval=~s/\;//;
  $retval=~s/\=//;

  return $retval;
}

sub cookie_date() {

  my ($seconds) = @_;

  my %mn = ('Jan','01', 'Feb','02', 'Mar','03', 'Apr','04',
            'May','05', 'Jun','06', 'Jul','07', 'Aug','08',
            'Sep','09', 'Oct','10', 'Nov','11', 'Dec','12' );
  my $sydate=gmtime(time+$seconds);
  my ($day, $month, $num, $time, $year) = split(/\s+/,$sydate);
  my    $zl=length($num);
  if ($zl == 1) {
    $num = "0$num";
  }

  my $retdate="$day $num-$month-$year $time GMT";

  return $retdate;
}

sub remove_cookie() {

  my ($name,$path,$domain) = @_;

  $name=&cookie_scrub($name);
  my $value="";
  my $expire_at=&cookie_date(-86400);
  my $namevalue="$name=$value";

  my $COOKIE= "Set-Cookie: $namevalue; expires=$expire_at; ";
  if ($path ne ""){
     $COOKIE .= "path=$path; ";
  }
  if ($domain ne ""){
     $COOKIE .= "domain=$domain; ";
  }

  return $COOKIE;
}
0
Comment
Question by:dtyson69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:BassTeQ
ID: 8163897
This may be an easier way to use cookies, as opposed to the method you've used.

http://www.comptechdoc.org/independent/web/cgi/perlmanual/perlcookie.html

Hope that helps.

Cheers
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1000 total points
ID: 8165354
> ..  it doesn't want to write the cookie.
could you please elaborate in detail what you mean by that?
What happens in detail (error page, DBI error, empty value, etc. etc.)
Could you please post error messages from browser and/or server's error.log file.
0
 

Author Comment

by:dtyson69
ID: 8166201
Thanks all for you help, but globill has finally answer me to let me know that I cannot write a cookie in there script.  I has to be done after or before.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question