PHP Login script help.

by 6 replies
7
Hi all,

I'm a bit stuck with my login script right now. I'm trying to move most of what (was) a working script into a class function. Here we go.

Esentially, user enters in name and password via form. Then it is checked below

PHP Code:
if(isset($_POST['submit'])){

$user=$_POST['user']; 
$password=$_POST['password'];


//To ensure that none of the fields are blank when submitting the form if
if(isset($_POST['user']) && isset($_POST['password'])) 
    {    
    
    
$user stripslashes($user);
$password stripslashes($password); //<<For some protection use of the slashes
$db1=new dbmember();
$db1->openDB();           
$result=$db1->logcon($user$password);


if(
$result[0]==1)
{
    
session_start();
    
$_SESSION['user'] = $user;
    
$_SESSION['password'] = $password;
    
$_SESSION['loggedin'] = "true";
    
header("location:index.php");
}
        else
        {
            print (
'<div id="error">Acess denied, wrong username or password?</div>');
        }
        }
        else
            {
             print (
'<div id="error">Enter something!</div>');
        }


Obviously, minus the form. Here's the function.

Code:
function logcon($user, $password )
{
    
   $esc_user = mysqli_real_escape_string($this->conn, $user);
   $esc_password = mysqli_real_escape_string($this->conn,$password);  
$sql = "SELECT ALL from USERS WHERE username  ='{$user}' AND password='{$password}'";
 $result = mysqli_query($this->conn, $sql);
 

              $login = mysqli_fetch_array($result); //<< Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
            return $login;
            }
So my question is, why would mysqli_query($this->conn, $sql); be resulting in a boolean, not a number? Any pointerss on setting this straight?
#programming #login #php #script
  • If you go here....

    PHP: mysqli::query - Manual

    You will see....

    Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

    So it can return a boolean.
  • Also, you should never get back a number(I'm guessing you mean Integer). You should get back either a "mysqli_result object" or you should get a boolean.
    • [1] reply
    • Hi got it, seems my problem was with my SQL query. It was messing up causing an error to be moved down the code where I was focusing all my attention.

      So now I've cleared the error, but the login script is not accepting correct login credentials (wrong username) could I get a second opinion on why that might be happening?

      Thanks
      • [2] replies
  • Echo out your $sql right before that call so you can see what you are actually running.

Next Topics on Trending Feed