Hello almut,

I totally applicate your time and effort in assisting me in this what I thought was a simple action.

But I must be missing something and its looking right at me but I'm perl blind.

So I have included the code I been using, please I know this is not the best approach on some things.

The code is not complete, I stopped due to not able to drop a cookie to the client browser,
after that I will check if cookie is valid and all that good stuff.
package Apache2::CookieClient;
use strict;
use CGI::Carp qw/fatalsToBrowser/;
use CGI::Cookie;
use Crypt::CBC; # Calls MD5 from inside the package
use DBI; # for SQL Server database connection
use CGI qw(:standard);
use Apache2::RequestRec (); # for $r->content_type
use Apache2::Connection (); # for $c->remote_ip
use Apache2::Const -compile => ':common';
sub handler
{
my $r = shift;
my $c = $r->connection();
my $uri = $r->uri(); #incoming URL
my $product = $r->dir_config('product'); #Var to grab product
## Cookie Vars.
my $query = new CGI();
my $your_host_name = $query->remote_host();
my $new_cookie_product;
my $retreived_cookie;
my $encryption_key = $product; #You make up this phrase
my $plaintext = $your_host_name . "::" . $product;
my $retrieved_decrypted;
my $cipher = new Crypt::CBC($encryption_key);
my $retval = 0;
my $skip_it = 0;


## When this cookie ever gets sent the browser
$retreived_cookie = cookie('<companyname>' . $product); # Grabs the cookie

## No Cookie, need to check if they have IP access
if (!$retreived_cookie) {
if ( check_ip($c->remote_ip(), $product) )
{
$new_cookie_product = new_cookie($product,$plaintext,$cipher);
### I have tried everything I have seen/read and I can't make it add a cookie to client browser
$r->headers_out->add('Set-Cookie' => $new_cookie_product);
#Send them on there marry way
return Apache2::Const::OK;
}
else # No IP matchs for that product, need to username login
{
#Send them to Login Page
return Apache2::Const::OK;
}
}


sub new_cookie {
my($product, $plaintext,$cipher) = @_;
my $ciphertext = $cipher->encrypt_hex($plaintext);
return new CGI::Cookie(
-name=>'<companyname>' . $product,
-value=>$ciphertext,
-path=>'/',
-expires=>''
);

}


sub check_ip
{
##Check IP to Product code, query request to SQL SERVER to authenicate
#returns a 0 = no access or a 1 = access granted
my ($ip, $product) = @_;
my $user;
my $conn = DBI->connect("DBI:Sybase:<servername>", "<username>", "<password>") || die DBI->errstr;
$conn->do("use <databasename>") || die DBI->errstr;
my $qry = "exec <StoredProcedure>'" . $ip . "','" . $product . "'";
my $smt = $conn->prepare($qry) || die DBI->errstr;
$smt->execute() || die DBI->errstr;
while(my $var = $smt->fetchrow_arrayref)
{
$user = $var->[0];
}

$smt = undef;
$conn->disconnect;
return $user;
}
}
1;

In reply to Re^4: CGI:Cookie, Apache2, Mod_perl2 by overworked
in thread CGI:Cookie, Apache2, Mod_perl2 by overworked

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.