How !isset in Ruby

A

Alexey Tafintsev

Hello people!
How !isset in Ruby?

example php
<?php
if(!isset($MY_UID))
{
//bla-bla
}
else
{
//bla-bla
}
?>
 
O

Oscar Del Ben

[Note: parts of this message were removed to make it a legal post.]

What should !isset do? It's not obvious that we know php
 
A

Alexey Tafintsev

Oscar said:
What should !isset do? It's not obvious that we know php
Oscar I need Basic Authenticate! I use Ruby CGI!


<?php

if(!isset($PHP_AUTH_USER))
// user unknown
{
Header("WWW-Authenticate: Basic realm=\"Admin Center\"");
Header("HTTP/1.0 401 Unauthorized");
exit();
}
else
// User - Ok, unknown password
{
// password insert
$password = "$PHP_AUTH_PW";
// проÑмотр базы Ð´Ð»Ñ Ð¿Ð¾Ð»ÑƒÑ‡ÐµÐ½Ð¸Ñ Ñ€ÐµÐ°Ð»ÑŒÐ½Ð¾Ð³Ð¾ паролÑ
$link = mysql_connect($dbhost, $dbuser, $dbpasswd);
mysql_select_db($dbname);
$result=mysql_query("SELECT password FROM auth WHERE
name=\"$PHP_AUTH_USER\"");
$row=mysql_fetch_array($result);
// проверка
if ($row==NULL) // in DB user unknown
{
Header("WWW-Authenticate: Basic realm=\"Admin Center\"");
Header("HTTP/1.0 401 Unauthorized");
exit();
}
else // In DB User Ok, check password
{
$real_password="$row[password]";
if ($real_password!=$password)
{
Header("WWW-Authenticate: Basic realm=\"Admin Center\"");
Header("HTTP/1.0 401 Unauthorized");
exit();
}
}
}

?>
 
F

Florian Gilcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Oscar I need Basic Authenticate! I use Ruby CGI!

What Oscar tried to tell you: while some of us know PHP, we are not
here to read it. It seems like you have a specific problem you need
solved. So please specify the problem in english, not in PHP (which is
commented in kyrillic...).

It just might be that the Ruby Solution could be very different from
one in PHP.

Regards,
Florian Gilcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkhVCa0ACgkQJA/zY0IIRZYFJACgw2ZRMP0T5i/LP/+k+/pwp3WY
teEAn3lFjY5yq+0/nyX/3JgEBOBY74ym
=fB/D
-----END PGP SIGNATURE-----
 
T

Tor Erik Linnerud

How !isset in Ruby?

Judging by http://uk2.php.net/isset, !(var.nil? rescue true) will
achieve something similar to !isset().

var.nil? rescue true
=> true

var = 123
var.nil? rescue true
=> false

var = nil
var.nil? rescue true
=> true

But I am not sure that it makes any sense when used with the Ruby CGI
library. An exception will be raised if a variable which doesn't exist
is accessed. All variables are generally evaluated to true, except for
those set to false or nil. If you want more help you have to tell us
what your problem is more exactly.

Tor Erik
 
M

Martin Boese

you can use defined?, however it will also hit on constants (classes), methods
and probably more ..:


if (!defined?($my_uid)) then

else

end
 
R

Robert Klemme

you can use defined?, however it will also hit on constants (classes), methods
and probably more ..:

For global variables testing for nil is probably better because they are
always and implicitly defined:

robert@fussel ~
$ ruby -e 'p $foo'
nil

robert@fussel ~
$ ruby -e 'p foo'
-e:1: undefined local variable or method `foo' for main:Object (NameError)

robert@fussel ~
$ ruby -e 'p @foo'
nil

Cheers

robert
 
A

Alexey Tafintsev

THANKS MANS !!!!
Working :))))

----------------------------
#!c:/ruby/bin/ruby.exe

$kcode = "windows-1251"

require 'cgi'
cgi = CGI.new

user = ENV['AUTH_USER']
password = ENV['AUTH_PASSWD']

if (!defined?(ENV['AUTH_USER'])) then
puts cgi.header({"Status" => "401 Authorization Required",
"Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web
Password\""})
else
if user == nil
puts cgi.header({"Status" => "401 Authorization Required",
"Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web
Password\""})
else
real_password=password
if real_password!=password
puts cgi.header({"Status" => "401 Authorization Required",
"Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web
Password\""})
end
end
end
 
M

Marc Heiler

if (!defined?(ENV['AUTH_USER'])) then

you can omit the "then"

and you dont need the enclosing ()




if ! defined?(ENV['AUTH_USER'])

but i think this here might be cleaner



unless defined? ENV['AUTH_USER']
 
A

Alexey Tafintsev

Thank you Marc!!!! I rewrite this example :)))
---------------------
#!c:/ruby/bin/ruby.exe

$kcode = "windows-1251"

require 'cgi'
cgi = CGI.new

def auth_lang
myauthlang = ENV['HTTP_ACCEPT_LANGUAGE']
case myauthlang
when /ru/
alang = "Ошибка 401: Ðеобходима авторизациÑ"
else
alang = "Error 401: Authorization Required"
end
end

user = ENV['AUTH_USER']
password = ENV['AUTH_PASSWD']

unless defined? user
puts cgi.header({"Status" => "401 Authorization Required",
"Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web
Password\""})
puts auth_lang
else
if user == nil
puts cgi.header({"Status" => "401 Authorization Required",
"Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web
Password\""})
puts auth_lang
else
real_password=password
if real_password!=password
puts cgi.header({"Status" => "401 Authorization Required",
"Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web
Password\""})
puts auth_lang
end
end
end
---------------------
 
D

David A. Black

Hi --

real_password=password
if real_password!=password

I haven't looked at your code carefully but those two lines sort of
jumped out at me. Do you mean them to go in the opposite order?


David
 
S

Stefano Crocco

Thank you Marc!!!! I rewrite this example :)))
---------------------
#!c:/ruby/bin/ruby.exe

$kcode =3D "windows-1251"

require 'cgi'
cgi =3D CGI.new

def auth_lang
myauthlang =3D ENV['HTTP_ACCEPT_LANGUAGE']
case myauthlang
when /ru/
alang =3D "=D0=9E=D1=88=D0=B8=D0=B1=D0=BA=D0=B0 401: =D0=9D=D0=
=B5=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=D0=B0 =D0=B0=D0=B2=D1=82=D0=
=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F"
else
alang =3D "Error 401: Authorization Required"
end
end

user =3D ENV['AUTH_USER']
password =3D ENV['AUTH_PASSWD']

unless defined? user
puts cgi.header({"Status" =3D> "401 Authorization Required",
"Type" =3D> "text/html", "WWW-Authenticate" =3D> "Basic realm=3D\"Web
Password\""})
puts auth_lang
else
if user =3D=3D nil
puts cgi.header({"Status" =3D> "401 Authorization Required",
"Type" =3D> "text/html", "WWW-Authenticate" =3D> "Basic realm=3D\"Web
Password\""})
puts auth_lang
else
real_password=3Dpassword
if real_password!=3Dpassword
puts cgi.header({"Status" =3D> "401 Authorization Required",
"Type" =3D> "text/html", "WWW-Authenticate" =3D> "Basic realm=3D\"Web
Password\""})
puts auth_lang
end
end
end
---------------------

If I'm reading your code correctly, I think your use of defined? is wrong. =
The=20
user local variable will always be defined, because you assign to it. If EN=
V=20
doesn't contain the 'AUTH_USER' entry, user will be nil, but it will be=20
defined. If you want to check whether the 'AUTH_USER' environment variable=
=20
exists, you can use

ENV.has_key?('AUTH_USER')

or=20

ENV['AUTH_USER'].nil?

If you try running your code, you'll see that the branch corresponding to=20
'AUTH_USER' being undefied is never executed, whether that environment=20
variable exists or not.

Stefano
 
A

Alexey Tafintsev

Hello David!!!
My question for this topic "How !isset in Ruby?"
Answer: !defined? or unless defined?
Thanks mans :)))

real_password=password
if real_password!=password

This for DataBase check, see example in php! :))))

$password = "$PHP_AUTH_PW";
$link = mysql_connect($dbhost, $dbuser, $dbpasswd);
mysql_select_db($dbname);
$result=mysql_query("SELECT password FROM auth WHERE
name=\"$PHP_AUTH_USER\"");
$row=mysql_fetch_array($result);

$real_password="$row[password]";
 
D

David A. Black

Hi --

real_password=password
if real_password!=password

This for DataBase check, see example in php! :))))

My point is that since you have set real_password to password in the
first line, there is no point checking to see whether they're equal in
the second line, because you already know they are. The body of that
'if' statement will never be executed.


David
 
A

Alexey Tafintsev

David, How you be written this code?
If user = "test" and userpassword = "test" without database query, only
local check.
 
D

David A. Black

Hi --

David, How you be written this code?
If user = "test" and userpassword = "test" without database query, only
local check.

You probably mean == rather than =, I think.


David
 
A

Alexey Tafintsev

Hello again!
This code don't work! Please, help...

---------------------
#!c:/ruby/bin/ruby.exe

$kcode = "windows-1251"

require 'cgi'
cgi = CGI.new

login_successful = false

if ENV['AUTH_USER'].nil? and ENV['AUTH_PASSWD'].nil?
user = ENV['AUTH_USER']
pass = ENV['AUTH_PASSWD']
else if user == 'test' and pass == 'test'
login_successful = true
end
end

if (!login_successful)
puts cgi.header({"Status" => "401 Authorization Required", "Type" =>
"text/html", "WWW-Authenticate" => "Basic realm=\"Web Password\""})
puts "401 Authorization Required"
else
puts cgi.header
puts "OK"
end
 
S

Stefano Crocco

Hello again!
This code don't work! Please, help...

---------------------
#!c:/ruby/bin/ruby.exe

$kcode = "windows-1251"

require 'cgi'
cgi = CGI.new

login_successful = false

if ENV['AUTH_USER'].nil? and ENV['AUTH_PASSWD'].nil?
user = ENV['AUTH_USER']
pass = ENV['AUTH_PASSWD']
else if user == 'test' and pass == 'test'
login_successful = true
end
end

if (!login_successful)
puts cgi.header({"Status" => "401 Authorization Required", "Type" =>
"text/html", "WWW-Authenticate" => "Basic realm=\"Web Password\""})
puts "401 Authorization Required"
else
puts cgi.header
puts "OK"
end
---------------------

It would be helpful saying what exactly isn't working (that is: what did you
expect and what instead happened, which error message you got, if any, and so
on).

At any rate, I think there's a mistake in the first if. The line

if ENV['AUTH_USER'].nil? and ENV['AUTH_PASSWD'].nil?

tests that neither the AUTH_USER nor the AUTH_PASSWD variables are defined, In
the following two lines, you put the values of the two environment variables
(which, remember, don't exist) in the two variables user and pass. This means
that both user and pass will always be nil. Then, there's the fact that you
don't use those two variables (but maybe, the code you show here isn't
complete).

By the way, in ruby the structure of an if-else expression is:

if ...
...
elsif ...
...
else
...
end

Note that the third line is elsif, not else if. I don't think this can cause
problems in your situation, but I suggest you to change it all the same.

I hope this helps

Stefano
 
A

Alexey Tafintsev

Stefano!
No visual and log error! I enter login and password "test" in
authorization window, then push enter. Opens new window Basic
authorization. I dont see testing page "OK". Login and password don't
work.

if login and password == 'test'
puts cgi.header
puts "OK"
end

Maybe nedeed use Base64 -> ENV['HTTP_AUTHORIZATION']???????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top