Page 1 of 1

Email notifications won't jump to correct post

Posted: Tue Sep 22, 2009 11:42 am
by anivad
For my forums script, the page to view the thread and post replies includes this under the new reply form:

Code: Select all

print "<input type='hidden' name='pageid' value='" . $p . "'>";
On execution, this other script comes into play, and includes:

Code: Select all

$p = $_POST['pageid'];
When the reply is successfully posted and inserted into the MySQL database, this gets executed; firstly:

Code: Select all

header('Location: http://mysite.com/view.php?threadid=' . $threadid . "&p=" . $p . "#" . $postid);
which works completely fine. The page reloads with the page number and anchor link both working. BUT in this bit that follows right after for the e-mail alerts:

Code: Select all

$message = "$author has posted a reply to the topic '$subject' at http://mysite.com/view.php?threadid=$threadid&p=$p#$postid.
the $p mysteriously always changes to '1' in the resulting e-mail alert, even though the header manages to keep the actual value. $threadid and $postid are both correct; only $p isn't. Any idea why this happens? Help would be really appreciated; it's been bugging me for a while.

Re: Email notifications won't jump to correct post

Posted: Tue Sep 22, 2009 11:58 am
by jackpf
Can you post the whole script?

Also, try var_dump() 'ing $p in various places to see where it loses its value.

Re: Email notifications won't jump to correct post

Posted: Tue Sep 22, 2009 12:05 pm
by anivad
There is no break at all between the two lines: in the first, $p has its value, and in the very next one it reverts to '1', so it's not so much a matter of where it loses its value as why...

Whole code for the view and post-reply page:

Code: Select all

<?
 
include '../db.php';
include 'ascheck.php'; 
 
$p = $_GET['p'];
$y = 20;
if ($p == '') {
$p = 1;
$x = 0;
}
 
elseif ($p != '') {
$x = ($p - 1) * 20;
}
 
$threadid = $_GET['threadid'];
$sql = "SELECT * FROM f_threads WHERE threadid='$threadid'";
$result = mysql_query($sql);
$info = mysql_fetch_object($result);
$forumid = ($info->forumid);
$subject = ($info->subject);
 
$fsql = "SELECT * FROM forums WHERE forumid='$forumid'";
$fresult = mysql_query($fsql);
$finfo = mysql_fetch_object($fresult);
$forumname = ($finfo->forumname);
print "<html>";
include 'forumsformat.htm';
 
print "<center><p><b><a href='forumsindex.htm'>Forums</a> :: <a href='display.php?forumid=" . $forumid . "'>" . $forumname . "</a> :: <u>" . $subject . "</u></b>";
 
if ($forumid == 10 && $scheck) {
print "<p><table border='1' width='760'>";
print "<tr><td colspan='4' class='h'><b>This forum is members only.</b></td></tr></table>";
}
elseif ($forumid == 9 && $scheck) {
print "<p><table border='1' width='760'>";
print "<tr><td colspan='4' class='h'><b>This forum is members only.</b></td></tr></table>";
}
elseif ($forumid == 11 && $scheck) {
print "<p><table border='1' width='760'>";
print "<tr><td colspan='4' class='h'><b>This forum is members only.</b></td></tr></table>";
}
 
else {
 
$csql = "SELECT * FROM f_posts WHERE threadid='$threadid' ORDER BY postdate";
$cresult = mysql_query($csql);
$rc = mysql_num_rows($cresult);
print "<p>";
 
if($rc > 20) {
$a = $x;
$b = $x + 20;
$pgn = ($rc / 20) + 1;
for($i=1; $i<$pgn; $i++) {
 
if($i == $p) {
print "[<font color='#383838'><b>" . $i . "</font></b>] ";
}
elseif($i != $p) {
print "<a href='view.php?threadid=" . $threadid . "&p=" . $i . "'>" . $i . "</a> ";
}
}
}
 
$sql = "SELECT * FROM f_posts WHERE threadid='$threadid' ORDER BY postdate ASC LIMIT $x, $y";
$result = mysql_query($sql);
 
print "<table border='1' width='760'>";
print "<tr><td colspan='4' class='h'><b>" . $subject . "</b></td></tr>";
 
while($info = mysql_fetch_object($result)) {
 
print "<tr><td class='desc' width='120'><a name='" . $info->postid . "'><b>";
 
if($info->post == '[ Post deleted by poster ]') {
print "</b><td class='desc'>" . $info->postdate . "</td></tr><tr><td></td><td class='m'>" . $info->post . "</td></tr>";
}
 
else{
 
$asql = "SELECT * FROM login WHERE uname='$info->author'";
$aresult = mysql_query($asql) or die (mysql_error());
 
    if ($aresult) {
$data = mysql_fetch_assoc($aresult);
$userid = $data['userid'];
$avatar = $data['avatar'];  
 
print "<a href='../profile.php?userid=" . $userid . "'>" . $info->author . "</a></b></td><td class='desc'>" . $info->postdate . "</td></tr><tr><td class='c' valign='top'>";
    
if ($avatar != '') {
print "<br>";
print "<img src='../" . $avatar . "'>";
}
}
 
$sql = "SELECT * FROM comments WHERE cname='$info->author'";
$cresult = mysql_query($sql);
$num_rows = mysql_num_rows($cresult);
    $ccount = $num_rows;
 
$sql = "SELECT * FROM kmdbreviews WHERE rname='$info->author'";
$rresult = mysql_query($sql);
$num_rows = mysql_num_rows($rresult);
    $rcount = $num_rows;
    
        
$fsql = "SELECT * FROM f_posts WHERE author='$info->author'";
$fresult = mysql_query($fsql) or die (mysql_error());
$fpostcount = mysql_num_rows($fresult);
 
$tcount = $fpostcount + $rcount + $ccount;
 
 
if($tcount < 50) {
print "<br><img src='../pictures/pip.png'>";
}
 
elseif($tcount >= 50 && $tcount < 100) {
print "<br><img src='../pictures/pip.png'> <img src='../pictures/pip.png'>";
}
elseif($tcount >= 100 && $tcount < 250) {
print "<br><img src='../pictures/pip.png'> <img src='../pictures/pip.png'> <img src='../pictures/pip.png'>";
}
elseif($tcount >= 250) {
print "<br><img src='../pictures/pip.png'> <img src='../pictures/pip.png'> <img src='../pictures/pip.png'> <img src='../pictures/pip.png'>";
}
 
print "<br><span>Forum Posts: <b>" . $fpostcount . "</b><br>Comments: <b>" . $ccount . "</b><br>Reviews: <b>" . $rcount . "</b></span></td>";
 
$post = stripslashes(nl2br($info->post));
$post = eregi_replace("(\[img\])", "<img src='", $post);
$post = eregi_replace("(\[/img\])", "'>", $post);
 
print "<td class='m' valign='top'><div style='width: 600px; overflow: auto'>" . $post . "</div>";
 
if ($info->author == $uname) {
print "<br><form name='edit' method='post' action='editpost.php'>";
print "<input type='hidden' name='postid' value='$info->postid'>";
print "<input type='hidden' name='post' value='$info->post'>";
print "<input type='hidden' name='forumid' value='$info->forumid'>";
print "<input type='hidden' name='forumname' value='$forumname'>";
print "<input type='hidden' name='subject' value='$subject'>";
print "<input type='image' src='../pictures/edit.png' name='ep'>";
print "</form>";
}
 
print "</td></tr>";
 
}
}
 
print "<table>";
 
if($rc > 20) {
$a = $x;
$b = $x + 20;
$pgn = ($rc / 20) + 1;
for($i=1; $i<$pgn; $i++) {
 
if($i == $p) {
print "[<font color='#383838'><b>" . $i . "</font></b>] ";
}
elseif($i != $p) {
print "<a href='view.php?threadid=" . $threadid . "&p=" . $i . "'>" . $i . "</a> ";
}
}
}
 
print "<hr>";
 
if(!$scheck) {
 
print "<p><center><b>Add Reply</b></center>";
 
print "<form name='postform' method='post' action='addreply.php'>";
print "<input type='hidden' name='forumid' value='" . $forumid . "'>";
print "<input type='hidden' name='forumname' value='" . $forumname . "'>";
print "<input type='hidden' name='threadid' value='" . $threadid . "'>";
print "<input type='hidden' name='pageid' value='" . $p . "'>";
 
print "<table border='0'><tr>";
 
print "<td class='c'></td><td class='c'><b>" . $uname . "</b></td></tr>";
print "<input type='hidden' name='author' value='" . $uname . "'>";
print "<tr><td></td><td class='c'><font color='#383838'>Formatting: [b]bold[/b], [i]italics[/i], [u]underline[/u], [url]link[/url], [img]image[/img], [quote]quote[/quote], [strike]strikethrough[/strike].</font></td></tr>";
print "<tr><td class='c' align='right'><b>Post</b></td><td class='c'><textarea id='post' name='post' cols='80' rows='15' wrap='VIRTUAL' font='verdana'></textarea></td></tr>";
 
 
$esql = mysql_query("SELECT * FROM f_email WHERE threadid='$threadid' AND user='$uname'");
$enum = mysql_num_rows($esql);
 
if($enum == 0){
print "<tr><td></td><td class='c'><input type='checkbox' name='notify' value='notify'><b>E-mail me when a reply is posted to this topic</b></td></tr>";
}
else {
print "<tr><td></td><td class='c'><b>Email notification has been enabled for this topic. To disable e-mail notification for this topic, <a href='http://www.mysite.com/forums/disablen.php?id=" . $threadid . "'>click here</a>.</b></td></tr>";
}
 
print "<tr><td class='c' align='right' colspan='2'>";
print "<input type='submit' name='submit' value='Add Reply'></td></tr>";
 
print "</table></form>";
 
}
 
else {
print "<p><b>You must be registered and logged in to post on the forums.</b></table><p>";
}
 
}
?>
 
 
</div>
 
<? include '../footer.htm' ?>
 
</html>
And this is 'addreply.php', executed upon post:

Code: Select all

<?
 
include '../db.php';
include '../common.php';
 
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 
$post = $_POST['post'];
 
if($post == '') {
error('Post field left empty');
}
else {
$post = mysql_real_escape_string($post);
$post = htmlspecialchars($post);
$post = eregi_replace('(\\\')', ''', $post);
$post = eregi_replace('(\[b\])', '<b>', $post);
$post = eregi_replace('(\[/b\])', '</b>', $post);
$post = eregi_replace('(\[i\])', '<i>', $post);
$post = eregi_replace('(\[/i\])', '</i>', $post);
$post = eregi_replace('(\[u\])', '<u>', $post);
$post = eregi_replace('(\[/u\])', '</u>', $post);
$post = eregi_replace('(\[strike\])', '<u>', $post);
$post = eregi_replace('(\[/strike\])', '</strike>', $post);
$post = preg_replace('#(\[url\])(.+?)(\[\/url\])#', '<a href="\\2">\\2</a>', $post);
$post = preg_replace('#(\[img\])(.+?)(\[\/img\])#', '<img src="\\2">', $post);
$post = preg_replace('#(\[quote\])(.+?)(\[\/quote\])#', '<blockquote><hr><font color="#383838">\\2</font><hr></blockquote>', $post);
 
 
$forumid = $_POST['forumid'];
$threadid = $_POST['threadid'];
$forumname = $_POST['forumname'];
$postdate = date('Y-m-d H:i');
$author = $_POST['author'];
$p = $_POST['pageid'];
 
$notify = $_POST['notify'];
 
$psql = "INSERT INTO f_posts SET forumid='$forumid', threadid='$threadid', author='$author', post='$post', postdate='$postdate'";
$presult = mysql_query($psql);
 
$lpsql = "UPDATE f_threads SET latestpostdate='$postdate' WHERE threadid='$threadid'";
$lpresult = mysql_query($lpsql);
 
$sub = mysql_query("SELECT * FROM f_threads WHERE threadid='$threadid'");
$sinfo = mysql_fetch_object($sub);
$subject = $sinfo->subject;
 
$postid = mysql_query("SELECT * FROM f_posts WHERE threadid='$threadid' AND postdate='$postdate' AND author='$author'");
$postid = mysql_fetch_object($postid);
$postid = $postid->postid;
 
if($notify) {
$asql = mysql_query("SELECT * FROM login WHERE uname='$author'");
$ainfo = mysql_fetch_object($asql);
$email = $ainfo->email;
 
$nsql = mysql_query("INSERT INTO f_email SET threadid='$threadid', subject='$subject', user='$author', email = '$email'");
}
 
 
if(!$presult) {
error('A database error occured during submission');
}
 
else {
header('Location: http://www.mysite.com/forums/view.php?threadid=' . $threadid . "&p=" . $p . "#" . $postid); 
 
$message = "$author has posted a reply to the topic '$subject' at http://www.mysite.com/forums/view.php?threadid=$threadid&p=$p#$postid.
 
To deactivate e-mail notification for this topic, visit
http://www.mysite.com/forums/disablen.php?id=$threadid.
 
$sql = mysql_query("SELECT * FROM f_email WHERE threadid='$threadid' AND user != '$author'");
 
$ms = "Mysite Forums: " . $subject;
 
while ($send = mysql_fetch_object($sql)) {
mail($send->email, $ms, $message, 'From: admin@mysite.com');
}
}
}
}
 
?>

Re: Email notifications won't jump to correct post

Posted: Tue Sep 22, 2009 12:30 pm
by jackpf
Hmm...that's strange. I can't really test it since I can't set up the same environment.

It looks fine.

But just try var_dump() 'ing $p before you send the header (comment out the header if you want to be able to see it...) then before you've defined the message, and then after you've defined $message.

Obviously something must cause it to lose its value. You just need to find out what.

Re: Email notifications won't jump to correct post

Posted: Tue Sep 22, 2009 11:29 pm
by anivad
Weird... looks like it doesn't lose its value after all.

I got this for the var_dump() before the header, before the message and after the message. $p = 2.

string(1) "2" string(1) "2" string(1) "2"

So whatever it is happens here:

$message = "$author has posted a reply to the topic '$subject' at http://www.mysite.com/forums/view.php?threadid=$threadid&p=$p#$postid. ... ";

Re: Email notifications won't jump to correct post

Posted: Wed Sep 23, 2009 5:46 am
by jackpf
Odd.

Try putting curly braces round $p.

Like

Code: Select all

$message = "....{$p}.....";
I wouldn't have thought this would help, but I really can't explain why it would be losing value. Worth a try I guess.

Re: Email notifications won't jump to correct post

Posted: Thu Sep 24, 2009 7:44 am
by anivad
ARGH. Okay, I solved it. People were adding replies on page 1 of the thread rather than the last, so that was the value that got assigned to $p.