Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,098
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Better Code: If all 3 variables set

    I have a page which allows users to sort a listing of article summaries, as well as navigate from page to page in the listing.

    The URL looks like this...
    Code:
    www.debbie.com/articles/by-author/desc/3
    Where the Sort-Field = "by-author", Sort-Order = "desc", and the Page = "3"


    In order for the URL to be considered "valid", either there must be no variables or all 3 variables must exist.

    So, both of these URLs would be valid...
    Code:
    www.debbie.com/articles/
    www.debbie.com/articles/by-author/desc/3

    But any of these should cause a redirect back to www.debbie.com/articles/
    Code:
    www.debbie.com/articles/by-author/desc/
    www.debbie.com/articles/by-author//3
    www.debbie.com/articles//desc/3

    Also, I have a separate block of code that validates each $_GET variable like this...
    PHP Code:
        // Check for Page in URL.
        
    if (isset($_GET['page']) && $_GET['page']){
            
    // Page in URL.
            
    $currPage $_GET['page'];
            
            if ((
    ctype_digit($currPage)) && ($currPage 0)){
                
    // Valid Format.
                // Continue processing...

            
    }else{
                
    // Redirect to Subsection.
                
    header("Location: " BASE_URL "/articles/");

                
    // End script.
                
    exit();
            }

        }else{
            
    // Page Not in URL.
            // Do nothing.
            
            // Continue processing...

        
    }//End of CHECK FOR PAGE IN URL 

    In order to check if there were 3 out of 3 values, I did this...
    PHP Code:
        if ($sortName && $sortDir && $currPage){
            echo 
    "ALL VALUES PRESENT";
            
        }else{
            echo 
    "MISSING VALUES";
        } 
    But that doesn't work so well, because if any one of those values is NULL, then my code crashes. Also, it doesn't allow for the fact that the person doesn't want to sort anything and so the URL is just www.debbie.com/articles/ which would simply show all articles summaries sorted in a predetermined order.


    This is embarrassing, but I could use some help finding a better way to check to see if none or all 3 variables exist.

    Make sense?!

    Suggestions??

    Sincerely,


    Debbie

  • #2
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    I'm not sure what kind of server you're using but you may be able to save time writing code and just use a redirect rule via your server instead. Here's how you do it with Apache: https://library.linode.com/web-serve...directing-urls

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,098
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Linux_Sage View Post
    I'm not sure what kind of server you're using but you may be able to save time writing code and just use a redirect rule via your server instead. Here's how you do it with Apache: https://library.linode.com/web-serve...directing-urls
    Thanks for the suggestion, but my mod_rewrite is complicated enough, and designed to pass things through to my PHP.

    Besides, what I am trying to do should be easy in PHP - I could just use some outside advice on a cleaner way to code things than the path I seem to be going down...

    Thanks,


    Debbie

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    PHP Code:
    if(empty($sortName) || empty($sortDir) || empty($currPage))
        echo 
    "MISSING VALUES";
    else
        echo 
    "ALL VALUES PRESENT"
    -OR-
    PHP Code:
    if(!empty($sortName) && !empty($sortDir) && !empty($currPage))
        echo 
    "ALL VALUES PRESENT";
    else
        echo 
    "MISSING VALUES"
    The code will assume the variables exist too, just a heads up. If needed you could add an isset() check for them as well or place a @ in front of the variables (ex. @$currPage) to hide the errors.

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,098
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    PHP Code:
    if(empty($sortName) || empty($sortDir) || empty($currPage))
        echo 
    "MISSING VALUES";
    else
        echo 
    "ALL VALUES PRESENT"
    -OR-
    PHP Code:
    if(!empty($sortName) && !empty($sortDir) && !empty($currPage))
        echo 
    "ALL VALUES PRESENT";
    else
        echo 
    "MISSING VALUES"
    The code will assume the variables exist too, just a heads up. If needed you could add an isset() check for them as well or place a @ in front of the variables (ex. @$currPage) to hide the errors.
    That is sort of what I was getting at... isset()... (Such a PITA)

    Can I initialize my variable to NULL instead?

    Or should I do this...
    PHP Code:
        $sortName = (isset($sortName) ? $sortName '');
        
    $sortDir = (isset($sortDir) ? $sortDir '');
        
    $currPage = (isset($currPage) ? $currPage ''); 
    Sincerely,


    Debbie

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by doubledee View Post

    Can I initialize my variable to NULL instead?
    That would work, or even false, or an empty string. So long as it exists is all that it cares about.

    Quote Originally Posted by doubledee View Post

    Or should I do this...
    PHP Code:
        $sortName = (isset($sortName) ? $sortName '');
        
    $sortDir = (isset($sortDir) ? $sortDir '');
        
    $currPage = (isset($currPage) ? $currPage ''); 
    That would work too.

  • #7
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,098
    Thanks
    27
    Thanked 0 Times in 0 Posts
    It's kind of annoying, because the way my mod_rewrite is working (or not), if the user leaves out a value, things shift, thus screwing the logic of how I'd like things to work.

    For example, this...
    Code:
    www.debbie/com/articles//desc/3/
    Ends up as...

    $sortName = 'desc'
    $sortDir = '3'
    $currPage = ''

    I guess that is a whole other issue I don't want to address.

    It seems like the code above is working, I just feel like there is a more eloquent way to do all of this.

    Maybe I should save "refactoring" for later...


    Debbie

  • #8
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Your mod_rewrite is too "loose". You can make it only match when there are three values and be more strict with their type
    Code:
    RewriteRule ^articles/$ /somefile.php [L]
    RewriteRule ^articles/([a-z_]+)/(asc|desc)/([0-9]+)/$ /somefile.php?sort_name=$1&sort_dir=$2&page=$3 [L]
    Anything else will get a 404 error page.

    You still need to validate them because you can never trust user input
    PHP Code:
    <?php

    define
    ('BASE_URL''http://127.0.0.1');

    $validSortNames = array('by_author''by_category' /* etc... */);

    # Set the variables to false if they weren't set
    $sortName = isset($_GET['sort_name']) ? $_GET['sort_name'] : false;
    $sortDir  = isset($_GET['sort_dir']) ? $_GET['sort_dir'] : false;
    $currPage = isset($_GET['page']) ? $_GET['page'] : false;

    /**/header('Content-Type: text/plain');
    if(
    $sortName === false && $sortDir === false && $currPage === false)
    {
        
    /**/echo 'None of the variables were set, ie. a request for /articles/' "\n\n";
    }
    elseif(!
    in_array($sortName$validSortNamestrue)
           || (
    $sortDir != 'asc' && $sortDir != 'desc'/* I presume these are the only options */
           
    || !ctype_digit($currPage)
           || 
    intval($currPage) < 1)
    {
        
    /**/echo 'One of the values is missing or incorrect.' "\n\n";
        
    /*header('Location: ' . BASE_URL . '/articles/');
        exit();*/
    }
    else
    {
        
    /**/echo 'All the values are set and valid.' "\n\n";
    }

    /**/var_dump(array('$sortName'=>$sortName'$sortDir'=>$sortDir'$currPage'=>$currPage));

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,332
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    Quote Originally Posted by doubledee View Post
    isset()... (Such a PITA)
    Really? - I use it everywhere and it's a very easy function to use and even takes multiple variables as parameters.

    Quote Originally Posted by doubledee View Post
    Can I initialize my variable to NULL instead?
    You could but it would not work. Null basically means non existent.

    If you did this:

    PHP Code:
    <?php
    $Var 
    null;

    print (isset(
    $Var))? 'Var exists''Var does not exist';
    ?>
    the isset() function will return false because the $Var variable does not exist. See this example: PHP code - 5 lines - codepad

    Quote Originally Posted by Dubz View Post
    That would work <snip> So long as it exists is all that it cares about.
    No it would not work and would not exist.
    Last edited by tangoforce; 05-03-2014 at 12:32 AM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by tangoforce View Post
    I use it everywhere and it's a very easy function to use and even takes multiple variables as parameters.
    I always forget about this, hence why I didn't use it. I think between that and empty is why I forget, this can take multiple, empty can't.

    Quote Originally Posted by tangoforce View Post
    No it would not work and would not exist.
    I never really checked or thought about it, guess that's what happens when you assume.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •