Web Analytics Made Easy -
StatCounter login script - CodingForum

Announcement

Collapse
No announcement yet.

login script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • login script

    I found this script at StarGeeks, and have a few questions about creating a registration and login form... i've already tried but had little sucess sadly,

    oh, and one more question... in the MakeTableLogins at the bottom it creates the mysql_querry using pasword char, and not password... is this intentional?
    PHP Code:
    <?
    function MakeTableLogins($database$host$db_user$db_pass) {//create the logins table
        
    $linkID mysql_connect($host$db_user$db_pass);
        
    mysql_select_db($database$linkID);
        
    mysql_query("create table logins (user char(32), pasword char(32))"$linkID);
    }

    function 
    Encrypt($string) {//hash then encrypt a string
        
    $crypted crypt(md5($string), md5($string));
        return 
    $crypted;
    }

    function 
    AddUser($database$host$db_user$db_pass$username$password) { //add user to table logins
        
    $linkID mysql_connect($host$db_user$db_pass);
        
    mysql_select_db($database$linkID);
        
    $password encrypt($password);
        
    $username encrypt($username);
        
    mysql_query("insert into logins values ('$username', '$password')"$linkID);
    }

    function 
    Login($database$host$db_user$db_pass$user$password) { //attempt to login false if invalid true if correct
    $auth false;
    $user Encrypt($user);

    $linkID mysql_connect($host$db_user$db_pass);
    mysql_select_db("$database"$linkID);
    $result mysql_query("select password from logins where user = '$user'"$linkID);
    $pass mysql_fetch_row($result);
    mysql_close($linkID);

    if (
    $pass[0] === (Encrypt($password))) {
         
    $auth true;
    }
    return 
    $auth;
    }
    ?>

  • #2
    the password should be written the same twice, becase else, you willnever get a row returned ...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      I'm not sure if loren answered your question or not, but here's my two bits:

      First of all, I think that you should realize that the code you've posted is just a few functions. You have to call those functions to make them work.

      Secondly, you claim that the code should make forms -- it doesn't do that. What you've got there is php code that runs on the server side.

      You will need a login form (in html probably) that will ask for a username and password. Your form will have to post this information to a php file containing the code you posted.

      You must also include in that file some code to call those functions.

      Here is an example of the form you will need:
      Code:
      <form method="post" action="login.php">
      Username: <input type="text" name="user">
      Password: <input type="password" name="password">
      </form>
      Then in the login.php file, you need to include all that code you submitted earlier, as well as something like the following:
      PHP Code:
      $database "myDB"//name of your mySQL database
      $host "http://127.0.0.1"//location of the host hosting the dB
      $db_user "me"//db username
      $db_password "password"//password for db_user

      $login_authorized Login($database$host$db_user$db_pass$user$password);

      if(
      $login_authorized) {
        echo 
      "You are an authorized user";
      else {
        echo 
      "You are not an authorized user (or you just mistyped your username or password...)"
      I haven't tested that code, but it's so simple I hope I didn't get it wrong! But that's sort of what you need.

      The code I've supplied you doesn't need to be in a function like what you've seen. It needs to be outside of a function to run.

      As you can see I only made use of Login. Encrypt seems to be used by the other functions, so you don't need to call it. MakeTableLogins (whether it's correct or not, I don't know. But it looks good to me) actually creates a table called logins in your mySQL database. If you've already created that table in your database, then you don't need to call this. AddUser does just that, it adds a new user to your logins table (which must exist...).

      It looks like you can make your code more sofisticated by doing if/else statements checking to see if the table exists use it or else create it. If the user exists try to log him/her in, or else add them. Or you can create a registration form to take down some information and call AddUser to add them like that.

      Hope that helps,
      Sadiq.

      Comment


      • #4
        resigter

        Thx, sadiq, yeah, i understood most of the code... except i think i might have set it up wronge, i used the $PHP_SELF function, and had my login php information in a file called functions.php, which is required... i'm guessing this is where i went wronge.. now can anyone give me an example of setting up the registration form? (ps: i'll check the code when i get home)

        Comment

        Working...
        X