Reputation: 1465
Congrats to me. I've officially make the switch from asp.net to PHP.
Now, I'm stuck. :-)
I have some code that I found on some code snippets site and have a question about the following piece.
if (isset($_POST['doRegCheck']))
: if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck']))
: redirect_to("index.php?do=users");
endif;
endif;
Can anyone tell me exactly what is going here? Does this code check the value of the doRegCheck POST variable to make sure it's not empty?
Thanks in advance.
Upvotes: 0
Views: 84
Reputation: 92274
The code will redirect only if the HTTP headers contain a variable called doRegCheck
and that variable is set to 0 (?doRegCheck=0
), or nothing (?doRegCheck=
). That seems backwards to me. I would think you would redirect if the variable is present and it's not set to falsy values like 0,false,F
That syntax (colons) is usually used in templates (when you're switching in and out of PHP mode), makes it harder to read.
Upvotes: 1
Reputation: 46602
The current form makes the readability harder as it is, it should be:
if (isset($_POST['doRegCheck'])):
if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])):
redirect_to("index.php?do=users");
endif;
endif;
Which translates to:
if (isset($_POST['doRegCheck'])){
if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])){
redirect_to("index.php?do=users");
}
}
Meaning:
//is $_POST['doRegCheck'] set
if (isset($_POST['doRegCheck'])){
//intval will return (int)0 even if its abc,
// perhaps it should be is_numeric() as it will
// pass if the value is 0 or the value is abc or empty
if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])){
//some user function to redirect
redirect_to("index.php?do=users");
}
}
Upvotes: 0
Reputation: 1
Yes, you have the right idea, if 'doRegCheck' is set, but equal to '0' or has no value '' then it will redirect you to 'index.php?do=users'. So as far as I can tell it's basically making sure that if you hit say a registration page without a confirmation it will not complete the registration process.
Upvotes: 0
Reputation: 522081
First, let's reformat this properly:
if (isset($_POST['doRegCheck'])) :
if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])) :
redirect_to("index.php?do=users");
endif;
endif;
Then, let's take out the redundancies. empty
basically includes a check for isset
. empty
will also recognize "0"
as "empty". empty
recognizes any false value or non-existent value as "empty". This code may try to check of the value is numeric by using intval
. So to summarize, this should be more comprehensible:
if (empty($_POST['doRegCheck']) || !is_numeric($_POST['doRegCheck'])) {
redirect_to("index.php?do=users");
}
Upvotes: 1