Login code not working

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

Post Reply
tycoon
Forum Newbie
Posts: 6
Joined: Wed Jun 29, 2005 10:02 am

Login code not working

Post by tycoon »

Hi all.....i'm trying to develope login page for my website..the code always seem to redirect to the failed login even though username and password are corect...can anybody please look trough my code

Code: Select all

<?php
session_start();
header("Cache-control: private");

$username = $POST['username'];
$_SESSION['username'] = $username;
$password = $POST['password'];
$_SESSION['password'] = $password;

$db = @mysql_connect('localhost', 'root') or die('could not connect to mysql db, the server return the error:'.mysql_error());
mysql_select_db("timelinesys",$db) or die('could not connect to mysql db, the server return the error:'.mysql_error());
$query = "select * from user where username='$username' and password='$password'";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);

if ($count == 1)
{
header('Location: menu.php'); 
}
else
{
header('Location: loginfailed.php'); 
}
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

looks to be in order everything does. Try just using an if to output results and check valid credentials you should:

ex:

Code: Select all

if($row = mysql_fetch_assoc($sql)){
  header('Location: menu.php');
}else{
  header('Location: loginfailed.php');
}
also try you might to echo the query. The output it will show. some problems you might see...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP-Code.
Please read the link in my sig Posting code in the Forums
tycoon
Forum Newbie
Posts: 6
Joined: Wed Jun 29, 2005 10:02 am

Post by tycoon »

Thank you everybody for guidence and help
p/s:for any conviniece
Post Reply