Hello people!
How !isset in Ruby?
example php
<?php
if(!isset($MY_UID))
{
//bla-bla
}
else
{
//bla-bla
}
?>
···
--
Posted via http://www.ruby-forum.com/.
Hello people!
How !isset in Ruby?
example php
<?php
if(!isset($MY_UID))
{
//bla-bla
}
else
{
//bla-bla
}
?>
--
Posted via http://www.ruby-forum.com/.
What should !isset do? It's not obvious that we know php
2008/6/15, Alexey Tafintsev <alexey@goldtovar.com>:
Hello people!
How !isset in Ruby?example php
<?php
if(!isset($MY_UID))
{
//bla-bla
}
else
{
//bla-bla
}
?>--
Posted via http://www.ruby-forum.com/\.
you can use defined?, however it will also hit on constants (classes), methods
and probably more ..:
if (!defined?($my_uid)) then
else
end
On Sunday 15 June 2008 11:56:54 Alexey Tafintsev wrote:
Hello people!
How !isset in Ruby?example php
<?php
if(!isset($MY_UID))
{
//bla-bla
}
else
{
//bla-bla
}
?>
Oscar Del ben wrote:
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();
}
}
}
?>
--
Posted via http://www.ruby-forum.com/\.
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
On 15.06.2008 14:37, Martin Boese wrote:
you can use defined?, however it will also hit on constants (classes), methods and probably more ..:
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
On Jun 15, 2008, at 1:34 PM, Alexey Tafintsev wrote:
Oscar Del ben wrote:
What should !isset do? It's not obvious that we know php
Oscar I need Basic Authenticate! I use Ruby CGI!
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
--
Posted via http://www.ruby-forum.com/\.
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
----------------------------
--
Posted via http://www.ruby-forum.com/.
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']
--
Posted via http://www.ruby-forum.com/\.
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
---------------------
--
Posted via http://www.ruby-forum.com/.
Hi --
On Mon, 16 Jun 2008, Alexey Tafintsev wrote:
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
--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!
If I'm reading your code correctly, I think your use of defined? is wrong. The
user local variable will always be defined, because you assign to it. If ENV
doesn't contain the 'AUTH_USER' entry, user will be nil, but it will be
defined. If you want to check whether the 'AUTH_USER' environment variable
exists, you can use
ENV.has_key?('AUTH_USER')
or
ENV['AUTH_USER'].nil?
If you try running your code, you'll see that the branch corresponding to
'AUTH_USER' being undefied is never executed, whether that environment
variable exists or not.
Stefano
On Sunday 15 June 2008, Alexey Tafintsev wrote:
Thank you Marc!!!! I rewrite this example :-)))
---------------------
#!c:/ruby/bin/ruby.exe$kcode = "windows-1251"
require 'cgi'
cgi = CGI.newdef auth_lang
myauthlang = ENV['HTTP_ACCEPT_LANGUAGE']
case myauthlang
when /ru/
alang = "Ошибка 401: Необходима авторизация"
else
alang = "Error 401: Authorization Required"
end
enduser = 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
---------------------
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]";
--
Posted via http://www.ruby-forum.com/.
Thank you Stefano :-)))
--
Posted via http://www.ruby-forum.com/.
Hi --
On Mon, 16 Jun 2008, Alexey Tafintsev wrote:
real_password=password
if real_password!=passwordThis 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
--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!
David, How you be written this code?
If user = "test" and userpassword = "test" without database query, only
local check.
--
Posted via http://www.ruby-forum.com/.
Hi --
On Mon, 16 Jun 2008, Alexey Tafintsev wrote:
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
--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!
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
---------------------
--
Posted via http://www.ruby-forum.com/.
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
On Sunday 15 June 2008, Alexey Tafintsev wrote:
Hello again!
This code don't work! Please, help...---------------------
#!c:/ruby/bin/ruby.exe$kcode = "windows-1251"
require 'cgi'
cgi = CGI.newlogin_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
endif (!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
---------------------
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']???
--
Posted via http://www.ruby-forum.com/.