CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Cookies 2

 



kidd
User

Feb 28, 2002, 6:29 PM

Post #1 of 10 (4581 views)
Cookies 2 Can't Post

Can any one help with a CGI that checks how many times the user logged in on my site, and after the fifth time he's redirected to anothe page...

"So you want to be a cowboy
Well you know it's more than just a ride
Guess you got to know the real thing
If you want to know the other side
Ain't nobody riding shotgun
In this world tonight"


Paul
Enthusiast

Mar 1, 2002, 2:58 AM

Post #2 of 10 (4576 views)
Re: [kidd] Cookies 2 [In reply to] Can't Post

[perl]

use CGI qw(:standard);

my $times = cookie('Cookie_Name') || 0;

$times > 5 ? redirect('some_url') : set_cookie();

[/perl]

Im assuming you know how to set a cookie?


(This post was edited by RedRum on Mar 1, 2002, 2:59 AM)


yapp
User

Mar 3, 2002, 12:57 PM

Post #3 of 10 (4566 views)
Re: [RedRum] Cookies 2 [In reply to] Can't Post

...This should actually be posted in the beginner/immediate forum...

I throught this does the trick:
[perl]
use CGI qw(:cgi);
my $times = cookie('Cookie_Name') || 0;

if($times > 5) {
print redirect('some_url'); # Your redirect needs to be printed to the browser.
exit;
}
else {
set_cookie();
}

sub set_cookie
{
my $cookieObj = cookie(-name => 'Cookie_Name', -value=> $times);
print header( -cookie => $cookieObj ); # Send cookie with HTTP header
}
[/perl]

btw, use CGI qw(:cgi); would be enough for CGI programs. (unless you want to generate HTML through the CGI.pm

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

(This post was edited by yapp on Mar 3, 2002, 12:59 PM)


Paul
Enthusiast

Mar 3, 2002, 1:04 PM

Post #4 of 10 (4562 views)
Re: [yapp] Cookies 2 [In reply to] Can't Post

You don't need an exit if you are redirecting.


yapp
User

Mar 4, 2002, 8:21 AM

Post #5 of 10 (4558 views)
Re: [RedRum] Cookies 2 [In reply to] Can't Post

is it? I thought your script doesn't need any processing anymore.. (I always prepare everything first, then print stuff from the dat and exit)

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


Jasmine
Administrator

Mar 4, 2002, 11:09 AM

Post #6 of 10 (4550 views)
Re: [yapp] Cookies 2 [In reply to] Can't Post

That's correct. You don't need exit, because after successful completion of the program, it exits anyway. You can use exit to prematurely quit a program (that is, to not run the rest of it), but I always recommend writing programs so that it doesn't need to exit.

Also, if memory serves, you should never use exit in a program that's supposed to run on mod_perl. One of the mod_perl gurus can confirm/deny this Smile


Paul
Enthusiast

Mar 4, 2002, 11:37 AM

Post #7 of 10 (4547 views)
Re: [Jasmine] Cookies 2 [In reply to] Can't Post

>>you should never use exit in a program that's supposed to run on mod_perl<<

Correct. Although if you _must_ use an exit you should use:

Apache->exit;


yapp
User

Mar 6, 2002, 5:54 AM

Post #8 of 10 (4540 views)
Re: [Jasmine] Cookies 2 [In reply to] Can't Post

Oeps! I forgot that. I'm still planning to find out more about mod_perl, and do something with it...

- Can the exit be overwritten?

- my print_footer_HTML() function exits my program by default.
- in the main loop, an error page will be displayed when an invalid show= option has been passed through (script.cgi?show=x)

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


Jasmine
Administrator

Mar 6, 2002, 9:08 AM

Post #9 of 10 (4537 views)
Re: [yapp] Cookies 2 [In reply to] Can't Post

Yep, you can override any built-in function with use subs

[perl]use subs qw/ exit /;

sub exit {
print "hello";
}

exit; # prints hello[/perl]

Then later, if you wanted to use the overridden function as it was before you changed it, just prepend CORE:: to the sub name, like so: CORE::exit;.


yapp
User

Mar 6, 2002, 11:12 PM

Post #10 of 10 (4530 views)
Re: [Jasmine] Cookies 2 [In reply to] Can't Post

OK, then I'll make my own exit, that does a Apache->exit for mod_perl.
How can I test for mod_perl? Can you show me some code?

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives