Free Web Hosting Forum
(#1 (permalink))
Old
Junior Member
Krestalve is on a distinguished road
 
Posts: 4
Join Date: Sep 2011
Default php login - 09-19-2011, 03:05 AM

My login code doesn't seem to be properly searching my database. I made a simple database with one username/password to test it out and there is always an error saying that it is the wrong username/password. Can you take a look and help me out?

PHP Code:
<?php
$host
="localhost"// Host name
$username="username"// Mysql username
$password="password"// Mysql password
$db_name="login"// Database name
$tbl_name="registered"// 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");

// username and password sent from form
$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_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo 
"Wrong Username or Password";
}
?>
Is there something wrong with the script? I'm a total php noob and am trying to figure this out as I go.
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-19-2011, 03:26 AM

My answer is prepared assuming that your site is hosted in 000webhost.com.

In your code $host, $username and $db_name are not correctly defined.
With your code, even database connection is not possible, and it's strange
that you did not get any error message on the database connection failure.
For details, please refer to the following thread posted by me:

Insert data into mysql database

It will be helpful if you read more posts in the same thread.

If your site is hosted by other webhosting company, please disregard my answer above.

Last edited by grace1004; 09-19-2011 at 03:53 AM.
Reply With Quote
(#3 (permalink))
Old
Junior Member
Krestalve is on a distinguished road
 
Posts: 4
Join Date: Sep 2011
Default 09-19-2011, 04:02 AM

are you talking about $host, $username and $db_name in themselves, or what is in the quotation marks? I just put generic stuff in the quotation marks so it didn't have my personal login information... and I was asking the same question in another forum, so didn't want to have host-specific information in there...
if you mean $host, $username and $db_name themselves... ugh... lol...
Reply With Quote
(#4 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-19-2011, 04:48 AM

If you added comments that you put generic stuff, it would not have misled me ... lol.
Your code looks OK. However, if $mypassword is encrypted you will get the message,
"Wrong Username or Password." So, please connect to your database or enter into
phpMyAdmin and see if password is encrypted in your table.
Reply With Quote
(#5 (permalink))
Old
Junior Member
Krestalve is on a distinguished road
 
Posts: 4
Join Date: Sep 2011
Default 09-19-2011, 05:01 AM

I had just noticed that part as well and tried to fix using the following (only posting the part I changed):

PHP Code:
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// encrypt password
$encrypted_mypassword=md5($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='$encrypted_mypassword'";
$result=mysql_query($sql); 
Reply With Quote
(#6 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-19-2011, 05:11 AM

PHP Code:
<?
if($count==1){ 
// Register $myusername, $mypassword and redirect to file "login_success.php" 
session_register("myusername"
session_register("mypassword"); 
header("location:login_success.php"); 

else { 
echo 
"Wrong Username or Password"

?>
In the recent version of php, $_SESSION variable is used instead of session_registetr().
If you use session_register() in register_globals=Off environment, it will not work.
So, I suggest the following code:

PHP Code:
<?
session_start
(); // if you want to use session, this should be added at the top.
.
.

if(
$count == 1) { 
// Register $myusername, $mypassword and redirect to file "login_success.php" 
$_SESSION['myusername'] = $row['username']; 
$_SESSION['mypassword'] = $row['password'];  
header("location:login_success.php"); 

else { 
echo 
"Wrong Username or Password"

?>

Last edited by grace1004; 09-19-2011 at 09:55 AM.
Reply With Quote
(#7 (permalink))
Old
Member
Passionless is on a distinguished road
 
Posts: 48
Join Date: Sep 2011
Default 09-19-2011, 07:39 AM

Hi,
It works here as I test. It could be something with $_POST or something with your DB schema.
A mistake I made "once" is to have a, say, VARCHAR(20) column and try to store MD5 hashed there (32 chars). The inserts success with a warning and the selects never match anything.

BTW instead of...
PHP Code:
// encrypt password 
$encrypted_mypassword=md5($mypassword); 
You can
PHP Code:
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password=md5('$mypassword')"
$result=mysql_query($sql); 
Good luck.
Reply With Quote
(#8 (permalink))
Old
Junior Member
Krestalve is on a distinguished road
 
Posts: 4
Join Date: Sep 2011
Default 09-19-2011, 02:51 PM

omg i'm such an idiot, hahaha... that was it... I was set to varchar 20 >_>
THANK YOU! Such a stupid easy fix, hahaha... It's the little things that escape us, ya?
Reply With Quote
Reply

Thread Tools
Display Modes

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com