login page with PHP problem

I’m trying to create a login.php page for my website. Everytime I log in it goes to my “Sorry, could not log you in. Wrong login information.” error. I have seen this code on a few website and now have copied it exactly and it still isnt working. Im just learning PHP… And just dont know what to do… Can anyone take a look and give me a little insight into what is going on…

[php]<?php

session_start();

// dBase file
include “dbconfig.php”;

if ($_GET[“op”] == “login”)
{
if (!$_POST[“username”] || !$_POST[“password”])
{
die(“You need to provide a username and password.”);
}

// Create query  
$q = "SELECT * FROM `contacts` "        
	."WHERE `username`='".$_POST["username"]."' "        
	."AND `password`=PASSWORD('".$_POST["password"]."') " 
	//."AND `password`='".$_POST["password"]."' "  
	."LIMIT 1";  
	
// Run query  
$r = mysql_query($q) or die('Query failed: ' . mysql_error());

if ($obj = mysql_fetch_object($r))   
{        
	// Login good, create session variables        
	$_SESSION["valid_id"] = $obj->id;      
	$_SESSION["username"] = $_POST["username"];        
	$_SESSION["time"] = time();        
	
	// Redirect to member page        
	Header("Location: members.php");        
}  
else        
{        
	// Login not successful        
	die("Sorry, could not log you in. Wrong login information.");        
}  

}
else
{
//If all went right the Web form appears and users can log in
echo “<form action=”?op=login" method=“POST”>";
echo “Username: <input name=“username” size=“15”>
”;
echo “Password: <input type=“password” name=“password” size=“8”>
”;
echo “<input type=“submit” value=“Login”>”;
echo “”;
}

?>[/php]

Any help will be greatly apprieciated!!! Thanks!!

I have altered and commented the script, try it and if it doesn’t work then post any new errors you are having or try echoing out the variables to see where it is going wrong

[php]<?php

session_start();

//checks to see if the there is a user logged in, if the user somehow navigates here while logged in then their username is shown and the time they logged in
if(isset($_SESSION[“username”])) {
$username = $_SESSION[“username”];
$login_time = $_SESSION[“time”];
echo “Username: $username

You have been logged in since: $login_time”;
//logout button should be here

} else {

// dBase file
include “dbconfig.php”;

if ($_GET[“op”] == “login”)
{
if (!$_POST[“username”] || !$_POST[“password”])
{
die(“You need to provide a username and password.”);
}

//creates variables with the post data
$username = $_POST["username"];
$password = $_POST["password"];

// Create & Run query
// no need to limit to one as a username should be unique
$r = mysql_query("SELECT * FROM contacts WHERE username = '$username' AND password = PASSWORD($password)") or die('Query failed: ' . mysql_error());

//gets the number of results found
$num = mysql_num_rows($r);

//if one result is found then the login details are correct, if 0 results found then they are incorrect
if ( $num != '0')   
{      
	//puts the users data into an array called row
	$row= mysql_fetch_array($r)  
	
	// Login good, create session variables  
	//$row['id'] gets the users id from the database, assuming you have a column called 'id'      
	$_SESSION["valid_id"] = $row['id'];      
	$_SESSION["username"] = $username       
	$_SESSION["time"] = time();        
	
	// Redirect to member page        
	Header("Location: members.php");        
}  
else        
{        
	// Login not successful        
	die("Sorry, could not log you in. Wrong login information.");        
}  

}
else
{
//If all went right the Web form appears and users can log in
echo “<form action=”?op=login" method=“POST”>";
echo “Username: <input name=“username” size=“15”>
”;
echo “Password: <input type=“password” name=“password” size=“8”>
”;
echo “<input type=“submit” value=“Login”>”;
echo “”;
}
}
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service