Hello people!
How !isset in Ruby?
example php
<?php if(!isset($MY_UID)) { //bla-bla } else { //bla-bla } ?>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(); } } } ?>-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Jun 15, 2008, at 1:34 PM, Alexey T. 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!
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 G.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
iEYEARECAAYFAkhVCa0ACgkQJA/zY0IIRZYFJACgw2ZRMP0T5i/LP/+k+/pwp3WY
teEAn3lFjY5yq+0/nyX/3JgEBOBY74ym
=fB/D
-----END PGP SIGNATURE-----
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
you can use defined?, however it will also hit on constants (classes),
methods
and probably more …:
if (!defined?($my_uid)) then
else
end
On 15.06.2008 14:37, Martin B. wrote:
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
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
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’]
Hi –
On Mon, 16 Jun 2008, Alexey T. 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
#!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’]
On Sunday 15 June 2008, Alexey T. wrote:
myauthlang = ENV[‘HTTP_ACCEPT_LANGUAGE’]
puts auth_lang
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
Thank you Stefano :-)))
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]";
David, How you be written this code?
If user = “test” and userpassword = “test” without database query, only
local check.
Hi –
On Mon, 16 Jun 2008, Alexey T. 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
Hi –
On Mon, 16 Jun 2008, Alexey T. 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
On Sunday 15 June 2008, Alexey T. wrote:
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
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
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’]???
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs