Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

Isset vrs != ""

Recommended Posts

I was told this wasn't affective, I didn't believe but I think I was shown first hand.
I was writing up some scripts, I wanted someone to have to enter an email address before gaining access
It is for a client that has yahoo server, and there php ini settings are aweful, register globals is on for one, a nother you can't see your errors, and unless you can log into there account you can't even see the error logs, it's a nightmare working with yahoo, anyway, I also had to cuss them out on the phone 2 times, for not giving proper tech support, for like 3 different clients, enough rambling, well I set up a gateway page, it was easier for me to keep up with, with yahoo server, I co uldn't debug when using a form on the same page, so I put it in another page, an dran all the link access through that.
I tried with my form at first like this
if (isset($emailaddress)) and it didn't work I changed it to $emailaddress != ""
and it worked
but then I don't understnad this
when I build a newsletter

if (isset(subscribe))




if (isset(unsubscribe))
that is how I always differentiate my 2 scripts and I do them on the same page, any one got any wisdow to shed on this subject.
By the way

[code]if(!(getmxrr(substr(strstr($_POST['email'], '@'), 1), $temp)) || checkdnsrr(gethostbyname(substr(strstr($_POST['email'], '@'), 1)), "ANY")) {
$errorhandler .= "The Domain name for the email address does not exist<br />";
}[/code]

This tests the email address to 100% make sure the dns server exists. It doesn't say whether the email is valid but it's checks to make sure the domain name exists atleast, another thing to help decrease the chance of bad emails.

Share this post


Link to post
Share on other sites
[quote]I tried with my form at first like this
if (isset($emailaddress)) and it didn't work I changed it to $emailaddress != ""
and it worked
but then I don't understnad this
when I build a newsletter[/quote]
That becuase isset doest not check the value of the variable, but it checks for the existance of the variable. If you want to check whether variable is empty use th empty function like so:
[code=php:0]if(empty($var))
{
    echo 'var is empty!';
}[/code]

Share this post


Link to post
Share on other sites
isset tests to see if a variable has been set, not weather or not it contains a value. I think your looking for [url=http://php.net/empty]empty[/url].

Share this post


Link to post
Share on other sites
So what is the use of isset.
This is the same situation, for instance, when I do a newsletter

<form name="newslettertest" action="newsletterpage.php" method="post">
<label for="emailaddress">Email Address:</label>
<input name="emailaddress" id="emailaddress" type="text" maxlength="80" />
<label for="verifyemail">Verify Email Address:</label>
<input name="verifyemail" id="verifyemail" type="text" maxlength="80" />
<input name="subscribe" id="subscribe" type="submit" value="SubScribe" />
<input name="unsubscribe" id="unsubscribe" type="submit" value="UnSubscribe" />
</form>


Processor down here
[code]<?php
if (isset($subscribe)) {

// work here

}

if (isset($unsubscribe)) {
// work here
}
?>[/code]

This is the exact same thing I am doing on this.

[code]<form name="logemail" action="logpage" method="post">
<label for="emailaddress">Email Address:</label>
<input name="emailaddress" id="emailaddress" type="text" maxlength="80" />
<input name="submit" id="submit" type="submit" value="Gain Access!" />
</form>[/code]



Then over on the page I have it sort of like password protected, just limited access
<?php
if (isset($emailaddress)) {
?>
//show entire page here
<?php
} else {
echo "You suck put in your email";
}
?>
It's the exact same idea, when I do the newsletter this works perfectly, when I do it here it doesn't.

Share this post


Link to post
Share on other sites
It is to make sure the variable your trying to use exists before you actually try to use it.

Share this post


Link to post
Share on other sites
I told you isset checks whether the variable acually exists.

EDIT: Thorpe beat me.. :(

Share this post


Link to post
Share on other sites
I believe this is because when you have a post variable like that, it isn't initialized even if it has no value (i.e. user inputs nothing, no post variable initialized) which would make isset false.

Just my guess.  I use a similar thing on my site.

Share this post


Link to post
Share on other sites
I think I need to rethink how I am processing my newsletters just in case, this is somewhat disturbing.

Share this post


Link to post
Share on other sites
The variables should be sent regardless, except for checkboxes which seem to not send anything if they are not true.

Share this post


Link to post
Share on other sites
if (empty($emailaddress)) {
?>
//show entire page here
<?php
} else {
echo "You suck put in your email";
}
?>

isset only lets the code know theres a varable set to it.


for example

$name="redarrow";

if isset($name) {

echo " hello";

}

In this example the $name is set becouse the varable $name is redarrow.

but if name has not been set like below and has no setting to the varable the echoed message comes up ok.


$name=" ";

if isset($name) {

echo " hello";

}else {

echo "sorry the varable $\name is not set":
}

Share this post


Link to post
Share on other sites
ah I see what you mean, so it would be useful then in this situation.  for like submit buttons.  Because if there clicked they are set, if they are not theya re empty.

Share this post


Link to post
Share on other sites
This...

[code=php:0]
$name=" ";

if isset($name) {

echo " hello";

}else {

echo "sorry the varable $\name is not set":
}
[/code]

Will echo "hello", because $name IS set.

Share this post


Link to post
Share on other sites
Was name set above becouse the double quotes had a gap meaning set to a space.


$name="";

if isset($name) {

echo " hello";

}else {

echo "sorry the varable $\name is not set":
}

Share this post


Link to post
Share on other sites
[quote]ah I see what you mean, so it would be useful then in this situation.  for like submit buttons.  Because if there clicked they are set, if they are not theya re empty.[/quote]

I would never rely on a submit button being sent. Different browsers hadnle them differently. As for your comment, your still off track. If the variable is not set it wont exist. Not existing and being empty are different things. Something has to exist to be empty.

Share this post


Link to post
Share on other sites
[b]Redarrow[/b]
The following will make $name set:
$name;
$name='';
$name=null;
$name=" ";
As you are creating that variable with a value. if you didnt make the variable and just had:
[code=php:0]if(!isset($name))
{
  echo "name is not set";
}[/code]
Then you'll get 'name isset'

Share this post


Link to post
Share on other sites
[quote]Was name set above becouse the double quotes had a gap meaning set to a space.[/quote]
No... even setting it to an empty string, eg; $name = "" , still instantiates the variable.

Share this post


Link to post
Share on other sites
Then does anyone have any small example code, or a test example, of any possible situation where isset can be effectively used in programming without taking any risks?

Share this post


Link to post
Share on other sites
$name=" ";

unset($name);

if isset($name) {

echo " hello";

}else {

echo "sorry the varable $\name is not set":
}

so do you not have $name there then  or use unset like above.

thank you intreasting not  in books lol............

Share this post


Link to post
Share on other sites
[code=php:0]
if (isset($var)) {
  echo "now I can safely use $var without fear of error because it exists";
} else {
  echo 'the variable $var does not exist, therefore trying to use it will produce a warning';
}
[/code]

Share this post


Link to post
Share on other sites
For you two submit buttons this will work:
[code=php:0]<?php

if(isset($_POST['button1']))
{
  echo "You have clicked button1";
}
else if(isset($_POST['button2']))
{
  echo "You have clicked button2";
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  <input type="submit" name="button1" value="Button 1" />
  <input type="submit" name="button2" value="Button 2" />
</form>[/code]

Share this post


Link to post
Share on other sites
So in esence the isset let's php know that there's a varable in a code and should be set.

Share this post


Link to post
Share on other sites
Yes isset checks whether the variable your are refering to exists.

Share this post


Link to post
Share on other sites
thank you wildteen88

i want also to know if you use the unset($name);

will this unset a isset cheers.

Share this post


Link to post
Share on other sites
If you use unset then isset will return false, meaning the variable is non-existant.

Share this post


Link to post
Share on other sites
Thank you wildteen88 and others for your examples and explination's cheers mate's.

sorry businessman to but in, but cheers.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.