Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
I am trying to implement some anti-spam code on our side. My plan is to implement what is described in this post and on top of that, to determine the top level domain and block some top level domains completely, like for instance .ru (all spam messages I receive are exclusively sent with .ru email addresses). Our site is aimed at a very local audience, anyway, so we can safely do this.

So what I would like to do is in k_success handling is this:
<cms:php>
find top level domain in the email address
</cms:php>
<cms:if topleveldomain <> 'ru' >
... email contact form notification
... success response to form user
<cms:else />
... success response to form user
</cms:if>

How can I use the variable $topleveldomain in the couch script?
"I have never tried that before, so I think I should definitely be able to do that" - Pippi Longstocking
As explained in viewtopic.php?f=2&t=9882#p22669, you should find it easier to place all the PHP code in a PHP function (as opposed to putting it in Couch <cms:php> tag) -
Code: Select all
<?php
    // move all the relevant code to within a function ..
    function get_topleveldomain( $param ){
        blah..
        blahh $param..
        ..
        ..
        bla..
       
        return $result;
    }
?>

If the function is used in multiple templates, place it in a common PHP file like addons/kfunctions.php.

OK, with the function in place, you can call it through Couch as follows -
Code: Select all
<cms:php>
    // invoke the function from within Couch ..
    $result = get_topleveldomain( <cms:show url /> );
   
    // set result in global context for use by Couch tags
    global $CTX;
    $CTX->set( 'topleveldomain', $result, 'global' );
</cms:php>

And now you have the variable named 'topleveldomain' available for use
Code: Select all
<cms:if topleveldomain <> 'ru' >

As a variation of the technique above, you may make the PHP function itself set the result in context e.g.
Code: Select all
<?php
    // move all the relevant code to within a function ..
    function get_topleveldomain( $param ){
        blah..
        blahh $param..
        ..
        ..
        bla..
       
        global $CTX;
        $CTX->set( 'topleveldomain', $result, 'global' );
    }
?>

Which would simplify the code used with Couch to just this -
Code: Select all
<cms:php>
    get_topleveldomain( <cms:show url /> );
</cms:php>
Code: Select all
<cms:if topleveldomain <> 'ru' >
    ...
</cms:if>

If you happen to need more info on how Couch code interacts with raw PHP, you may find the following post useful -
viewtopic.php?f=4&t=10627#p26647

Hope this helps.
Excellent KK, once again thanks for your quick answer ! This helps indeed.
"I have never tried that before, so I think I should definitely be able to do that" - Pippi Longstocking
3 posts Page 1 of 1