Page 1 of 1

Need some help with Login script,

Posted: Fri Oct 29, 2010 1:01 pm
by ptrax

Code: Select all

<?php

   $tbl_name="login"; // Table name 

   // Connect to databse.
   $databs = 'c:\database\UsernameDB.mdb';
   $dbConnect = odbc_connect("Driver={Microsoft Access Driver (*.mdb)}; DBQ=$databs","","") or die ("Error opening database .... use the browsers BACK button");

   // Get username and password sent from form 
   $myusername=$_POST['myusername']; 
   $mypassword=$_POST['mypassword'];

   // Build SQL Query
   $sql = "SELECT top 100 * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
   $result = odbc_exec($dbConnect, $sql)

   // Look for Matching table row
   $count = odbc_num_rows($result);
   
   // If result matched $myusername and $mypassword, table row must be 1 row
   if($count==1)
      {
         // Register $myusername, $mypassword and redirect to file "login_success.php"
         session_register("myusername");
         session_register("mypassword"); 
         header("location:login_success.php");
      }
      else 
      {
         echo "Wrong Username or Password";
      }

    // close the connection
    odbc_close($dbConnect);

?>
Please help a PHP mong, I am using WAMP 2.0 (latest download) and MS access 2003.
I keep getting an error on line 18 ($count = odbc_num_rows($result);) I can't see the problem. I have found that odbc_num_rows will return a -1 instead of 1 for access but the issue should be fixed by adding the "top 100" into the SQL statment.

Any help would be greatfull. Trax

Re: Need some help with Login script,

Posted: Fri Oct 29, 2010 3:19 pm
by Ragnis
You are missing a semicolon at the end of line 15.

Re: Need some help with Login script,

Posted: Fri Oct 29, 2010 3:56 pm
by ptrax
Thank you I did miss that, been looking at it to long....

The login_sccess.php script is;

Code: Select all

<HTML>
   <HEAD>
      <TITLE>Login</TITLE>
   </HEAD>
   
   <BODY>
 
<?

   // Check if session is registered
   session_start();
   if(!session_is_registered(myusername))
      {
         header("location:main_login.php");
      }
?>

         <H1>Login Successful</H1>

 
   
   
   </BODY>
</HTML>


However the script still fails to work. Everytime I login Wrong Username or Password is displayed. Makes me think the SQL is wrong or the row count doesn't work.

Does anyone have any throughts?

Many Thanks, Trax


I have just tested the SQL in access and it seems to work fine! I have also changed the ($count==1) in the if statement to ($count==-1) incase the SQL fix above doesn't work, but then login is successful everytime even with incorrect login details.