Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-02-2012, 01:15 AM   PM User | #1
codercorey
New to the CF scene

 
Join Date: Jun 2012
Location: Barrie
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
codercorey is an unknown quantity at this point
Keep getting invalid login when i try to login

any help on why its not letting log in would be appreciated, thanks

PHP Code:
<?php
// get login values & hash the password
$username = ($_POST['username']);
$password =  sha1($_POST['password']);

//  connect to the database
$conn mysqli_connect('','''''') or die('connection error');

// write a sql query
$sql ="SELECT id FROM admins WHERE username = '$username' AND password = '$password'";
echo 
$sql;
// execute the sql and store the query result
$result mysqli_query($conn$sql);
// count the number of rows in the database 
$count mysqli_num_rows($result);
echo 
$count;
// check the count to see the amount of matches 

if($count == 1)
{

    echo 
'logged in successfully';
    
// get first record from query
    
$row mysqli_fetch_array($result);

    
// store the id in the session object
    
session_start(); 
    
$_SESSION['user_id'] = $row['id']; 

}
else
{
    echo 
'Invalid Login'
    
}

mysqli_close($conn);

?>
codercorey is offline   Reply With Quote
Old 08-02-2012, 01:58 AM   PM User | #2
BoganBum
New to the CF scene

 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
BoganBum is an unknown quantity at this point
Try removing the () like

$username = $_POST['username'];
BoganBum is offline   Reply With Quote
Old 08-02-2012, 02:00 AM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
PHP Code:
session_start(); 
the above statement must occur before any echo's/output, so place it as the first line.

PHP Code:
$row mysqli_fetch_array($resultMYSQLI_ASSOC); 
or MYSQLI_BOTH, MYSQLI_NUM.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-02-2012, 10:43 AM   PM User | #4
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 594
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
after removing the brackets that bogan suggests
try this as your query:

HAVING instead of AND
PHP Code:
$sql ="SELECT id FROM admins WHERE username = '".$username."' HAVING password = '".$password."'"
Arcticwarrio is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 PM.


Advertisement
Log in to turn off these ads.