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
https://forums.phpfreaks.com/topic/115026-my-login-page-problem/
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.

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());
}
?>

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  ???

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;

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.

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();
?>

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'

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.