Not Correct Login

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Not Correct Login

Post by SilverMist »

Hey, I'm trying this script out, but it keeps showing up "Welcome, you're infailed login"

Code: Select all

<?php
      $host = "localhost";
      $dbusername = "user";
      $password = "pass";
      $database = "db";
      $server = mysql_connect($host, $dbusername, $password) or die(mysql_error());                        
      $rs = mysql_select_db($database  , $server);

$username = $HTTP_POST_VARS['txtName'];
$password = $HTTP_POST_VARS['txtPassword'];
$rs = mysql_query("SELECT * FROM Members WHERE Name = '$username' AND Password = '$password' ");

//if the user exists
if (($rs)>0)
{
  //make a cookie for the user
  setcookie("loginname",$username);
} 
{
 echo("welcome. you're in");
}
if (else);
{
 echo("failed login");
}
?>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

Code: Select all

$rs = mysql_query("SELECT * FROM Members WHERE Name = '$username' AND Password = '$password' ");

//if the user exists
if (($rs)>0)
{
  //make a cookie for the user
  setcookie("loginname",$username);
}
{
echo("welcome. you're in");
}
if (else);
{
echo("failed login");
}
should be:

Code: Select all

$rs = mysql_query("SELECT * FROM Members WHERE Name = '$username' AND Password = '$password' ");
//if the user exists
if (mysql_num_rows($rs)>0){
  //make a cookie for the user
  setcookie("loginname",$username);
  echo("welcome. you're in");
}else{
  echo("failed login");
}
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

if (($rs)>0)
Look at that part. $rs will be set if you get results, but also if you do not. Try using that with [php_man]mysql_num_rows[/php_man]().

Edit: I need to be faster... ;)
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

Code: Select all

<?php
//if the user exists
if (($rs)>0)
{
  //make a cookie for the user
  setcookie("loginname",$username);
}
{
echo("welcome. you're in");
}
if (else);
{
echo("failed login");
}
?>
Change to

Code: Select all

<?php
//if the user exists
if (($rs)>0)
{
  //make a cookie for the user
  if(setcookie("loginname",$username))
  {
    echo("welcome. you're in");
  }
    else
  {
    echo "Error setting cookie.";
  }
}
  else
{
  echo("failed login");
}
?>
/Edit... me too... sorry...
Last edited by Goowe on Wed Mar 17, 2004 9:26 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

also - if you set a cookie with no time() value assigned, it will delete itself after the browser closes.

:wink:
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Post by SilverMist »

I tried it, and it says mysql_num_rows is not valid. And then I registered, and when I went to login, it said "failed login"
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

IF your using a recent version of PHP then your goign to want to change those $HTTP_POST_VARS to $_POST
timhortons
Forum Commoner
Posts: 29
Joined: Thu Jan 15, 2004 11:48 am
Location: Calgary, AB, Canada

Post by timhortons »

I wouldnt complicate things by telling him to change variables around illusionist :p

I mean, if he uses the old variables, then at least it might work if it's moved to an older PHP release.

Keep things simple i always say, and newbies dont need to get caught up over those types of things right away!
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Post by SilverMist »

Hey! I am a she thank you very much! But I tried all methods stated, and yet still the script chooses to be a pain in the behind!
timhortons
Forum Commoner
Posts: 29
Joined: Thu Jan 15, 2004 11:48 am
Location: Calgary, AB, Canada

Post by timhortons »

Oops, my bad, sorry!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Might be best to post the code you have now after making your changes.
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

Here's what I'd do.

Code: Select all

<?php
$host = "localhost";
$dbusername = "user";
$password = "pass";
$database = "db";

// Connect to mysql and database
$link = mysql_connect($host, $dbusername, $password)
	or die("<P>Could not connect: ".mysql_error()."</P>");
mysql_select_db($database) 
	or die("<P>Could not select database</P>");                   

$username = $HTTP_POST_VARS['txtName'];
$password = $HTTP_POST_VARS['txtPassword'];

$query = "SELECT * FROM Members WHERE Name = '".$username."' AND Password = '".$password."'";
$result = mysql_query($query) or die("<P>Query failed: ".mysql_error()."</P>");

// Check to see if user exists
if(mysql_num_rows($result) > 0)
{
	// Make cookie for user
	if(setcookie("loginname", $username))
	{
		echo "Welcome";
	}
		else
	{
		echo "Error setting cookie.";
	}
}
	else
{
	echo "Failed login.";
}
?>
http://us2.php.net/manual/en/function.m ... m-rows.php
http://us2.php.net/manual/en/function.mysql-query.php

/Edit: Fixed code
Last edited by Goowe on Wed Mar 17, 2004 9:43 pm, edited 1 time in total.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

complicating? ok...
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Post by SilverMist »

It says Parse Error, Line 9. Possibly from the quotations?
Last edited by SilverMist on Wed Mar 17, 2004 9:44 pm, edited 1 time in total.
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

Reload that code up there, it should work... I had an error
Post Reply