Username & Password

This is a discussion on "Username & Password" within the PHP Forum section. This forum, and the thread "Username & Password are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 28th, 2007, 14:50
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Username & Password

Hi I am having problems creating a user login feature. I have created a form to allow users to create a username and password which then adds this information to mysql.

When i try to login it says:

no such login in the system. please try again.

The login name and password is correct so it has to be my process code which is:

PHP: Select all

<?php

$host 
'localhost'
$user 'username';
$pass 'password';
$db 'users';
$table 'members';

$dbh=mysql_connect ("$host""$user""$pass") or die ('I cannot connect to the database because:' mysql_error());
mysql_select_db ("$db"); 
$sql="SELECT Username FROM $table WHERE Username='".$Username."' and Password='".$Password."'";
$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.";
}

?>
Can anyone help me with why it is not working, or is there a better way of doing it?


Thanks
Robert

Last edited by karinne; Mar 28th, 2007 at 14:53. Reason: Please use [php]...[/php] tags when displaying PHP code!
Reply With Quote

  #2 (permalink)  
Old Mar 28th, 2007, 14:54
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

Where do you grab $Username and $Password? I don't see those variables assigned to anything anywhere in your code.
Reply With Quote
  #3 (permalink)  
Old Mar 28th, 2007, 14:59
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

This is my signin Form:

<FORM ACTION="authenticate.php" METHOD="get">
USERNAME: <INPUT NAME="Username" TYPE="text" ID="Username"><BR><BR>
PASSWORD: <INPUT NAME="Password" TYPE="text" ID="Password"><BR><BR>
<INPUT NAME="Login" TYPE="submit">
</FORM>

so how would i declare those two variables in php script, like this?

$Username =$_POST['Username'];
$Password =$_POST['Password'];


Thanks
Robert
Reply With Quote
  #4 (permalink)  
Old Mar 28th, 2007, 15:02
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

So ... authenticate is the code you posted previously?

Then yes ... by using

PHP: Select all

 $Username =$_POST['Username'];
$Password =$_POST['Password']; 
in there, you will be assigning the input to those variables.

I'll move this to the php forum since your problem lies with the coding and not the database
Reply With Quote
  #5 (permalink)  
Old Mar 28th, 2007, 15:19
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

hmmm still not working when i tried that and authenticate is the code i posted previously.

The username and password are correct i have checked the entries in the database i entered on other form.

What else could be wrong?

Thanks
Robert

Last edited by robukni; Mar 28th, 2007 at 16:13.
Reply With Quote
  #6 (permalink)  
Old Mar 28th, 2007, 21:49
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

add $r to

PHP: Select all

if(mysql_affected_rows()==0){ 

so it becomes

PHP: Select all

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

try that and see.
Reply With Quote
  #7 (permalink)  
Old Mar 29th, 2007, 03:37
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

Hmm tried that brought up an error. I cant figure it out at all.

Have to look some more at it


Thanks
Robert
Reply With Quote
  #8 (permalink)  
Old Mar 29th, 2007, 07:55
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

You're not using any type of encryption are you? Perhaps the password is encrypted and you're not encrypting the submitted password before checking it against the database?
Reply With Quote
  #9 (permalink)  
Old Mar 29th, 2007, 12:14
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

Ok ... try changing this

PHP: Select all

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

to

PHP: Select all

if(!mysql_num_rows($r)){ 

Reply With Quote
  #10 (permalink)  
Old Mar 29th, 2007, 14:22
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

Hi ok that worked thanks.

One more question how do i load a certain page when they successfully login?

At the minute if they just type the url in of the page it will still bring it up without logging in.

Difficult to explain hope you know what i mean.


Thanks
Robert
Reply With Quote
  #11 (permalink)  
Old Mar 29th, 2007, 18:30
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

Ok for the above question i have came accross the following code

PHP: Select all

<?php
session_start
();if ( $log_out )
{
session_unregister"valid_user" );
session_destroy();
session_start();
}
function 
write_log_in$text )
{
echo 
"

$text

<form method='post' action=''><br />
<p>User ID: <input type='text' name='user_name/><br />
<p>Password: <input type='password' name='password'/><br />
<p><input type='submit' value='Log In'/><br/>
</form>
"
;

// end write_log_in function
function verify()
{
// check to see if they’re already logged in
if ( session_is_registered"valid_user" ) ) return true;
// check to see if visitor has just tried to log on
$user_name $_POST["user_name"];
$password $_POST["password"];


if ( 
$user_name && $password )
{
// verify password and log in to database
$db mysql_pconnect"localhost""$user_name""$password" );
if ( 
$db )
{
// register session variable and exit the verify function
$valid_user $user_name;
$_SESSION['valid_user'] = $valid_user;
return 
true;
}
else
{
// bad user and password
$text "User Name and Password did not match";
write_log_in$text );
}
}
else
{
// user must log in
$text "This is a secure server. Please log in.";
write_log_in$text );
}
// end verify function
?>

<html>

<head>
<title></title>

<body>


<?php
// check for valid user
if ( verify() )
{
echo 
"

Log out"
;
// begin secure content
echo "

Clatu, verata, nicto

"
;
// end secure content
// end if ( verify() )
?>
</body>
</html>
This code will work but the problem i have is i want to add the fuction:

PHP: Select all

select "select user_name from users
where user_name='$user_name'
and password=PASSWORD( '$password' )"
;
$query mysql_query$select );
if ( 
mysql_num_rows$query ) == )
{
// validated user and password 
How would i do this?

Thanks
Robert

Last edited by karinne; Mar 30th, 2007 at 15:00. Reason: Please use [php]...[/php] tags when displaying code!
Reply With Quote
  #12 (permalink)  
Old Mar 30th, 2007, 14:51
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

If you are checking for a unique user name, do that as a distinct module and forget the password. Then check the password once you have validated the unique user name.

Security issues: 1) The change to POST is a good one. 2) You very badly need to take the database connection out of this file and put it into a separate file stored in your root directory, where it cannot be accessed directly over the internet. Then include it at the top of your php (require_once). Like this:
Code: Select all
<?php
#Script 7.0 - mysql_connect.php March 30 2007
DEFINE ('DB_USER' , 'myfile_user');
DEFINE ('DB_PASSWORD' , '888mypw3x');
DEFINE ('DB_HOST' , 'localhost');
DEFINE ('DB_NAME' , 'myfilez_mydatabasename');
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL:' . mysql_error() );
@mysql_select_db (DB_NAME) OR die ('Could not select database' . mysql_error() );

//escape data function
function escape_data ($data)  {
    if (ini_get('magic_quotes_gpc'))  {
    $data = stripslashes($data);
    }
    if (function_exists('mysql_real_escape_string'))  {
        global $dbc;
        $data = mysql_real_escape_string(trim($data), $dbc);
    }  else  {
    $data = mysql_escape_string(trim($data));
}
return $data;
}
?>
You would save this file in your root directory as, say, 'mysql_login_connect.php'. I have obviously included a stripslash feature that should work in PHP 4x and 5x. Just remove it if you want. You could end this file at the fifth line:
Code: Select all
OR die ('Could not connect to MySQL');
Then drop down one level into the public_html directory and put something like this at the top of your login page. I've just copied the first lines of a working file (both examples are just copies of actual files with name & password changes) so excuse any extraneous matter:
Code: Select all
<?php
//Script 9.1 - login.php  July 4, 2006
//Send NOTHING to the browser prior to the setcookie() lines (I left this comment in, in case you want to use this for cookies

if (isset($_POST['submitted']))  {
    require_once('../mysql_login_connect.php');

// Note - the '../' accesses the root directory, which can be done from your php script
 but not from a web browser directly.  If this was a subfile in say "mysite", you would use '../../', and so on.

    $errors = array();
                                                    
    if (empty($_POST['email']))  {
        $errors[] = 'You forgot to enter your email';
    }  else  {
        $e = escape_data($_POST['email']);
    }
Obviously this contains some stuff like references to my error reporting method, which you can ignore, adopt, or replace.
Reply With Quote
  #13 (permalink)  
Old Mar 30th, 2007, 14:54
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

Ah ... thanks for the tip Mason!
Reply With Quote
  #14 (permalink)  
Old Apr 4th, 2007, 22:36
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Username & Password

Hi thanks for your reply. I have got my login system working and made those two changes which are POST and put the DB login to separate page.

What other measures can i take to improve security?
I was thinkin about password encryption maybe.

my code to create account is s follows

Code: Select all
<?php 
if ((!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) )
{
echo " The information you entered is not valid. <A HREF=purchase.php>Click here</A> to try again.";
}
else
{
include("dbconnect.php"); 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';"; 
$qry = mysql_query($check)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 
if ($num_rows != 0) { 

echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=linkname.php>Try again</a>";
exit; 
} else {

// insert the data
$insert = mysql_query("insert into $table values ('', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['name']."', '".$_POST['address']."', '".$_POST['email']."', '".$_POST['fax']."', '".$_POST['telephone']."')")
or die("Could not insert data because ".mysql_error());

// print a success message
echo "Your user account has been created!<br><br>"; 
echo "Please login using the menu on the right"; 
}
}
?>

Another problem if anyone can help me with.
The service i am creating this for will have to be bought. but at the minute when i submit this the user will have full access to the website.

The process is register - buy - finished.

How do i prevent the user from having access after regsitering until they pay through paypal??

I am using payapl at the moment as i am not experienced with E-Commerce.

Any suggestions welcome.


Thanks
Robert

Last edited by JacobHaug; Apr 5th, 2007 at 02:25. Reason: Added [code]...[/code] tags around code!
Reply With Quote
Reply

Tags
login feature

Thread Tools

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Forgot password and Change password PHP script Chono PHP Forum 4 May 16th, 2008 09:13
input $_SESSION['username'] = $username; on login page help Aaron1988 PHP Forum 2 Jan 28th, 2008 14:14
Need help with Username and Password database.!asap tcpodg PHP Forum 8 Sep 26th, 2007 14:12
urgent advise needed please, username and password marie2007 Databases 7 Jun 7th, 2007 12:51
what is default sqlserver username & password ? clickme_not Databases 1 Aug 27th, 2006 17:57


All times are GMT. The time now is 11:11.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43