www.webdeveloper.com
Results 1 to 4 of 4

Thread: Nesting if commands

  1. #1
    Join Date
    Jul 2005
    Posts
    50

    Nesting if commands

    I'm trying to make a script that will check for a username then if user=abc, for instance, check if pass=123. Here is my code.

    PHP Code:
    <?php
    $name 
    $_GET['name'];
    $pass $_GET['pass'];
    if(
    $name == "Joan Jones"){if($pass == "karen94"){print "Hello $name"}else{print "naughty naughty"}};
    ?>

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    What's wrong with what you are using? What you are doing is a bit basic. What you need is to store all the usernames and passwords either in a file or database. In this example I will use an array to store username and password a design which can be easily extended manually.
    PHP Code:
    <?php
    // a make user array
    $users = array('abc' => '123');

    // check user exists or exit
    if(!array_key_exists($_GET['name'], $users))exit('Naughty Naughty');

    // check password corresponds with user or exit
    if($users[$_GET['name']] != $_GET['pass'])exit('Naughty naughty');

    print 
    'Hello '.$_GET['name'];
    ?>

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    You can use a logical "and" in this case instead of nested ifs.
    PHP Code:
    $name $_GET['name'];
    $pass $_GET['pass'];
    if(
    $name == "Joan Jones" and $pass == "karen94")
    {
      print 
    "Hello $name";
    }
    else
    {
      print 
    "naughty naughty";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jul 2005
    Posts
    50
    Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles