Private_Message_System - Can't seem to Login?

Ask about a PHP problem here.
Post Reply
NewToPHP
Posts: 3
Joined: Tue Aug 28, 2012 5:34 pm

Private_Message_System - Can't seem to Login?

Post by NewToPHP »

Hi,

I was going through your "Private Message System Tutorial", everything was going smoothly, until I reached near the end of Video 04.

For some reason, I can't seem to login to the inbox. I type in a correct username, and password from my database into the login fields, and I get the "Login Failed" error.

I'm new to PHP, and a novice at coding, but I have had some experience here and there with coding. As far as I can tell, I entered everything in correctly.

NOTE: My database has the fields id, username, and password. Not user_id, user_name, and user_password. My Database is named "seas".

index.php:
[syntax=php]<?php

include("core/init.inc.php");
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="ext/css/main.css" />
<title>Really Good Message Thingy</title>
</head>
<body>
<div id="wrap">
<?php

include($include_file);

?>

</div>
</body>[/syntax]

init.inc.php:
[syntax=php]<?php

$core_path = dirname(__FILE__);
if (empty($_GET['page']) ||
in_array("{$_GET['page']}.page.inc.php", scandir("{$core_path}/pages")) == false){
header('HTTP/1.1 404 Not Found');
header('Location: index.php?page=inbox');

die();
}

session_start();

mysql_connect("localhost", "root", "");
mysql_select_db("seas");

include("{$core_path}/inc/users.inc.php");

if (isset($_POST['username'], $_POST['password'])){
if (($user_id = validate_credentials($_POST['username'], $_POST['password'])) !== false){
$_SESSION['id'] = $user_id;

header('Location: index.php?page=inbox');
die();
}
}

if (empty($_SESSION['id']) && $_GET['page'] !== 'login'){
header('HTTP/1.1 403 Forbidden');
header('Location: index.php?page=login');
die();
}


$include_file = "{$core_path}/pages/{$_GET['page']}.page.inc.php";

?>[/syntax]

Login.page.inc.php:

[syntax=php]<h1>Login</h1>

<?php

if (isset($_POST['username'], $_POST['password'])){
echo '<div class="msg error">Login Failed.</div>';

}

?>
<form action="index.php?page=login" method="post">
<div>
<label for="username"> Name</label>
<input type="text" name="username" id="username" />
</div>

<div>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</div>
<div>
<input type="submit" value="Login" />
</div>
</form>[/syntax]

users.inc.php:
[syntax=php]<?php
// Checks a given username and password combination, returning the users ID
function validate_credentials($username, $password){
$username = mysql_real_escape_string($username);
$password = sha1($password);

$result = mysql_query("SELECT id FROM users WHERE username = '$username' AND password = '$password'");

if (mysql_num_rows($result) != 1){

return false;
}
return mysql_result($result, 0);
}
?>[/syntax]
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Private_Message_System - Can't seem to Login?

Post by FrederickGeek8 »

In Login.page.inc.php
You have
[syntax=php]if (isset($_POST['username'], $_POST['password'])){
echo '<div class="msg error">Login Failed.</div>';

}[/syntax]

Which was making it so, if the username and password are set then it gives you an error, soooooooo you want

[syntax=php]if (isset($_POST['username'], $_POST['password']) === false){
echo '<div class="msg error">Login Failed.</div>';

}[/syntax]


The way I think of it is isset is two words, is and set. So if you put it in a sentence:
If the username and password are(is) set then bla bla bla
NewToPHP
Posts: 3
Joined: Tue Aug 28, 2012 5:34 pm

Re: Private_Message_System - Can't seem to Login?

Post by NewToPHP »

Tried that fix, it didn't work. (Still won't login)

(The Video Tutorial shows it as the way I had it)
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Private_Message_System - Can't seem to Login?

Post by Helx »

You may have to try cookies.
NewToPHP
Posts: 3
Joined: Tue Aug 28, 2012 5:34 pm

Re: Private_Message_System - Can't seem to Login?

Post by NewToPHP »

Turns out that my passwords weren't sha1 encrypted.

Now everything works fine.

Thanks for the help.
Post Reply