Results 1 to 3 of 3

Thread: need urgent help in creatin a login page

  1. #1
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy need urgent help in creatin a login page

    hi
    i am fairly new to php n mysql.i hv been asked to do a login page using sessions.i hv tried but cant find the correct code and cant understand sessions.i hv to do this by monday.
    i hv a table called user which has 3 fields id(primary),username and password.

    my form shud authenticate the user and if the username and password is correct den it shuld be directed to a pae called admintasks.php.

    i will be really greatful is some1 can help me with this,,
    thanks in advance
    suk
    p.s:i kno twey has worked on this but somehow i cant follow his code..simply usin his code is not workin...and i cant understand wht changes to make..
    plsssssssssssss help

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this (not tested but should work):

    Code:
    <?php
    session_start();
    
    ####### Configuration ########
    $DBserver = "localhost"; //server for Database
    $DBuser = "USERNAME"; //user for MySQL Database
    $DBpass = "PASSWORD"; //password for above mentioned username
    $DB = "DATABASE"; //database the tables are located in
    
    $usersTable = "user"; //Table the user information is in
    
    $redirect = "admintasks.php"; //page to redirect to on successful login
    ####### End Config ########
    
    $conn = mysql_connect($DBserver, $DBuser, $DBpass);
    mysql_select_db($DB, $conn);
    
    if (@$_POST['act'] == "do_login") {
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $userInfo = mysql_query("SELECT * FROM `$usersTable` WHERE `username`= '$username'");
    
     if (mysql_num_rows($userInfo) < 1) {
       echo 'There are no users with that username in the database. Please go back and enter a valid username/password combination.';
     }
    
     else {
      $q = mysql_fetch_array($userInfo);
      
      if ($q['password'] != $password) {
        echo 'The password is invalid. Please go back and enter a valid username/password combination.';
      }
    
      else {
         $_SESSION['id'] = $q['id'];
         $_SESSION['username'] = $q['username'];
         $_SESSION['password'] = $q['password'];
    
         header('Location: '.$redirect);
      }
     }
    }
    
    else {
    ?>
    
    <html>
     <head>
       <title>Login</title>
     </head>
    <body>
    
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
    <input type="hidden" name="act" value="do_login">
    
    Username: <input type="text" name="username" value=""> <br>
    Password: <input type="password" name="password" value=""> <br>
    <input type="submit" value="Login">
    </form>
    </body>
    </html>
    
    <?php
    }
    ?>
    Edit as you see fit.
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up thanks!!!

    thanks..the code is perfectly workin...

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
  •