For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > Why does variable value change?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Why does variable value change?
Don

2005-04-24, 3:56 am

I don't have much experience with Perl, so I'm sure the problem is probably
pretty obvious to many of you. But, when I run the following GCI script I
find the "if" tests on "loggedInCookie"/value and
"usernamePasswordPassedCookie"/value both pass the "if". But, the last "if"
shows that both "havelogin" and "haveusernameandpassword" are "false".
What's going on here?

Thanks for any help,
Don

..
..
..
# Check to see if client has logged in and passed Username/Password test.
#
my @cookiepairs=split(/;/, $ENV{'HTTP_COOKIE'});
my ($pair, $name, $value, %cookie);
my $havelogin = 'false';
my $haveusernameandpassword = 'false';
foreach $pair (@cookiepairs)
{
($name, $value) = split(/=/, $pair);
if($name == 'loggedInCookie' and $value == 'true')
{
$havelogin = 'true';
}
if($name == 'usernamePasswordPassedCookie' and $value == 'true')
{
$haveusernameandpassword = 'true';
}
}
if($havelogin == 'false' or $haveusernameandpassword == 'false')
{
# html code will go here stating user not logged in.
exit;
}
# Continue here if login is ok.
..
..
..


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
xhoster@gmail.com

2005-04-24, 3:56 am

Don <no@adr.com> wrote:
> I don't have much experience with Perl, so I'm sure the problem is
> probably pretty obvious to many of you. But, when I run the following
> GCI script I find the "if" tests on "loggedInCookie"/value and
> "usernamePasswordPassedCookie"/value both pass the "if". But, the last
> "if" shows that both "havelogin" and "haveusernameandpassword" are
> "false". What's going on here?


"==" compares it's arguments for numeric equality. You want "eq", which
compares them for string equality.

> if($havelogin == 'false' or $haveusernameandpassword == 'false')



Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Axel

2005-04-24, 8:56 am

Don <no@adr.com> wrote:
> I don't have much experience with Perl, so I'm sure the problem is probably
> pretty obvious to many of you. But, when I run the following GCI script I
> find the "if" tests on "loggedInCookie"/value and
> "usernamePasswordPassedCookie"/value both pass the "if". But, the last "if"
> shows that both "havelogin" and "haveusernameandpassword" are "false".
> What's going on here?


If you had enabled warnings (as guidelines for this group specifically
mention), the problem would have been made obvious.

Hint: String comparisons and numeric comparisons do not use the same
operator.

Axel

John Bokma

2005-04-24, 8:56 am

Axel wrote:

> Don <no@adr.com> wrote:
>
> If you had enabled warnings (as guidelines for this group specifically
> mention), the problem would have been made obvious.
>
> Hint: String comparisons and numeric comparisons do not use the same
> operator.


Is the OP going to:

[1] enable warnings
[2] change the comparison
[3] both
[4] neither

:-D.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

John Bokma

2005-04-24, 8:56 am

Don wrote:

> # Check to see if client has logged in and passed Username/Password
> test. #
> my @cookiepairs=split(/;/, $ENV{'HTTP_COOKIE'});
> my ($pair, $name, $value, %cookie);
> my $havelogin = 'false';
> my $haveusernameandpassword = 'false';


The problem with string constants as used here is that a typo can
introduce behaviour that will take you quite some time to pin down.

> foreach $pair (@cookiepairs)
> {
> ($name, $value) = split(/=/, $pair);
> if($name == 'loggedInCookie' and $value == 'true')
> {
> $havelogin = 'true';
> }
> if($name == 'usernamePasswordPassedCookie' and $value ==
> 'true') {
> $haveusernameandpassword = 'true';
> }
> }
> if($havelogin == 'false' or $haveusernameandpassword == 'false')
> {
> # html code will go here stating user not logged in.
> exit;
> }
> # Continue here if login is ok.


This code gives me the creepy feeling that anyone setting
usernamePasswordPassedCookie to true is logged in...

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Tad McClellan

2005-04-24, 8:56 am

Don <no@adr.com> wrote:

> I don't have much experience with Perl,



Then you should ask for all the help you can get!


> so I'm sure the problem is probably
> pretty obvious to many of you.

^^^^^^

The problem is even obvious to a _machine_.

If you ask it to, the machine will tell you where your problem is...


> What's going on here?



You aren't working smart.


> if($name == 'loggedInCookie' and $value == 'true')



You should always enable warnings when developing Perl code!


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Axel

2005-04-24, 8:56 am

John Bokma <postmaster@castleamber.com> wrote:
[color=darkred]
[color=darkred]
[color=darkred]
> Is the OP going to:
>
> [1] enable warnings
> [2] change the comparison
> [3] both
> [4] neither


Who knows? I am going to have a beer :)

Axel

Don

2005-04-24, 3:56 pm

On Sun, 24 Apr 2005 01:54:02 -0500, Axel <axel@white-eagle.invalid.uk>
wrote:

>Don <no@adr.com> wrote:
>
>If you had enabled warnings (as guidelines for this group specifically
>mention), the problem would have been made obvious.
>
>Hint: String comparisons and numeric comparisons do not use the same
>operator.
>
>Axel

I have "use warnings" at the top of the module. But, all I get is "500
server error" when I run the script on the server.

I changed the operators as suggested, but still get that error message, and
no warnings from Perl.

Don

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Randal L. Schwartz

2005-04-24, 3:56 pm

*** post for FREE via your newsreader at post.newsfeed.com ***
[color=darkred]
(Please don't use "adr.com" if it doesn't belong to you.)

Don> # Check to see if client has logged in and passed Username/Password test.
Don> #
Don> my @cookiepairs=split(/;/, $ENV{'HTTP_COOKIE'});

Please don't do this. Use CGI.pm, and the "cookie" method there.
Otherwise, you will incorrectly declare an encoded value of "%74rue"
to be false, even though technically it's true. (A browser could
choose to hexify every character of an incoming cookie at its
discretion.)

print "Just another Perl hacker,"; # the original!

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

Tad McClellan

2005-04-24, 3:56 pm

Don <no@adr.com> wrote:

> I have "use warnings" at the top of the module. But, all I get is "500
> server error" when I run the script on the server.



Did you look in your server logs? That is where STDERR goes
for CGI programs.

Did you try running your program from the command line rather than
from the CGI environment?

Have you read the answser to your Frequently Asked Question already?

perldoc -q 500

My CGI script runs from the command line but not the browser. (500
Server Error)

Have you checked out the Perl FAQs related to CGI programming with Perl?

perldoc -q CGI

Where can I learn about CGI or Web programming in Perl?

How can I get better error messages from a CGI program?


> I changed the operators as suggested, but still get that error message, and
> no warnings from Perl.



Show the code.

Show the messages.

We are not very good mind readers...


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
John Bokma

2005-04-24, 8:56 pm

Axel wrote:

> Who knows? I am going to have a beer :)


Cheers :-D

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Gunnar Hjalmarsson

2005-04-25, 3:57 am

Randal L. Schwartz wrote:
> Don writes:
>
> Please don't do this. Use CGI.pm, and the "cookie" method there.
> Otherwise, you will incorrectly declare an encoded value of "%74rue"
> to be false, even though technically it's true. (A browser could
> choose to hexify every character of an incoming cookie at its
> discretion.)


Which standard allows browsers to send anything in a Cookie header but
what it once received via a Set-Cookie header?

Do you know of any browser that actually does it?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Tad McClellan

2005-04-25, 3:58 am

Don <no@adr.com> wrote:

> Subject: Why does variable value change?



Because if it didn't change, then it wouldn't be "variable",
it would be "constant".

:-)


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Kevin Michael Vail

2005-04-25, 3:58 am

In article <slrnd6ofpq.p5e.tadmc@magna.augustmail.com>,
Tad McClellan <tadmc@augustmail.com> wrote:

> Don <no@adr.com> wrote:
>
>
>
> Because if it didn't change, then it wouldn't be "variable",
> it would be "constant".
>
> :-)


But it has to *want* to change.

Oh, wait, wrong joke.
--
Kevin Michael Vail | a billion stars go spinning through the night,
kevin@vaildc.net | blazing high above your head.
. . . . . . . . . | But _in_ you is the presence that
. . . . . . . . | will be, when all the stars are dead.
. . . . . . . . . | (Rainer Maria Rilke)
Joe Smith

2005-04-25, 3:58 am

Randal L. Schwartz wrote:

> (Please don't use "adr.com" if it doesn't belong to you.)


Don, since your email address is at adr.com, do you work for
JPMorgan Chase Bank or for Thomson Financial?
Bart Lateur

2005-04-25, 8:58 am

Don wrote:

>I have "use warnings" at the top of the module. But, all I get is "500
>server error" when I run the script on the server.
>
>I changed the operators as suggested, but still get that error message, and
>no warnings from Perl.


You don't debug a CGI script by trying to guessthe source of "server
errors". That's like searching for a needle in a haystack. Instead, rely
on Perl's own error messages, in the error log. CGI::Carp's
"fatalsToBrowser" can help, sending error messages to the browser
instead. (but do this only for debugging.)

See Tye McQueen's "warningsToBrowser", to extend the mechanism to
warnings, not just fatal errors.

<http://www.perlmonks.org/?node=115286>

--
Bart.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com