SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Location
    Rep of Ireland
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Login for 5 people to access back office page

    I want to get this piece of Login code to direct a user to a back_office.html page but I cannot get it to perform as required!

    PHP Code:
    <?php
    session_start
    (); 
    $logins = array(
    'u1' => 'p1',
    'u2' => 'p2',
    'u3' => 'p3',
    'u4' => 'p4',
    'u5' => 'p5'
    );

    $user strtolower($_POST['user']);
    $pass $_POST['pass']; 

    if (isset(
    $logins[$user])&&$logins[$user]==$pass) {
        
    $_SESSION['username'] = $user;


    if (!isset(
    $_SESSION['username'])) {
        exit(
    'Access denied.'); 
    }
    ?>
    Any direction to get working greatly appreciated.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Essentially:

    PHP Code:
    $logins = array( 
    'u1' => 'p1'
    'u2' => 'p2'
    'u3' => 'p3'
    'u4' => 'p4'
    'u5' => 'p5' 
    ); 


    // spoof the vars for now, change these to test

    $_POST['user'] = 'u1';
    $_POST['pass'] = 'p1';  // change to P1 p2 etc


    if( 
        isset(
    $_POST['user'] )
    && isset (
    $_POST['pass'])
    && 
    array_key_exists(strtolower($_POST['user']), $logins)
    && 
    strtolower($_POST['pass']) === strtolower($logins[$_POST['user']]) // if the pass matches the value of the login key 
    ){
    echo 
    "You are in my son... ";

    You could arguably improve that by picking things apart and failing straight off if the POST vars are not set, then reassigning the vars to $user etc, you can also add trim() as well as strtolower() ing the incoming vars.

    Generally a bad idea to have passwords in open text though, at least move this into an include file so that is never available from the public html docs or below.

    array_key_exists


Bookmarks

Posting Permissions

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