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 05-26-2011, 06:26 PM   PM User | #1
gilgalbiblewhee
Regular Coder

 
Join Date: Mar 2005
Posts: 735
Thanks: 4
Thanked 1 Time in 1 Post
gilgalbiblewhee is an unknown quantity at this point
login problems

I ran into some problems in the login.
The index page shows this:
PHP Code:
<form action="login/login.php" method="post"
  <
label for="login-username" style="float: left; margin: 0px 0px 0px 5px;">Username:</label><br /> 
  <
input type="text" name="username" id="login-username" value="" style="float: left; margin: 0px 0px 0px 5px; border: 1px solid #7A1010; color: #7A1010;" />
  <
label for="login-password" style="float: left; margin: 0px 0px 0px 5px;" >Password:</label><br /> 
  <
input type="password" name="password" id="login-password" value="" style="float: left; margin: 0px 0px 0px 5px; border: 1px solid #7A1010; color: #7A1010;" /><br /> 
  <
br />
  <
label for="login-remember" style="float: left; margin: 0px 0px 0px 5px;">Remember me?</label>
  <
input type="checkbox" name="remember" id="login-remember" style="float: left; margin: 5px 0px 0px 5px;" /><br />
  <
input type="submit" value="Login" style="float: left; margin: 0px 0px 0px 5px; background-color: #7A1010; color: #EAE8C8;" />
</
form
The form leads to here:
PHP Code:
<?php
// Database connection file
require_once("includefiles/dbconnection.php");
$un=isset($_POST['username']) ? $_POST['username'] : "";
$pw=isset($_POST['password']) ? $_POST['password'] : "";
echo 
"Hellooooooo!!!!!!!!!!!!".$un." ".$pw;
// Form submitted?
if($_SERVER['REQUEST_METHOD'] == "POST"){
    
$errors = array();
    
// Validate form
    
foreach($_POST as $key => $value){
        if(empty(
$value)){
            
$errors[$key] = $key " was empty";
        }
    }
    
// If no errors, continue
    
if(count($errors) == 0){
        
$sql sprintf("SELECT usergroup AS success FROM {$dbTable} WHERE username='%s' AND password=MD5('%s')"$un$pwextract(mysql_fetch_assoc(mysql_query($sql)));
        
//echo $sql;
        // If this is not set, there was an error
        
if(!isset($success)){
            
$errors[] = "that username and password combination are incorrect";
        }else{
            
// Remember me?
            
if(isset($_POST['remember'])){
                
setcookie("login"$_POST['username'] . ":" $successtime() + (3600 24 30)); // store for 30 days
            
}
            
// Log the user in
            
$_SESSION['login'] = true;
            
$_SESSION['username'] = $_POST['username'];
            
$_SESSION['group'] = $success;
            
$_SESSION['just_logged_in'] = true// to display a message
            // Redirect back to the main page
            
$redirect true;
            unset(
$errors);
        }
    }
}else{
    
// The form was not submitted, so they shouldn't be here
    
$redirect true;
}
// Redirect if needed
if(isset($redirect)){
    
header("Location: " $baseURL);
    exit;
}
include(
"login-form.php");
?>
But this page shows blank.
__________________
Compare bible texts (and other tools):
TheWheelofGod
gilgalbiblewhee is offline   Reply With Quote
Old 05-26-2011, 06:52 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
First of all, any script that uses SESSION, but start like this:

<?php
session_start();

Your blank page indicates you have a PHP script error, but your error
reporting is turned off, so it won't tell you what is wrong.

I would change the top part of your script to this ....

<?php
session_start();
error_reporting(E_ALL);





.
mlseim is offline   Reply With Quote
Old 05-26-2011, 07:02 PM   PM User | #3
gilgalbiblewhee
Regular Coder

 
Join Date: Mar 2005
Posts: 735
Thanks: 4
Thanked 1 Time in 1 Post
gilgalbiblewhee is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
First of all, any script that uses SESSION, but start like this:

<?php
session_start();

Your blank page indicates you have a PHP script error, but your error
reporting is turned off, so it won't tell you what is wrong.

I would change the top part of your script to this ....

<?php
session_start();
error_reporting(E_ALL);
.
Should be
PHP Code:
      $sql sprintf("SELECT usergroup AS success FROM {$dbTable} WHERE username='%s' AND password=MD5('%s')"
                   
mysql_real_escape_string($_POST['username']), $_POST['password']);
      
extract(mysql_fetch_assoc(mysql_query($sql)));
      echo 
$sql
__________________
Compare bible texts (and other tools):
TheWheelofGod
gilgalbiblewhee is offline   Reply With Quote
Old 05-26-2011, 07:12 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
What does your last post mean?
That you found the problem, or you are asking about it?
mlseim is offline   Reply With Quote
Old 05-26-2011, 08:00 PM   PM User | #5
gilgalbiblewhee
Regular Coder

 
Join Date: Mar 2005
Posts: 735
Thanks: 4
Thanked 1 Time in 1 Post
gilgalbiblewhee is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
What does your last post mean?
That you found the problem, or you are asking about it?
It doesn't solve the problem but the original code was:
PHP Code:
      $sql sprintf("SELECT usergroup AS success FROM {$dbTable} WHERE username='%s' AND password=MD5('%s')"
                   
mysql_real_escape_string($_POST['username']), $_POST['password']);
      
extract(mysql_fetch_assoc(mysql_query($sql)));
      echo 
$sql
instead of
PHP Code:
$sql sprintf("SELECT usergroup AS success FROM {$dbTable} WHERE username='%s' AND password=MD5('%s')"$un$pwextract(mysql_fetch_assoc(mysql_query($sql))); 
I thought that was the error because it was in a bracket so I removed the ; and skipped a line. But that made it worse. I declared the $un and $pw to the $_POST above as well.
__________________
Compare bible texts (and other tools):
TheWheelofGod

Last edited by gilgalbiblewhee; 05-26-2011 at 08:02 PM..
gilgalbiblewhee is offline   Reply With Quote
Old 05-26-2011, 08:49 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I've never seen a query request using sprintf ... that's a new one for me.

Maybe you can try the query in a way like this:
http://www.tizag.com/mysqlTutorial/mysqltables.php


.
mlseim is offline   Reply With Quote
Old 05-27-2011, 01:40 AM   PM User | #7
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
gilgalbiblewhee, turn on error reporting or check the error log so you can see the what is causing the problem. You should also be hashing the password using PHP's md5() instead of passing the raw string to MySQL. You are open to SQL injection with the way you have it now.

Quote:
Originally Posted by mlseim View Post
I've never seen a query request using sprintf ... that's a new one for me.
sprintf() just formats the string. It's similar to do prepared statements in that you use placeholders and can limit the input to types, but you still have to execute the query.
Inigoesdr is offline   Reply With Quote
Old 05-27-2011, 01:49 AM   PM User | #8
gilgalbiblewhee
Regular Coder

 
Join Date: Mar 2005
Posts: 735
Thanks: 4
Thanked 1 Time in 1 Post
gilgalbiblewhee is an unknown quantity at this point
Quote:
Originally Posted by Inigoesdr View Post
gilgalbiblewhee, turn on error reporting or check the error log so you can see the what is causing the problem. You should also be hashing the password using PHP's md5() instead of passing the raw string to MySQL. You are open to SQL injection with the way you have it now.



sprintf() just formats the string. It's similar to do prepared statements in that you use placeholders and can limit the input to types, but you still have to execute the query.
Ok. Turning on the errors shows the following:
Quote:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in ...\login.php on line 22

Warning: extract() expects parameter 1 to be array, null given in ... \login.php on line 22
Line 22 is:
PHP Code:
      $sql sprintf("SELECT usergroup AS success FROM {$dbTable} WHERE username='%s' AND password=MD5('%s')"
                   
mysql_real_escape_string($_POST['username']), $_POST['password']);
      
extract(mysql_fetch_assoc(mysql_query($sql)));//line 22
      
echo $sql
...and the password is md5ed:
Quote:
$sql = sprintf("SELECT usergroup AS success FROM {$dbTable} WHERE username='%s' AND password=MD5('%s')"
, mysql_real_escape_string($_POST['username']), $_POST['password']);
extract(mysql_fetch_assoc(mysql_query($sql)));//line 22
echo $sql;
__________________
Compare bible texts (and other tools):
TheWheelofGod

Last edited by gilgalbiblewhee; 05-27-2011 at 01:53 AM..
gilgalbiblewhee is offline   Reply With Quote
Old 05-27-2011, 02:17 PM   PM User | #9
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by gilgalbiblewhee View Post
Ok. Turning on the errors shows the following:
Your query failed, find out why. What is the value of $sql after the sprintf line?

Quote:
Originally Posted by gilgalbiblewhee View Post
...and the password is md5ed:
Yeah, but if you read my message I state that you should do it in PHP instead of MySQL because you are passing the raw string to MySQL to be hashed, which leaves you open to SQL injection.
Inigoesdr 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 06:49 AM.


Advertisement
Log in to turn off these ads.