Sticky comment forms for sNews

Comments (15)

One thing that's been nagging me since the first time I used sNews was the lack of sticky forms for your readers. Sticky forms are basically a way to remember a user's name and url when commenting. Constantly typing in their name and url can get to be a pain in the rear for your regulars.

sticky form I prefer to let my readers choose whether or not to remember their info. We do this via a form option, typically a checkbox with a label such as "Remember me".

So today I whipped up this little mod to allow your sNews commenters such an option. When the option is checked and a comment submitted, this will take the name and url and encode it (to avoid storing the emails in plain text) and set it as cookie, then when the user is presented with a comment form, the cookie will be decoded and the name & url field will be automatically populated for them.

This is an easy mod to implement, I'm only showing how to do the name & url for this, as that is the standard for a stock sNews install, if you have added other fields, such as an email field with my gravatar mod, and you're unable to figure out how to make the additional field(s) sticky, let me know and I'll try to assist you.

OK, let's get started, as always BACK-UP your snews.php file and work off of a copy. Now at the very beginning of the file, just after the opening <?php tag and license block, insert the code as shown below;

<?php
/********************************************************

    sNews 1.7 - November 2008
    Update 1.3
    Copyright (C) Solucija.com
    sNews is licensed under a Creative Commons License

*********************************************************/
// user has posted a comment and wants us to save his info
if (isset($_POST['text']) && isset($_POST['comment'])) { // comment has been posted
    if($_POST['remember_me'] == 'yes') { // yes, user wants to save his/her info
        $name = cleanXSS($_POST['name']);
        $url = cleanXSS($_POST['url']);
        $cookie = base64_encode($name.'|'.$url);
        $expire = time()+15552000; // 6 months
        $domain = $_SERVER['HTTP_HOST'];
        setcookie('cinfo',$cookie,$expire,'/',$domain,false);
    } else { // user doesn't want info saved, let's delete any previous cookie as well
        $cookie = '';
        $expire = time()-99999999;
        $domain = $_SERVER['HTTP_HOST'];
        setcookie('cinfo',$cookie,$expire,'/',$domain,false);
    }
}

That will handle setting the cookie before anything else. Now within the function comment, find the following block of code and insert the highlighted part;

// recall and set vars for reuse when botched post
if($_SESSION[_SITE.'comment']['fail'] == true) {
    $name = $_SESSION[_SITE.'comment']['name'];
    $comment = $_SESSION[_SITE.'comment']['comment'];
    $url = $_SESSION[_SITE.'comment']['url'];
    unset($_SESSION[_SITE.'comment']);
} elseif (isset($_COOKIE['cinfo'])){
    $comment_info = base64_decode($_COOKIE['cinfo']);
    $bits = explode('|',$comment_info);
    $name = cleanXSS($bits['0']);
    $url = cleanXSS($bits['1']);
    $remember_me = "ok";
} else {
	$url = $name = $comment = '';
}

That will handle populating the name & url fields with the cookie info, now just below that, add the following checkbox code;

echo html_input('text', 'url', 'url', $url, l('url'), 'text', '', '', '', '', '', '', '', '', '')."\r\n";
echo html_input('checkbox', 'remember_me', 'remember_me', 'YES', l('comment_remember'), '', 'value="yes"', '', '', $remember_me, '', '', '', '', '')."\r\n";
echo html_input('textarea', 'text', 'text', stripslashes($comment), '* '.l('comment'), '', '', '', '', '', '5', '5', '', '', '')."\r\n";
echo mathCaptcha()."\r\n";

Now open your language file and add the following under the #comments section;

$l['comment_remember'] = 'Remember me';

That's it, upload your modified snews.php and language file and you should be all set. If you want to demo it, just look below.

bookmark / share this: Bookmark and Share
rated 3.8/5 (5 votes)


15 comments

Add a new comment »

Patric Patric said:
Mar 5th, 2009 at 10:09 am

Damn, Matt... cool as... well, a cool beer. Gonna use it.


Poppoll Poppoll said:
Mar 5th, 2009 at 6:36 pm

Thanks Matt
An other usefull mod.
PP


Patric Patric said:
Mar 7th, 2009 at 9:18 am

Aaaaaaand, using it ;) Goodie stuff, Matty


Roie Roie said:
Mar 19th, 2009 at 3:24 pm

Excellent idea and the best snews mod site, thanks a lot


Matt Matt said:
Mar 23rd, 2009 at 7:20 am

No problem, thanks Roie, and everyone! :)


asundrus asundrus said:
Apr 28th, 2009 at 9:34 am

Another mod added to my page, thanks again.


Sven - Philippe Sven - Philippe said:
Nov 4th, 2009 at 6:28 am

Hi Matt, I've been scratching my head to adapt this Mod to the comments form function in sNews.
Since I've changed it to shorten it (see http://snewscms.com/forum/index.php?topic=8391.msg61890#msg61890 for code],
I didn't find how to have this code in it the html echo.

If you have a minute... ;-)


Matt Matt said:
Nov 4th, 2009 at 7:48 am

hmmmmmm, not tested, but I think this is what you're looking for Sven.

echo '
<div class="commentsbox"><h2>', l('addcomment') ,'</h2>
<p>', l('required') ,'</p>
<form method="post" action="', _SITE ,'" id="post" accept-charset="UTF-8">
<p><label for="name">* ', l('name') ,'</label>:<br />
<input type="text" name="name" id="name" class="text" value="', $name ,'" /></p>
<p><label for="url">', l('url') ,'</label>:<br />
<input type="text" name="url" id="url" class="text" value="',$url,'" /></p>
<p><input name="remember_me" id="remember_me" value="',$remember_me,'" checked="checked" type="checkbox"> <label for="remember_me">', l('comment_remember') ,'</label></p>
<p><label for="text">* ', l('comment') ,'</label>:<br />
<textarea name="text" rows="5" cols="5" id="text"></textarea></p>
',  mathCaptcha() , '
 <p>
<input type="hidden" name="category" id="category" value="', $categorySEF ,'" />
<input type="hidden" name="id" id="id" value="', $_ID ,'" />
<input type="hidden" name="article" id="article" value="', $art_value ,'" />
<input type="hidden" name="commentspage" id="commentspage" value="', $back_to_page ,'" />
<input type="hidden" name="ip" id="ip" value="', $_SERVER['REMOTE_ADDR'] ,'" />
<input type="hidden" name="time" id="time" value="', time() ,'" />
<input type="submit" name="comment" id="comment" class="button" value="', l('submit') ,'" />
</p>
</form>
</div>';


Sven - Philippe Sven - Philippe said:
Nov 4th, 2009 at 9:20 am

You think and you're right pal!
I think and I'm allways wrong.-D

It just works fine.
Thanks a lot Matt.


Sven - Philippe Sven - Philippe said:
Nov 5th, 2009 at 10:38 am

It works fine... it works fine... locally.
On line it fails.
Can you post some comments to try:
http://on-air.hiseo.fr/fatras/svg-et-html5/
Weirdo. -)


Matt Matt said:
Nov 5th, 2009 at 10:51 am

Sven, did you add the code to set the cookie? I posted a test comment and it didn't set the cookie.


Matt Matt said:
Nov 5th, 2009 at 10:58 am

Ahhh... I think I see the problem, empty value in the remember_me box, try this block instead...

if ($remember_me == 'ok') {$checked='checked="checked"';}
echo '
<div class="commentsbox"><h2>', l('addcomment') ,'</h2>
<p>', l('required') ,'</p>
<form method="post" action="', _SITE ,'" id="post" accept-charset="UTF-8">
<p><label for="name">* ', l('name') ,'</label>:<br />
<input type="text" name="name" id="name" class="text" value="', $name ,'" /></p>
<p><label for="url">', l('url') ,'</label>:<br />
<input type="text" name="url" id="url" class="text" value="',$url,'" /></p>
<p><input name="remember_me" id="remember_me" value="yes" ',$checked,' type="checkbox"> <label for="remember_me">', l('comment_remember') ,'</label></p>
<p><label for="text">* ', l('comment') ,'</label>:<br />
<textarea name="text" rows="5" cols="5" id="text"></textarea></p>
',  mathCaptcha() , '
 <p>
<input type="hidden" name="category" id="category" value="', $categorySEF ,'" />
<input type="hidden" name="id" id="id" value="', $_ID ,'" />
<input type="hidden" name="article" id="article" value="', $art_value ,'" />
<input type="hidden" name="commentspage" id="commentspage" value="', $back_to_page ,'" />
<input type="hidden" name="ip" id="ip" value="', $_SERVER['REMOTE_ADDR'] ,'" />
<input type="hidden" name="time" id="time" value="', time() ,'" />
<input type="submit" name="comment" id="comment" class="button" value="', l('submit') ,'" />
</p>
</form>
</div>';


Sven - Philippe Sven - Philippe said:
Nov 5th, 2009 at 11:42 am

Yeap! That was it.
Thanks again Mat.
And 5 stars for this Mod.
Hip hip hip...


Kreatur Kreatur said:
Nov 12th, 2009 at 7:28 am

So, how about remembering the e-mail? Can you please give the the modifications?


Matt Matt said:
Nov 12th, 2009 at 7:40 am

Hi Kreatur, that's easy, in the first step, find this;

$cookie = base64_encode($name.'|'.$url);

and change it to this;

$cemail = cleanXSS($_POST['email']);
$cookie = base64_encode($name.'|'.$url.'|'.$cemail);

then add the highlighted email lines in the second part;

        if($_SESSION[_SITE.'comment']['fail'] == true) {
            $name = $_SESSION[_SITE.'comment']['name'];
            $comment = $_SESSION[_SITE.'comment']['comment'];
            $url = $_SESSION[_SITE.'comment']['url'];
            $cemail = $_SESSION[_SITE.'comment']['email'];
            unset($_SESSION[_SITE.'comment']);
        } elseif (isset($_COOKIE['cinfo'])){
            $comment_info = base64_decode($_COOKIE['cinfo']);
            $bits = explode('|',$comment_info);
            $name = cleanXSS($bits['0']);
            $url = cleanXSS($bits['1']);
            $cemail = cleanXSS($bits['2']);
            $remember_me = "ok";
        } else {
            $url = $name = $comment = '';
        }

lastly, change the email field to this;

         echo html_input('text', 'email', 'email', $cemail, l('cemail'), 'text', '', '', '', '', '', '', '', '', '');



Write a comment

* = required field

:

:

:

:

You may insert urls in plain text, urls will be automatically linkified for trusted users and on seasoned posts only. All first comments are moderated, so use your email if you want to be remembered.


Back to top