Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

delete cookie

by bigup401 (Pilgrim)
on May 11, 2015 at 11:53 UTC ( [id://1126304]=perlquestion: print w/replies, xml ) Need Help??

bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

am trying to read cookie and delete it, bt seems there is a problem with my script. i can read the cookie very well. bt deleting it or expiring it is the problem i get

use CGI; my $cgi = CGI->new(); $sid = $cgi->cookie('$u'); $login = '#login'; $logout = '#index'; if ($sid) { $del = $cgi->delete($sid); print $cgi->redirect($logout); if (! $sid) { print $cgi->redirect($login); } }

Replies are listed 'Best First'.
Re: delete cookie
by Corion (Patriarch) on May 11, 2015 at 12:06 UTC

    Please help us help you better by showing us the following elements:

    Show us the part of your code where you delete the cookie.

    Please also show us the output including the headers which your script gives when deleting the cookie.

    Please also show us what your research has shown that is necessary to delete a cookie.

    Please also show us the steps that are necessary to change your script from writing the current headers to writing the headers that your research has shown you need.

    Please tell us where in the process you encounter problems.

Re: delete cookie (cgi101)
by Anonymous Monk on May 12, 2015 at 02:25 UTC

    bigup401 why would you expect "delete" as in https://metacpan.org/pod/CGI#Deleting-a-parameter-completely to touch cookies?

    Here is followup to cgi101

    sub NoCookiesNoMore { my( $q, $url ) = @_; my @names = $q->cookie; my @coo = map { $q->cookie( -name => $_, -value => [], -expires => '-10y', + ) } @names; $url and return $q->redirect( -location => $url, -cookie => [@coo] + ); return $q->redirect( -cookie => [@coo] ); } ## end sub NoCookiesNoMore __END__ $ perl GoToWellwithCookiesNoMore.cgi Status: 302 Found Set-Cookie: 1=; path=/; expires=Sat, 14-May-2005 02:28:08 GMT Set-Cookie: ro=; path=/; expires=Sat, 14-May-2005 02:28:08 GMT Set-Cookie: 3=; path=/; expires=Sat, 14-May-2005 02:28:08 GMT Set-Cookie: 5=; path=/; expires=Sat, 14-May-2005 02:28:08 GMT Date: Tue, 12 May 2015 02:28:08 GMT Location: http://localhost

      i got some idea and it worked for me. by overwriting the old cookie and then the browser will delete it because it has expired

      $page = '#page'; $cookie = $cgi->cookie(-name => 'user', -value => '', -expires=> '-1d' +); print $cgi->redirect(-location => $page, -cookie => $cookie);

      it works well now. can delete the old cookie by overwriting it

        Of course it worked, thats how HTTP works

Re: delete cookie
by Anonymous Monk on May 11, 2015 at 17:45 UTC
    Since cookie manipulation actually takes place on the browser, in response to HTTP headers, there might be a conflict between your redirect and set-cookie. Use a browser-side debugger to see exactly what HTTP headers and status-codes are being received, and exactly how the browser is reacting to them. (You can see the cookies, too.)

      yes i set the cookie well, and i can read it in browser, bt the problme is deleting or clearing it. this is it

      this is how i set my cookie and its working well.

      my $cookie = $cgi->cookie(-name => 'user', -value => 'user', -expires= +> '+1m'); print $cgi->redirect(-location => $nextpage -cookie => $cookie);

      and here i read my cookie

      $cookie = $cgi->cookie('user'); $page = '#page'; if (! $cookie) { print $cgi->redirect($page); } if ($cookie) { #continue }

      everything there is ok, bt the problem is clear the cookie (user)

        bigup401:

        I'm afraid I don't understand why you show us the code that works, but don't show the code you need help with. It just makes no sense to me.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1126304]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-25 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found