tags:

views:

199

answers:

4

Below is a snippet of PHP that I need to get working, I need to make sure that 3 items are not set already and not === ''

I have the part to make it check if not set but when I try to see if the values are empty as well (I might not even be thinking clearly right now, I might not need to do all this) But I need to make sure that redirect, login_name, and password are all not already set to a value, if they are set to a value I need to make sure that the value is not empty.

Can someone help, when I add in check to see if values are empty, I get errors with my syntax, also not sure if I should have 5-6 checks like this in 1 if/else block like that, please help

I need to check the following:
- $_GET['redirect'] is not set
- $_REQUEST['login_name'] is not set
- $_REQUEST['login_name'] is not != to ''
- $_REQUEST['password'] is not set
- $_REQUEST['password'] is not != to ''

if (!isset($_GET['redirect']) && (!isset($_REQUEST['login_name'])) && (!isset($_REQUEST['password']))){
    //do stuff
}



UPDATE

Sorry It is not very clear, I was a bit confused about this. Based on Hobodaves answer, I was able to modify it and get it working how I need it. Below is the code how I need it, it works great like this... So if that can be improved then that is the functionality that I need, I just tested this.

if (!isset($_GET['redirect']) 
    && empty($_GET['redirect'])
    && isset($_REQUEST['login_name'])
    && !empty($_REQUEST['login_name'])
    && isset($_REQUEST['password'])
    && !empty($_REQUEST['password'])
) {
    echo 'load user';
}

if this was loaded then it will run the login process

login.php?login_name=test&password=testing

If this is loaded then it will NOT run the login process

login.php?login_name=test&password=  
+2  A: 
if (!isset($_GET['redirect']) 
    && !isset($_REQUEST['login_name'])
    && empty($_REQUEST['login_name'])
    && !isset($_REQUEST['password'])
    && empty($_REQUEST['password'])
) {
    // do stuff
}

This is exactly what you describe, (not != empty === empty). I think you should edit your question though to explain what you're triyng to do, so we can suggest better alternatives.

Edit:

Your updated question can be simplified as:

if (empty($_GET['redirect'])
    && !empty($_REQUEST['login_name'])
    && !empty($_REQUEST['password'])
} {
    // load user
}
hobodave
Sorry this got a little confusing for me when I was writting the question, I modified your answer to work in the way that I need it, I posted it in my question above as well.
jasondavis
If a variable isn't set, then it will be empty. So you can remove the last two `!isset`.
Pikrass
works great, thanks
jasondavis
When passing these to mysql, if I don't want to use prepared statments or any other fancy stuff, will mysql_real_escape_string ran on these work good enough?
jasondavis
@jasondavis: yes
hobodave
+1  A: 

A more maintainable solution would be storing each key in an array, and then foreach over it and check if isset or empty. You're not very DRY with your current solution.

The implementation would look someting like:

<?php
$keys = array('login_name', 'password');

foreach($keys as $key)
{
   if(!isset($_REQUEST[$key]) || empty($_REQUEST['key'])
      // Show error message, kill script etc.
}


// Dot stuff

?>
alexn
A: 

You could create an array

$array = array(
               $_GET['redirect'], 
               $_GET['redirect'],
               $_REQUEST['login_name'],
               $_REQUEST['login_name'],
               $_REQUEST['password'],
               $_REQUEST['password']
);

foreach($array as $stuf)
{
   if(!empty($stuff) && $tuff !=0) 
      //do something
}
streetparade
-1: duplicate of alexn's answer, but worse.
hobodave
+1  A: 

If a global variable is not set, that is the same as being empty. Thus:

!is_set(($_REQUEST['username'])) is the same as empty($_REQUEST['username'])

So based on your update, you can simplify to:

if (empty($_GET['redirect'])
    && !empty($_REQUEST['login_name'])
    && !empty($_REQUEST['password'])
) {
    echo 'load user';
}

please read!

Sorry, the previous answer I gave will not give you what you want. Here is why:

If you use !_REQUEST['password'], it will return true if the password is empty or if it is not set. However if you use if($_REQUEST['password']) it will pass the conditional anytime that global variable is set, even if it is empty.

Therefore I recommend:

$no_redirect = (!$_GET['redirect']);
$login_name = (!$_REQUEST['login_name']) ? false : true;
$password = (!$_REQUEST['login_name']) ? false : true;

if($no_redirect && $login_name && $password) {
    echo 'load user';
}

Sorry for the previously bad info.

Anthony