Jump to content

login prob,, why?


justAnoob

Recommended Posts

this doesn't work,,, but my registration script works like this.

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$sql=sprintf("SELECT * FROM $tbl_name WHERE username ='$username' and password = '$password' LIMIT 1",
              mysql_real_escape_string($username),
              md5($pasword));
?>

and this works

<?php
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);

$sql="SELECT * FROM $tbl_name WHERE username ='$username' and password = '$password' LIMIT 1";
?>

Link to comment
Share on other sites

No, you don't have to use sprintf. It's actually better if you don't apply functions blindly if you don't know what they do and then come here and ask why it doesn't work and if you need to use it. :D If you don't know something, it's better to ask a more straight forward question.

 

Anyways, php.net is a great resource site for all things PHP. sprintf.

 

Edit - that's because you're not applying sprintf correctly. In you first post, $password is not hashed. So when you run the SQL, $password is still the original string the user typed in, not the hashed one in the DB. Read up on sprintf. I have the link above.

Link to comment
Share on other sites

http://php.net/sprintf

 

The function takes a string with variables to be replaced formatted like so:

 

%s = A string

%d = an integer

 

For now these are probably all you will need, theres a full list at the link on php.net

 

It then takes variables which are used to replace the % formatted data within the string, the first %? will be replaced by the first passed paramater, the 2nd %? by the second param etc...

 

SO for your query to work, it needs to be in the following format...

$sql = sprintf("SELECT * FROM %s WHERE username = '%s' AND password = '%s' LIMIT 1",
   $tbl_name,
   mysql_real_escape_string($username),
   md5($password) );

Link to comment
Share on other sites

What do you mean by problems? You need to be more specific if you want help with the problem.

 

haku, I think they were supposed to be full stops as the comma key is right next to the full stop key?

 

This touchtypo stuff is harder then it looks. =P 

Link to comment
Share on other sites

sprintf isn't all too effective for protecting your script. Just make sure that you cast user input thats expected to be integer type with (int) and use mysql_real_escape_string on your user inputted strings used for mysql.

 

Then when the data has been fetched by mysql just use stripslashes and htmlEntities on the data before echoing it to the screen.

Link to comment
Share on other sites

Okay, so back to the post I posted earlier where I took out 'sprintf' and then still had a problem with the ',' 

I've looked everywhere, and everyone is saying to use mysql_real_escape_string like this...

 

$var = mysql_real_escape_string($_POST['whatever'])

 

and not at the end of the SQL statement. here is a simple register script that i have. this is what i've seen on examples that people are doing to  protect against hackers.(i know if someone wanted to mess stuff up they will. just looking for some basic protection.)

 

<?php
session_start();
require_once 'connection.php';

// check if username or email is already registered, if so, give error message
$username = mysql_real_escape_string($_POST["username"]);
$sql = "SELECT * FROM members WHERE username = '$username'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
// if username exists, display error
if($count > 0)
{
$_SESSION['dup'] = "This username is already registered. Please try again.";
header("location: http://www.--------.com/registration.php");
exit();
}
// if data is good, register the new member
else
{
$username = mysql_real_escape_string($_POST["username"]);
$password = md5($_POST["password"]);
$email = mysql_real_escape_string($_POST["email"]);

    $sql = "INSERT INTO %s (username, password, email)VALUES('$username','$password','$email')";

mysql_query($sql) or die(mysql_error());

unset($_SESSION['dup']);
$_SESSION['goodreg'] = "Thank you for registering. Please log in above.";
sleep(2);
header("location: http://www.------.com/registration.php");
mysql_close();
    exit();
}
?>

Link to comment
Share on other sites

oopss, sorry, don't mind that, i forgot to change that back to my table name. but taking a look at the script, if it was yours, what would you do to beef up the security. I'm not looking for anything crazy. just your basic. also, why is my strLen not working correctly. it brings the error if the name is less then 6 or more than 12, but not something with, lets say 8.

 

<?php
session_start();
require_once 'connection.php';

// check if username or email is already registered, if so, give error message
$username = mysql_real_escape_string($_POST["username"]);
$sql = "SELECT * FROM members WHERE username = '$username'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
// if username exists, display error
if($count > 0)
{
$_SESSION['dup'] = "This username is already registered. Please try again.";
header("location: http://www.-----.com/registration.php");
exit();
}
if(strLen($username) < 6 || strLen($username) > 12 )
{
   $_SESSION['length'] =  'Username can only be 6-12 characters in length.';
   header("location: http://www.------.com/registration.php");
   exit();
}
// if data is good, register the new member
else
{
$username = mysql_real_escape_string($_POST["username"]);
$password = md5($_POST["password"]);
$email = mysql_real_escape_string($_POST["email"]);

    $sql = "INSERT INTO members (username, password, email)VALUES('$username','$password','$email')";

mysql_query($sql) or die(mysql_error());

unset($_SESSION['dup']);
$_SESSION['goodreg'] = "Thank you for registering. Please log in above.";
sleep(2);
header("location: http://www.-------.com/registration.php");
mysql_close();
    exit();
}
?>

Link to comment
Share on other sites

i looke on google and it shows examples of something like this. but i still get the same prob. even if i enter a username that is 6 characters long, i get the error that the username is not the correct length.

<?php
if($count > 0)
{
   $_SESSION['dup'] = "This username is already registered. Please try again.";
   header("location: http://www.--------.com/registration.php");
   exit();
}
elseif( strLen($_POST["$username"]) < 6 || strLen($_POST["$username"]) > 12 )
{
   $_SESSION['length'] =  'Username can only be 6-12 characters in length.';
   header("location: http://www.---------.com/registration.php");
   exit();
}
// if data is good, register the new member
else
{
?>

Link to comment
Share on other sites

Okay, a couple times it worked. Now it does not. I can enter a username that is 6 or more characters and it still gives me the error that the username is not the correct length.

<?php
session_start();
require_once 'connection.php';

$username = mysql_real_escape_string($_POST["username"]);
$email = mysql_real_escape_string($_POST['email']);

$sql = "SELECT * FROM members WHERE username = '$username' and email = '$email'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

// if username is currently being used, display error
if($count > 0)
{
unset($_SESSION['length']);
$_SESSION['dup'] = "This username or email is already registered. Please try again.";
header("location: http://www.-------.com/registration.php");
exit();
}
// if username is not correct length, display error
elseif ( strLen($_POST["username"]) < 6 )
{
   unset($_SESSION['dup']);
   $_SESSION['length'] =  'Username must be a minimum of 6 characters in length.';
   header("location: http://www.------.com/registration.php");
   exit();
}

// if data is good, register the new member
else
{
$username = mysql_real_escape_string($_POST["username"]);
$password = md5($_POST["password"]);
$email = mysql_real_escape_string($_POST["email"]);

    $sql = "INSERT INTO members (username, password, email)VALUES('$username','$password','$email')";

mysql_query($sql) or trigger_error();

unset($_SESSION['dup']);
$_SESSION['goodreg'] = "Thank you for registering. Please log in above.";
sleep(2);
header("location: http://www.------.com/registration.php");
mysql_close();
    exit();
}
?>

Is there something wrong with the if,elseif,else?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.