in reply to CGI:Cookie, Apache2, Mod_perl2

You have to include the cookie in the response header, e.g. with $cgi->header( -cookie => $new_cookie_product )  (where $cgi is your CGI.pm or CGI::Simple object),  or $req->headers_out->add('Set-Cookie' => $new_cookie_product)  (where $req is your Apache/mod_perl request object).

Update: use err_headers_out (as suggested by lihao) instead of headers_out, if you want to preserve the cookie on errors / internal redirects.

___

P.S. please put <c> ... </c> tags around your code section   (this preserves indentation, handles PM-special characters like [] properly, etc...)

Replies are listed 'Best First'.
Re^2: CGI:Cookie, Apache2, Mod_perl2
by overworked (Novice) on Jun 13, 2008 at 19:29 UTC
    Hello,
    I was including

    use CGI;
    my $query = new CGI();
    which I would use in the same example you gave me
    $query->headers_out->add('Set-Cookie' => $new_cookie_product)
    do I need to include the CGI:Simple?

      Calling headers_out on a CGI object won't work...  You didn't say whether you're using CGI-style scripts (via ModPerl::Registry) or the mod_perl API, so I mentioned both variants.  In the latter case, you don't need CGI (nor CGI::Simple), i.e. you'd simply call headers_out on the object provided by Apache2::RequestRec.

      Also see Generating HTTP Response Headers

        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;