Page 1 of 2

Not Correct Login

Posted: Wed Mar 17, 2004 9:20 pm
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");
}
?>

Posted: Wed Mar 17, 2004 9:23 pm
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");
}

Posted: Wed Mar 17, 2004 9:24 pm
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... ;)

Posted: Wed Mar 17, 2004 9:25 pm
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...

Posted: Wed Mar 17, 2004 9:26 pm
by tim
also - if you set a cookie with no time() value assigned, it will delete itself after the browser closes.

:wink:

Posted: Wed Mar 17, 2004 9:28 pm
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"

Posted: Wed Mar 17, 2004 9:30 pm
by Illusionist
IF your using a recent version of PHP then your goign to want to change those $HTTP_POST_VARS to $_POST

Posted: Wed Mar 17, 2004 9:33 pm
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!

Posted: Wed Mar 17, 2004 9:36 pm
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!

Posted: Wed Mar 17, 2004 9:38 pm
by timhortons
Oops, my bad, sorry!

Posted: Wed Mar 17, 2004 9:38 pm
by markl999
Might be best to post the code you have now after making your changes.

Posted: Wed Mar 17, 2004 9:38 pm
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

Posted: Wed Mar 17, 2004 9:39 pm
by Illusionist
complicating? ok...

Posted: Wed Mar 17, 2004 9:44 pm
by SilverMist
It says Parse Error, Line 9. Possibly from the quotations?

Posted: Wed Mar 17, 2004 9:44 pm
by Goowe
Reload that code up there, it should work... I had an error