Jump to content

My login page problem


daveeboi

Recommended Posts

I currently have a problem with my login page basically when checking my user login details.  I am entering details for a test user that I know is the correct username and password yet I'm still being dirested to my failed login page.

 

My code in the checklogin.php is :

<?php
ob_start();
$host="my host name"; // Host name
$username="my username"; // Mysql username
$password="my pword"; // Mysql password
$db_name="my db name"; // Database name
$tbl_name ="my reg members table";//table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = "$myusername";
$_SESSION["mypassword"] = "$mypassword";
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

 

Anything glaringly obvious that I'm totally missing?

Link to comment
Share on other sites

try to verify the username and password by directly see if they equal one another rather then counting the rows from the database..

 

 

if it works that way then you know for sure there is more then one entry..

 

other wise I guess you could just look at the DB if you can do that.

Link to comment
Share on other sites

also if this wasnt done while submitting your username and password orginally there maybe differences in them thats making the login fail..

 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

 

 

also is the query failing??

 

run the query through an if statement to see if its really successfull

<?php
if($sql)
{
echo("SUCCESS!!");
}
else
{
echo("FAIL!!!".mysql_error());
}
?>

Link to comment
Share on other sites

The sql function works, I think it's something to do with the line

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

 

See anything there?

Link to comment
Share on other sites

I think i might have found the problem...

 

try this

 

change

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

 

 

to this

 

$sql="SELECT * FROM $tbl_name WHERE username=' ".myusername." ' and password=' ".$mypassword ." ' ";

 

Link to comment
Share on other sites

When I was testing parts of the code earlier I'd found that the previous error came from the SESSION part in my login success.php but afetr entering your ammended code part there I am still getting an error but now it's the error from the checklogin.php I posted earlier "Worng username or password".  I'm confused  ???

Link to comment
Share on other sites

now that i'm looking for it i found these too

 

$_SESSION['myusername'] = "$myusername";
$_SESSION["mypassword"] = "$mypassword";

 

written in the above fashion makes them STRINGS

 

we want them to be parsed not outputted

 

change to :

$_SESSION['myusername'] = $myusername;
$_SESSION["mypassword"] = $mypassword;

Link to comment
Share on other sites

Ok, with the code using the line

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

 

I get the error from my login_success.php file from the

if(!isset($_SESSION['myusername']))

line I have there.

 

If I use this line of code in the file

 

$sql="SELECT * FROM $tbl_name WHERE username=' ".myusername." ' and password=' ".$mypassword ." ' ";

 

I get the 'Wrong username or password' error from the checklogin.php file.

Link to comment
Share on other sites

The ccode as is just now is :

 

<?php
ob_start();
$host="--------"; // Host name
$username="-------"; // Mysql username
$password="------"; // Mysql password
$db_name="------"; // Database name
$tbl_name ="registered_members";//table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

Link to comment
Share on other sites

you still need to change this line

 

<?php

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

?>

 

its going into the database as

(result)

SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'

 

it should be like this:

<?php

$sql="SELECT * FROM ". $tbl_name ." WHERE username=' ". $myusername ." ' and password=' ". $mypassword ." '";

?>

 

and would go to the database as this

 

SELECT * FROM value_of_tbl_name WHERE username='value_of_myusername' and password='value_of_mypassword'

Link to comment
Share on other sites

Ok, thanks anyway for trying.  Really can't understand it myself, it looks fine to me.  Might just take the easy way out and download a working script and alter it accordingly and just put this one down to unsolved mystery.  Thanks again.

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.