Page 1 of 1

private message system => can't log in

Posted: Thu Jan 03, 2013 1:54 pm
by Porkypie
Hey,

I've finished the tutorial for the private messaging system.
But have a small problem with logging in.
When I enter a username and password I don't get redirected to the next part, instead I just get the login page again.
Sadly I don't get any error so I don't have a clue what I about what I should do to fix it.

I was hoping someone here can help me fix my problem.

Thanks in advance!

Porkypie

Re: private message system => can't log in

Posted: Thu Jan 03, 2013 3:49 pm
by ExtremeGaming
You most likely have an undefined variable and your error reporting isn't high enough to display it. You are going to have to post the entire page that contains the form and the processing

Re: private message system => can't log in

Posted: Thu Jan 03, 2013 10:28 pm
by Porkypie
I've managed to fix the problem (forgot one "=" in the form thanks for making me look there ;))

Now I'm stuck on a new problem.

I get the following error message when I try to log in:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\....\core\inc\user.inc.php on line 10

And here's the code:
[syntax=php]
<?php

// controleert gebruikersnaam en wachtwoord combinatie en geeft de gebruikers ID.
Function validate_credentials($user_name, $user_password){
$user_name = mysql_real_escape_string($user_user);
$user_password = sha1($user_password);

mysql_query("select `user_id` from `users` where `user_name` = '{$user_name}' and `user_password` = '{$user_password}'");

if (mysql_num_rows($result) != 1){
return false;
}

return mysql_result($result, 0);
}

function fetch_user_ids($user_names){
foreach ($user_names as &$name){
$name = mysql_real_escape_string($name);
}

$result = mysql_query("select `user_id`, `user_name` from `users`where `user_name` in ('" . implode("', '", $user_names) . "')");

$names = array();

while (($row = mysql_fetch_assoc($result)) !== false){
$names[$row['user_name']] = $row['user_id'];
}

return $names;
}

?>
[/syntax]

I rechecked the tutorial but line 10 seems identical to me.
Does anyone have a clue?

Porkypie

Re: private message system => can't log in

Posted: Thu Jan 03, 2013 10:49 pm
by ExtremeGaming
The problem isn't with line 10 (completely). The real problem is there is no variable named $result. You need to change your query to $result = query so:

[syntax=php]
<?php

// controleert gebruikersnaam en wachtwoord combinatie en geeft de gebruikers ID.
Function validate_credentials($user_name, $user_password){
$user_name = mysql_real_escape_string($user_user);
$user_password = sha1($user_password);

// Changed this
$result = mysql_query("select `user_id` from `users` where `user_name` = '{$user_name}' and `user_password` = '{$user_password}'");

if (mysql_num_rows($result) != 1){
return false;
}

return mysql_result($result, 0);
}[/syntax]

Re: private message system => can't log in

Posted: Thu Jan 03, 2013 11:16 pm
by Porkypie
thanks again!

the error is gone now, but I get a login failed.
sadly no error message :o

Re: private message system => can't log in

Posted: Fri Jan 04, 2013 4:33 am
by ExtremeGaming
$user_user here:
[syntax=php]
$user_name = mysql_real_escape_string($user_user);[/syntax]
Should be $user_name

Re: private message system => can't log in

Posted: Fri Jan 04, 2013 4:01 pm
by Porkypie
Thanks again ExtremeGaming ;)

Took a good look at the video again since most mistakes in this part could have been prevented that way :p
but still the login problem remains.

I hope someone can help me further.

Re: private message system => can't log in

Posted: Sun Jan 06, 2013 12:00 am
by Porkypie
Had another look at my codes but still haven't found the mistake i've made.
I'm not sure where to look for the error either.
So if anyone needs parts of my code to help me out just give a shout and I'll post it ;)
I would be really happy if someone can help me to get the PM system to work :)

Porkypie

Re: private message system => can't log in

Posted: Sun Jan 06, 2013 1:13 am
by ExtremeGaming
Post the login file, as well as the files that is uses (functions, processing, etc.)

Re: private message system => can't log in

Posted: Sun Jan 06, 2013 10:53 am
by Porkypie
I think these are the files you need:

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

<?php

if (isset($_POST['user_name'], $_POST['user_password'])){
echo '<div class="msg error">Login failed.</div>';
}

?>

<form action="index.php?page=login" method="post">
<div>
<label for="user_name">Name</label>
<input type="text" name="user_name" id="user_name" />
</div>
<div>
<label for="user_password">Password</label>
<input type="password" name="user_password" id="user_password" />
</div>
<div>
<input type="submit" value="Login" />
</div>
</form>
[/syntax]

[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('Westpop');

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

if (isset($_POST['user_name'], $_POST['user_password'])){
if (($user_id = validate_credentials($_POST['user_name'], $_POST['user_password'])) !== false){
$_SESSION['user_id'] = $user_id;

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

die();
}
}

if (empty($_SESSION['user_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]
[syntax=php]
<?php

// controleert gebruikersnaam en wachtwoord combinatie en geeft de gebruikers ID.
Function validate_credentials($user_name, $user_password){
$user_name = mysql_real_escape_string($user_name);
$user_password = sha1($user_password);

$result = mysql_query("select `user_id` from `users` where `user_name` = '{$user_name}' and `user_password` = '{$user_password}'");

if (mysql_num_rows($result) != 1){
return false;
}

return mysql_result($result, 0);
}

function fetch_user_ids($user_names){
foreach ($user_names as &$name){
$name = mysql_real_escape_string($name);
}

$result = mysql_query("select `user_id`, `user_name` from `users` where `user_name` in ('" . implode("', '", $user_names) . "')");

$names = array();

while (($row = mysql_fetch_assoc($result)) !== false){
$names[$row['user_name']] = $row['user_id'];
}

return $names;
}

?>
[/syntax]

Re: private message system => can't log in

Posted: Sun Jan 06, 2013 8:49 pm
by Porkypie
Finally found what was going wrong.

Password was encrypted >.<

sadly this still doesn't fix everything. :(

Warning: Invalid argument supplied for foreach() in C:\.........\www\1\core\pages\inbox.page.inc.php on line 36

[syntax=php]
<?php

$errors = array();

if (isset($_GET['delete_conversation'])){
if (validate_conversation_id($_GET['delete_conversation']) === false){
$errors[] = 'Invalid conversation ID.';
}

if (empty($errors)){
delete_conversation($_GET['delete_conversation']);
}
}

$conversations = fetch_conversation_summery();

if (empty($conversations)){
$errors[] = 'U heeft geen berichten.';
}

if (empty($errors) === false){
foreach ($errors as $error){
echo '<div class="msg error">', $error, '</div>';
}
}

?>
<div class="actions">
<a href="index.php?page=new_conversation">Nieuw Gesprek</a>
<a href="index.php?page=logout">Log Uit</a>
</div>

<div class="conversations">
<?php

foreach ($conversations as $conversation){
?>
<div class="conversation <?php if ($conversation['unread_messages']) echo 'unread'; ?>">
<h2>
<a href="index.php?page=inbox&amp;delete_conversation=<?php echo $conversation['id']; ?>">[x]</a>
<a href="index.php?page=view_conversation&amp;conversation_id=<?php echo $conversation['id']; ?>"><?php echo $conversation['subject']; ?></a>
</h2>
<p>Last Reply: <?php echo date('d/m/Y H:i:s', $conversation['last_reply']); ?></p>
</div>
<?php
}

?>
</div>
[/syntax]

Hope someone can help me further again ;)

thanks in advance!

Porkypie

Re: private message system => can't log in

Posted: Mon Jan 07, 2013 12:17 am
by ExtremeGaming
You need to post your fetch_conversation_summery() function. Make sure it's returning an array.

Re: private message system => can't log in

Posted: Mon Jan 07, 2013 12:39 am
by Porkypie
[syntax=php]function fetch_conversation_summery(){
$sql = "select
`conversations`.`conversation_id`,
`conversations`.`conversations_subject`,
max(`conversations_messages`.`message_date`) as `conversation_last_reply`
max(`conversations_messages`.`message_date`) > `conversations_members`.`conversation_last_view` as `conversation_unread`
from `conversations`
left join `conversations_messages` on `conversations`.`conversation_id` = `conversations_messages'.'conversation_id`
inner join `conversations_members` on `conversations`.`conversation_id` = `conversations_members'.'conversation_id`
where `conversations_members`.`user_id` = {$_session['user_id']}
and `conversations_members`.`conversation_deleted` = 0
group by `conversations`.`conversation_id`
order by `conversation_last_reply` desc";

return mysql_error();

$result = mysql_query($sql);

$conversation = array();

while (($row = mysql_fetch_assoc($result)) !== false){
$conversations[] = array(
'id' => $row['conversation_id'],
'subject' => $row['conversation_subject'],
'last_reply' => $row['conversation_last_reply'],
'unread_messages' => ($row['conversation_unread'] == 1),


);
}

return $conversations;
}[/syntax]

Re: private message system => can't log in

Posted: Mon Jan 07, 2013 2:30 am
by ExtremeGaming
Remove the line completely that says:
[syntax=php]return mysql_error();[/syntax]

Re: private message system => can't log in

Posted: Mon Jan 07, 2013 11:02 pm
by Porkypie
I put that line in to prevent an error from happening so when i remove it is just get the old error back.

Re: private message system => can't log in

Posted: Mon Jan 07, 2013 11:29 pm
by Porkypie
I managed to figure it out. there was a mistake in my code.
after removing the:
[syntax=php]return mysql_error();[/syntax]
The error is gone but sadly the other error comes back.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Users\B_Kortekaas\Desktop\Studentpackage\WOS Portable\www\1\core\inc\private_message.inc.php on line 24

[syntax=php]
function fetch_conversation_summery(){
$sql = "select
`conversations`.`conversation_id`,
`conversations`.`conversation_subject`,
max(`conversations_messages`.`message_date`) as `conversation_last_reply`,
max(`conversations_messages`.`message_date`) > `conversations_members`.`conversation_last_view` as `conversation_unread`
from `conversations`
left join `conversations_messages` on `conversations`.`conversation_id` = `conversations_messages'.'conversation_id`
inner join `conversations_members` on `conversations`.`conversation_id` = `conversations_members'.'conversation_id`
where `conversations_members`.`user_id` = {$_SESSION['user_id']}
and `conversations_members`.`conversation_deleted` = 0
group by `conversations`.`conversation_id`
order by `conversation_last_reply` desc";

$result = mysql_query($sql);

$conversations = array();

while (($row = mysql_fetch_assoc($result)) !== false){
$conversations[] = array(
'id' => $row['conversation_id'],
'subject' => $row['conversation_subject'],
'last_reply' => $row['conversation_last_reply'],
'unread_messages' => ($row['conversation_unread'] == 1),


);
}

return $conversations;
}
[/syntax]

Re: private message system => can't log in

Posted: Tue Jan 08, 2013 12:02 am
by ExtremeGaming
First try changing your query to this:
[syntax=php]$sql = "select
`conversations`.`conversation_id`,
`conversations`.`conversation_subject`,
max(`conversations_messages`.`message_date`) as `conversation_last_reply`,
max(`conversations_messages`.`message_date`) > `conversations_members`.`conversation_last_view` as `conversation_unread`
from `conversations`
left join `conversations_messages` on `conversations`.`conversation_id` = `conversations_messages`.`conversation_id`
inner join `conversations_members` on `conversations`.`conversation_id` = `conversations_members`.`conversation_id`
where `conversations_members`.`user_id` = {$_SESSION['user_id']}
and `conversations_members`.`conversation_deleted` = 0
group by `conversations`.`conversation_id`
order by `conversation_last_reply` desc";[/syntax]
Noticed you were using quotes instead of backticks.
If you still receive the error, put the return mysql_error() back in, then on the file that is giving the error, put:

[syntax=php]echo fetch_conversation_summery();[/syntax]

Make sure it is right after your include(); to make sure we get the error message to display. Then post it here.

Re: private message system => can't log in

Posted: Tue Jan 08, 2013 9:02 am
by Porkypie
The mistake was actually in the query. I've rechecked it before I posted this but didn't notice a thing. :oops:
Thanks again!

when I try to send a message with the form and submit nothing happens.
Neither does it add anything to my database.

[syntax=php]<?php

if (isset($_POST['to'], $_POST['subject'], $_post['body'])){
$errors = array();

if (empty($_POST['to'])){
$errors[] = 'U moet minstens één naam invullen.';
}else if (preg_match('#^[a-z, ]+$#i', $_POST['to']) === 0){
$errors[] = 'De lijst van namen is incorrect.';
}else{
$user_names = explode(',', $_POST['to']);

foreach ($user_names as &$name){
$name = trim($name);
}

$user_ids = fetch_user_ids($user_names);

if (count($user_ids) !== count($user_names)){
$errors[] = 'De volgende gebruikers kunnen niet worden gevonden: ' .implode(', ', array_diff($user_names, array_keys($user_ids)));
}
}

if (empty($_POST['subject'])){
$errors[] = 'Uw bericht heeft geen onderwerp.';
}

if (empty($_POST['body'])){
$errors[] = 'U heeft geen bericht getypt.';
}

if (empty($errors)){
create_conversation(array_unique($user_ids), $_POST['subject'], $_POST['$body']);
}
}

if (isset($errors)){
if (empty($errors)){
echo '<div class="msg success">Uw bericht is verzonden! <a href="index.php?page=inbox">Terug naar Inbox</a></div>';
}else{
foreach ($errors as $error){
echo '<div class="msg error">', $error, '</div>';
}
}
}

?>

<form action="" method="post">
<div>
<label for="to">Ontvanger(s):</label>
<input type="text" name="to" id="to" value="<?php if (isset($_POST['to'])) echo htmlentities($_POST['to']); ?>" />
</div>
<div>
<label for="subject">Onderwerp:</label>
<input type="text" name="subject" id="subject" value="<?php if (isset($_POST['subject'])) echo htmlentities($_POST['subject']); ?>" />
</div>
<div>
<label for="body">Type hier uw bericht:</label>
<textarea name="body" rows="20" cols="117"><?php if (isset($_POST['body'])) echo htmlentities($_POST['body']); ?></textarea>
</div>
<div>
<input type="submit" value="verzenden" />
</div>
</form>[/syntax]

The form action is empty but this is also empty in the tutorial.

Could someone tell me how to fix this?

Re: private message system => can't log in

Posted: Tue Jan 08, 2013 12:41 pm
by ExtremeGaming
[syntax=php]if (isset($_POST['to'], $_POST['subject'], $_post['body'])){[/syntax]
$_POST is case-sensitive, change $_post to $_POST as it will always return false the way you have it.

Re: private message system => can't log in

Posted: Tue Jan 08, 2013 4:15 pm
by Porkypie
good to know :P

I think there's something wrong in this part of my code:

[syntax=php]
function create_conversation($user_ids, $subject, $body){
$subject = mysql_real_escape_string(htmlentities($subject));
$body = mysql_real_escape_string(htmlentities($body));

mysql_query("insert into `conversations` (`conversation_subject`) values ('{$subject}')");

$conversation_id = mysql_insert_id();

$sql = "insert into `conversations_messages` (`conversation_id`, `user_id`, `message_date`, `message_text`)
values ({$conversation_id}, {$_SESSION['user_id']}, unix_timestamp(), '{$body}')";

mysql_query($sql);

$values = array("({conversation_id}, {$_SESSION['user_id']}, unix_timestamp(), 0)");

foreach ($user_ids as $user_id){
$user_id = (int) $user_id;

$values[] = "({conversation_id}, {$user_id}, 0, 0)";
}

$sql = "insert into `conversations_members` (`conversation_id`, `user_id`, `conversation_last_view`, `conversation_deleted`)
values " . implode(", ", $values);

mysql_query($sql);
}
[/syntax]

I'm able to add messages to the message table but not able to really start a conversation. So when I log in with another account
I don't see any messages.

Re: private message system => can't log in

Posted: Thu Jan 10, 2013 7:30 pm
by jacek
Not really following this but you're users are not receiving messages it's probably that the insert into `conversations_members` query is failing, which makes sense since you have a typo on this line

[syntax=php]$values[] = "({conversation_id}, {$user_id}, 0, 0)";[/syntax]
it should be

[syntax=php]$values[] = "({$conversation_id}, {$user_id}, 0, 0)";[/syntax]