Array help

Discussion in 'PHP' started by hasbehas, Jul 24, 2010.

  1. #1
    Hi,

    I have this simple script.
    I need to define more than 1 user for this and also assign name for the username.

    Any ideas ?


    
    <?php
    session_start();
    
    $username = "user1";
    $password = "pass1";
    $name = "User";
    
    
    if(isset($_GET['logout']))
    {
      unset($_SESSION["login"]);
      echo "You have logged out ... ";
      echo "[<a href='index.php'>Login</a>]";
      exit;
    }
    
    if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !isset($_SESSION["login"]))
    {
      header("WWW-Authenticate: Basic realm=\"Agents\"");
      header("HTTP/1.0 401 Unauthorized");
      $_SESSION["login"] = true;
      echo "You are unauthorized ... ";
      echo "[<a href='index.php'>Login</a>]";
      exit;
    }
    else
    {
      if($_SERVER['PHP_AUTH_USER'] == $username && $_SERVER['PHP_AUTH_PW'] == $password)
      {
    echo "Welcome $name";
      }
      else
      {
        unset($_SESSION["login"]);
        header("Location:index.php");
      }
    }
    
    ?> 
    
    PHP:
     
    hasbehas, Jul 24, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Try this:

    <?php
      session_start();
      
      //array of username => password
      $login_info = array('user1' => 'pass1');
      
      if (isset($_GET['logout'])) {
          unset($_SESSION["login"]);
          echo "You have logged out ... ";
          echo "[<a href='index.php'>Login</a>]";
          exit;
      }
      
      if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !isset($_SESSION["login"])) {
          header("WWW-Authenticate: Basic realm=\"Agents\"");
          header("HTTP/1.0 401 Unauthorized");
          $_SESSION["login"] = true;
          echo "You are unauthorized ... ";
          echo "[<a href='index.php'>Login</a>]";
          exit;
      } else {
          if (array_key_exists($_SERVER['PHP_AUTH_USER'], $login_info) && $login_info[$username] == $_SERVER['PHP_AUTH_PW']) {
              echo "Welcome {$_SERVER['PHP_AUTH_USER']}";
          } else {
              unset($_SESSION["login"]);
              header("Location:index.php");
          }
      }
    ?>
    PHP:
    You can edit $login_info to add more 'accounts'.
     
    danx10, Jul 24, 2010 IP
  3. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #3
    Thanks verymuch for the effort.
    But the code does not work.
    I mean it does not accept the username & pass.
    Tried a few different ways but still could not get it to work.. :(

    Any ideas ?
     
    hasbehas, Jul 24, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    I just modified your code, theirfore it should'nt effect functionality, however I rewrote your code here you go:

    <?php
      session_start();
      //array of username => password
      $login_info = array('admin' => 'pass', 'danx10' => 'dppass');
     
      $file = basename(__FILE__);
      if (isset($_GET['logout'])) {
          unset($_SESSION["username"]);
          unset($_SESSION["login"]);
          header("Location: {$file}");
      }
     
      if (!isset($_SESSION['login'])) {
          if (isset($_POST['submit'])) {
              $username = $_POST['username'];
              $password = $_POST['password'];
              $errors = array();
              if (strlen($username) <= 1) {
                  $errors[] = "The username is too short.";
              }
              if (strlen($password) <= 1) {
                  $errors[] = "The password is too short.";
              }
             
              if (!array_key_exists($username, $login_info) || $login_info[$username] != $password) {
                  $errors[] = "Username or password are incorrect";
              }
             
              if (empty($errors)) {
                  $_SESSION["username"] = $username;
                  $_SESSION["login"] = true;
                  header("Location: {$file}");
              } else {
                  $out = "<ul>";
                  foreach ($errors as $error) {
                      $out .= "<li>{$error}</li>";
                  }
                  $out .= "</ul>";
                  echo $out;
              }
          } else {
    ?>
    <form method="post">
    Username: <input type="text" name="username"><br />
    Password: <input type="password" name="password"><br />
    <input type="submit" name="submit" value="Login">
    </form>
    <?php
          }
      } else {
    ?>
    Hello <?php
          echo $_SESSION["username"];
    ?>, you are logged in<br />
    <a href="?logout">Logout?</a>
    <?php
          /* private content should go here */
      }
    ?>
    PHP:
     
    danx10, Jul 24, 2010 IP
  5. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #5
    Thanks danx10, it worked just fine..

    One extra question. If I was to add a detail for that user, ie. membership number or some detail to print on the secure content, how would I do it ?
    Something like " $login_info = array('admin' => 'pass' => '4223', 'danx10' => 'dppass' =>'4224'); "
     
    hasbehas, Jul 26, 2010 IP