Sticky comment forms for sNews

Sticky comment forms for sNews

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.

Tags

 

You might like

Comments


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


Thanks Matt
An other usefull mod.
PP


Aaaaaaand, using it ;) Goodie stuff, Matty


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


No problem, thanks Roie, and everyone! :)


Another mod added to my page, thanks again.


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=... for code],
I didn't find how to have this code in it the html echo.

If you have a minute... ;-)


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>';


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

It just works fine.
Thanks a lot Matt.


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


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>';


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


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


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'];
<span class="highlight">$cemail = $_SESSION[_SITE.'comment']['email'];</span>
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']);
<span class="highlight">$cemail = cleanXSS($bits['2']);</span>
$remember_me = "ok";
} else {
$url = $name = $comment = '';
}

lastly, change the email field to this;

echo html_input('text', 'email', 'email', <span class="highlight">$cemail</span>, l('cemail'), 'text', '', '', '', '', '', '', '', '', '');

Comments are closed. No new comments allowed.

Copyleft 2002 - 2017 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top