Jump to content

[SOLVED] problems with login script


wikedawsum

Recommended Posts

Hello all. I'm having some serious problems and cannot figure out what is going on. I've created a simple login script so I can check connections to my database, etc.. (been having problems for a while and am doing this to narrow them down).

My login form is as follows:

[code]<form name="authenticate" method="post" action="login.php">
<input name="username" type="text" value="username" size="20"/><br>
<input name="passwd" type="text" value="passwd" size="20"/><br>
<input type="submit" name="submit" value="submit"/>
<input type="reset" name="reset" value="reset"/>
</form>[/code]

login.php:

[code]<?php

@mysql_connect("mysql.aacapartsandsupplies.com", "admin", "******") or die("Cannot connect to DB!");
@mysql_select_db("tokens") or die("Cannot select DB!");
$sql="SELECT username FROM user WHERE username='$username' and passwd=sha1('$passwd')";
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
print "no such login in the system. please try again.";
exit();
}
else{
print "successfully logged into system.";
//proceed to perform website’s functionality – e.g. present information to the user
}

?>[/code]

Unless I am missing something, all of this seems to look ok to me. However, when I try to login with my registered username and password, I get the "no such login in the system. please try again" error. What confuses me is that the username IS registered, and I can view that information by visiting [url=http://www.aacapartsandsupplies.com/users.php]http://www.aacapartsandsupplies.com/users.php[/url]. So, if the user is registered, why is it telling me they're NOT registered? If you want to check the form out it's located at [url=http://www.aacapartsandsupplies.com/login.html]http://www.aacapartsandsupplies.com/login.html[/url]. Again, these are just test pages and are not secure or anything. I'm just trying to narrow down my problems.

Hopefully I've explained this well enough. Thanks to anyone who can give me some insight.
Link to comment
Share on other sites

[code]
<?php

@mysql_connect("mysql.aacapartsandsupplies.com", "admin", "******") or die("Cannot connect to DB!");
@mysql_select_db("tokens") or die("Cannot select DB!");
$sql="SELECT username FROM user WHERE username='$username' and passwd=sha1('$passwd')";
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
print "no such login in the system. please try again.";
exit();
}
else{
print "successfully logged into system.";
//proceed to perform website’s functionality – e.g. present information to the user
}

?>
[/code]
should have to change

if(mysql_affected_rows()==0){

if im not mistaken
because then mysql_affected_rows has no array to actually look for so it should be

if(mysql_affected_rows($sql)==0){

If you just have a white screen it might be an error that your not seeing take out the die part and see if it works
Link to comment
Share on other sites

Fearsolider, tried using your suggestion..

[code]<?php

@mysql_connect("mysql.aacapartsandsupplies.com", "admin", "bc5106") or die("Cannot connect to DB!");
@mysql_select_db("tokens") or die("Cannot select DB!");
$sql="SELECT username FROM user WHERE username='$username' and passwd=sha1('$passwd')";
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows($sql)==0){
print "no such login in the system. please try again.";
exit();
}
else{
print "successfully logged into system.";
//proceed to perform website’s functionality – e.g. present information to the user
}

?>[/code]

Received this error:

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/.doris/aacatest/test.aacapartsandsupplies.com/members/login.php on line 12
no such login in the system. please try again.
Link to comment
Share on other sites

whoops
I posted my code wrong
I meant to say
Change
[code]
$r = mysql_query($sql);
[/code]
to
[code]
$r = @mysql_query($sql) or die(mysql_error());
[/code]
and
change
[code]
if(mysql_affected_rows($sql)==0){
[/code]
to
[code]
if(mysql_affected_rows($r)==0){
[/code]
Link to comment
Share on other sites

[quote]whoops
I posted my code wrong
I meant to say
Change

Code:
$r = mysql_query($sql);
to

Code:
$r = @mysql_query($sql) or die(mysql_error());
and
change

Code:
if(mysql_affected_rows($sql)==0){
to

Code:
if(mysql_affected_rows($r)==0){[/quote]

That gave me the same error.


[quote]That implies that the query you used is not valid, although it should show up in an mysql_error()[/quote]

I'm not sure how my query could be wrong.. I have a database named tokens with a table named user. username and passwd are two of the fields in the table. Not sure what could be wrong with the query.
Link to comment
Share on other sites

try this:
change
[code]
$sql="SELECT username FROM user WHERE username='$username' and passwd=sha1('$passwd')";
[/code]
to
[code]
$password=sha1($password);
$sql="SELECT username FROM user WHERE username='$username' and passwd='$password'";
[/code]
Link to comment
Share on other sites

No luck. Still saying I'm not registered. I tried creating a new database thinking perhaps I had setup something wrong in the original one. That didn't help. I have used databases with login scripts on this site before and they have worked fine. I'm about ready to pull my hair out!
Link to comment
Share on other sites

You're using the wrong function... [color=red]mysql_affected_rows()[/color] only works after INSERT, UPDATE, REPLACE or DELETE statements.  For SELECT statements you want to use [color=blue]mysql_num_rows()[/color]

Use this:

[code]<?php
// Connect to db
@mysql_connect("mysql.aacapartsandsupplies.com", "admin", "******") or die("Cannot connect to DB!");

// Select db
@mysql_select_db("tokens") or die("Cannot select DB!");

// Run query
$sql = "SELECT username FROM user WHERE username='$username' and passwd=sha1('$passwd')";
$r = mysql_query($sql);
if(!$r){
  $err=mysql_error();
  echo $err;
  exit();
}

// Check if user exists
if(mysql_num_rows() == 0){
  echo "no such login in the system. please try again.";
  exit();
}
else{
  echo "successfully logged into system.";
  // present information to the user
}
?>[/code]

Regards
Huggie
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.