Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

SPAM protection - captcha/magento session data problem
 
tsifra
Member
 
Total Posts:  57
Joined:  2008-09-14
 

I’m trying to integrate http://www.phpcaptcha.org/

http://www.magentocommerce.com/boards/viewthread/18885/
http://www.magentocommerce.com/boards/viewthread/25296/

I started with contacts form
1. .../design/...form.phtml

..
                    <
label for="captcha_code">Captcha code<span class="required">*</span></label><br />
                    <
input name="captcha_code" id="captcha_code" title="Code" value="" class="required-entry input-text" type="text" />
                    <
img id="captcha_image" src="/lib/phpcaptcha/securimage_show.php"/>
<?php
    
if (!isset($_SESSION['andrey_counter'])) $_SESSION['andrey_counter']=0;
    
$_SESSION['andrey_counter']++;
    
$_SESSION['andrey_test_value'"this is test";

    
$fp fopen('/home/webmaster/www/dev/lib/phpcaptcha/01.txt''w');
    
fwrite($fp'sid: ' session_id("\n");
    
fwrite($fp'sname: ' session_name() . "\n");
    
fwrite($fp'refreshed: ' $_SESSION['andrey_counter'"\n");
    
fwrite($fp'test: ' $_SESSION['andrey_test_value'"\n");
    
fclose($fp);
?>
..

Data in file is OK.
1.txt

sidebbe5f40ed32d30f83a19f9480a2ec0c
sname
frontend
refreshed
19
test
this is test

2. /lib/phpcaptcha/securimage_show.php
By default session for this script differs from magento session. So i’m trying to share magento’s session.

<?php
    
include 'securimage.php';
    
ini_set('display_errors',1);
    
error_reporting(E_ALL);

    
session_start();
    
//print_r(getallheaders());

    
if (!isset($_SESSION['andrey_counter'])) $_SESSION['andrey_counter']="not set";
    if (!isset(
$_SESSION['andrey_test_value'])) $_SESSION['andrey_test_value']="not set";

    
$fp fopen('/home/webmaster/www/dev/lib/phpcaptcha/02.txt''w');
    
fwrite($fp'sid: ' session_id("\n");
    
fwrite($fp'sname: ' session_name() . "\n");
    
fwrite($fp'refreshed: ' $_SESSION['andrey_counter'"\n");
    
fwrite($fp'test: ' $_SESSION['andrey_test_value'"\n");
    
fclose($fp);
    
    
$img = new securimage();
    
$img->show();
?>
Data in file is not OK
$_SESSION[’andrey_counter’] and $_SESSION[’andrey_test_value’] is empty! I don’t know howto fix this.
Take a look at sname. PHPSESSID not equals frontend (see 1.txt)
2.txt
sidebbe5f40ed32d30f83a19f9480a2ec0c
sname
PHPSESSID
refreshed
not set
test
not set

3. app/code/../contacts/.. /IndexController.php

...
$captha_code $postObject->getCaptcha_code();
...
    if (!isset(
$_SESSION['andrey_counter'])) $_SESSION['andrey_counter']="not set";
    if (!isset(
$_SESSION['andrey_test_value'])) $_SESSION['andrey_test_value']="not set";

    
$fp fopen('/home/webmaster/www/dev/lib/phpcaptcha/03.txt''w');
    
fwrite($fp'sid: ' session_id("\n");
    
fwrite($fp'sname: ' session_name() . "\n");
    
fwrite($fp'refreshed: ' $_SESSION['andrey_counter'"\n");
    
fwrite($fp'test: ' $_SESSION['andrey_test_value'"\n");
    
fclose($fp);
...
Data in file is OK.

3.txt

sidebbe5f40ed32d30f83a19f9480a2ec0c
sname
frontend
refreshed
18
test
this is test

So my problem is the session! How can i share data between magento and simple php script?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tsifra
Member
 
Total Posts:  57
Joined:  2008-09-14
 

Parallel thread

http://www.magentocommerce.com/boards/viewthread/26480/

 
Magento Community Magento Community
Magento Community
Magento Community
 
tsifra
Member
 
Total Posts:  57
Joined:  2008-09-14
 

Dirty solution

1. create script custom_code.php in lib/phpcaptcha

<?php
  
function generateCode($len)
  
{
    $charset 
'ABCDEFGHKLMNPRSTUVWYZ23456789';
    
$code '';

    for(
$i 1$cslen strlen($charset); $i <= $len; ++$i{
      $code 
.= strtoupper$charset{rand(0$cslen 1));
    
}
    
return $code;
  
}
?>

2. Modify securimage.php

..
  public 
$strcode NULL;

  function 
generateCode($len)
  
{
    $code 
'';
    if (
$this->strcode == NULL{
      
for($i 1$cslen strlen($this->charset); $i <= $len; ++$i{
        $code 
.= strtoupper$this->charset{rand(0$cslen 1));
      
}
    } 
else {
      $code 
$this->strcode;
    
}
    
return $code;
  
}
..

3. modify securimage_show.php

<?php
    
include 'securimage.php';
    
    
$img = new securimage();
    if (isset(
$_GET['seccode'])) {
        $img
->strcode substr($_GET['seccode']64);
    
}

    $img
->show();
?>

4. For example insert code into app/design/frontend/default/default/template/contacts/form.phtml
Here we generate code and send it to securimage_show script. not good idea but at least works. You should encode code here and decode it in securimage_show.php

..
                <
div class="clear"></div>
                <
div class="input-box">
                    <
label for="captcha_code">Enter code<span class="required">*</span></label><br />
                    <
input name="captcha_code" id="captcha_code" title="Code" value="" class="required-entry input-text" type="text" />
                    <
img id="captcha_image" src="/lib/phpcaptcha/securimage_show.php?seccode=<?php require_once 'lib/phpcaptcha/custom_code.php'; $as_code = generateCode(4); $_SESSION['as_contacts_code'] = $as_code; echo 'BLABLA' . $as_code . BLA'; ?>"/>
                </
div>
..

5. Install Exanto_Contacts.xml http://www.magentocommerce.com/extension/534/exanto-contacts

Modify app/code/local/Exanto/Contacts/controllers/IndexController.php

...
    public function 
postAction()
    
{
        $post 
$this->getRequest()->getPost();
        if ( 
$post {
            $translate 
Mage::getSingleton('core/translate');
            
/* @var $translate Mage_Core_Model_Translate */
            
$translate->setTranslateInline(false);
            try 
{
                $postObject 
= new Varien_Object();
                
$postObject->setData($post);
//CAPTCHA
            
$captha_code $postObject->getCaptcha_code();
            
$as_code '';
            if (isset(
$_SESSION['as_contacts_code'])) {
                $as_code 
$_SESSION['as_contacts_code'];
            
}
            
            
if ((strtolower($captha_code) != strtolower($as_code))) {
                Mage
::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Введен Ð½ÐµÐ²ÐµÑ€Ð½Ñ‹Ð¹ ÐºÐ¾Ð´.'));
            
else {
//CAPTCHA
....
If someone needs all sources for contacts, register and lazymonk blog comment - PM me. I’ll publish it here
 
Magento Community Magento Community
Magento Community
Magento Community
 
Wallace Goh
Jr. Member
 
Total Posts:  11
Joined:  2008-11-05
 

Hai,
I have tested using your solution but i was having some error.My contact us pages have error.Please click here for the error.

Please advice.

Regards,
Wallace

peerus - 21 December 2008 09:06 AM

Dirty solution

1. create script custom_code.php in lib/phpcaptcha

<?php
  
function generateCode($len)
  
{
    $charset 
'ABCDEFGHKLMNPRSTUVWYZ23456789';
    
$code '';

    for(
$i 1$cslen strlen($charset); $i <= $len; ++$i{
      $code 
.= strtoupper$charset{rand(0$cslen 1));
    
}
    
return $code;
  
}
?>

2. Modify securimage.php

..
  public 
$strcode NULL;

  function 
generateCode($len)
  
{
    $code 
'';
    if (
$this->strcode == NULL{
      
for($i 1$cslen strlen($this->charset); $i <= $len; ++$i{
        $code 
.= strtoupper$this->charset{rand(0$cslen 1));
      
}
    } 
else {
      $code 
$this->strcode;
    
}
    
return $code;
  
}
..

3. modify securimage_show.php
<?php
    
include 'securimage.php';
    
    
$img = new securimage();
    if (isset(
$_GET['seccode'])) {
        $img
->strcode substr($_GET['seccode']64);
    
}

    $img
->show();
?>

4. For example insert code into app/design/frontend/default/default/template/contacts/form.phtml
Here we generate code and send it to securimage_show script. not good idea but at least works. You should encode code here and decode it in securimage_show.php
..
                <
div class="clear"></div>
                <
div class="input-box">
                    <
label for="captcha_code">Enter code<span class="required">*</span></label><br />
                    <
input name="captcha_code" id="captcha_code" title="Code" value="" class="required-entry input-text" type="text" />
                    <
img id="captcha_image" src="/lib/phpcaptcha/securimage_show.php?seccode=<?php require_once 'lib/phpcaptcha/custom_code.php'; $as_code = generateCode(4); $_SESSION['as_contacts_code'] = $as_code; echo 'BLABLA' . $as_code . BLA'; ?>"/>
                </
div>
..

5. Install Exanto_Contacts.xml http://www.magentocommerce.com/extension/534/exanto-contacts

Modify app/code/local/Exanto/Contacts/controllers/IndexController.php

...
    public function 
postAction()
    
{
        $post 
$this->getRequest()->getPost();
        if ( 
$post {
            $translate 
Mage::getSingleton('core/translate');
            
/* @var $translate Mage_Core_Model_Translate */
            
$translate->setTranslateInline(false);
            try 
{
                $postObject 
= new Varien_Object();
                
$postObject->setData($post);
//CAPTCHA
            
$captha_code $postObject->getCaptcha_code();
            
$as_code '';
            if (isset(
$_SESSION['as_contacts_code'])) {
                $as_code 
$_SESSION['as_contacts_code'];
            
}
            
            
if ((strtolower($captha_code) != strtolower($as_code))) {
                Mage
::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Введен Ð½ÐµÐ²ÐµÑ€Ð½Ñ‹Ð¹ ÐºÐ¾Ð´.'));
            
else {
//CAPTCHA
....
If someone needs all sources for contacts, register and lazymonk blog comment - PM me. I’ll publish it here
 
Magento Community Magento Community
Magento Community
Magento Community
 
tSpid3r
Jr. Member
 
Total Posts:  17
Joined:  2008-11-07
 
tsifra - 20 December 2008 02:12 AM

I’m trying to integrate http://www.phpcaptcha.org/

http://www.magentocommerce.com/boards/viewthread/18885/
http://www.magentocommerce.com/boards/viewthread/25296/

I started with contacts form
1. .../design/...form.phtml
...

2. /lib/phpcaptcha/securimage_show.php
By default session for this script differs from magento session. So i’m trying to share magento’s session.
...
Data in file is not OK
$_SESSION[’andrey_counter’] and $_SESSION[’andrey_test_value’] is empty! I don’t know howto fix this.
Take a look at sname. PHPSESSID not equals frontend (see 1.txt)
...
So my problem is the session! How can i share data between magento and simple php script?

Did you ever find a sollution to this?

I’m also trying to implement a Captcha solution for Magento. I generate a php based immage and then use it next to the submit button by:

<img id="captcha_image" src="<?php echo $this->getSkinUrl('captcha/captcha.phtml'); ?>"

Everything is ok so far. The image is displayed and the code is changed every time the page is displayed.

In the phtml file I want to store the key by:

session_start();
$_SESSION["captcha"$captcha_code;

Here the session that is oppened has session_name(): PHPSESSID and a different session_id() which means that a new session is opened.

If I run session_name() in “form.phtml” (file for contact us page), i get session_name: frontend.

I tryied with no success to force magento to open the same session in the captcha.phtml file:

session_name('frontend');
session_start('frontend');
$_SESSION["captcha"$captcha_code;

Does anyone know why I can’t successfuly register my variable ($captcha_code) in the current active session opened by magento?

It is also worth mentioning that, when called by: 

<?php include('captcha.phtml'?>

all variables registered through the $_SESSION["variable"] = $value; method work perfectly fine.

 
Magento Community Magento Community
Magento Community
Magento Community
 
pezomatic
Sr. Member
 
Avatar
Total Posts:  190
Joined:  2008-11-05
 

Anyone got this working yet in 1.4?

I am looking for a way to integrate the official code from captcha.net for all of our site\’s Magento form submissions.

http://www.captcha.net/

I would like it to look look just like the captcha screen that magento is using on this thread.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Magento Exp
Member
 
Total Posts:  37
Joined:  2010-08-03
 
tsifra - 21 December 2008 09:06 AM

Dirty solution

1. create script custom_code.php in lib/phpcaptcha

<?php
  
function generateCode($len)
  
{
    $charset 
'ABCDEFGHKLMNPRSTUVWYZ23456789';
    
$code '';

    for(
$i 1$cslen strlen($charset); $i <= $len; ++$i{
      $code 
.= strtoupper$charset{rand(0$cslen 1));
    
}
    
return $code;
  
}
?>

2. Modify securimage.php

..
  public 
$strcode NULL;

  function 
generateCode($len)
  
{
    $code 
'';
    if (
$this->strcode == NULL{
      
for($i 1$cslen strlen($this->charset); $i <= $len; ++$i{
        $code 
.= strtoupper$this->charset{rand(0$cslen 1));
      
}
    } 
else {
      $code 
$this->strcode;
    
}
    
return $code;
  
}
..

3. modify securimage_show.php
<?php
    
include 'securimage.php';
    
    
$img = new securimage();
    if (isset(
$_GET['seccode'])) {
        $img
->strcode substr($_GET['seccode']64);
    
}

    $img
->show();
?>

4. For example insert code into app/design/frontend/default/default/template/contacts/form.phtml
Here we generate code and send it to securimage_show script. not good idea but at least works. You should encode code here and decode it in securimage_show.php
..
                <
div class="clear"></div>
                <
div class="input-box">
                    <
label for="captcha_code">Enter code<span class="required">*</span></label><br />
                    <
input name="captcha_code" id="captcha_code" title="Code" value="" class="required-entry input-text" type="text" />
                    <
img id="captcha_image" src="/lib/phpcaptcha/securimage_show.php?seccode=<?php require_once 'lib/phpcaptcha/custom_code.php'; $as_code = generateCode(4); $_SESSION['as_contacts_code'] = $as_code; echo 'BLABLA' . $as_code . BLA'; ?>"/>
                </
div>
..

5. Install Exanto_Contacts.xml http://www.magentocommerce.com/extension/534/exanto-contacts

Modify app/code/local/Exanto/Contacts/controllers/IndexController.php

...
    public function 
postAction()
    
{
        $post 
$this->getRequest()->getPost();
        if ( 
$post {
            $translate 
Mage::getSingleton('core/translate');
            
/* @var $translate Mage_Core_Model_Translate */
            
$translate->setTranslateInline(false);
            try 
{
                $postObject 
= new Varien_Object();
                
$postObject->setData($post);
//CAPTCHA
            
$captha_code $postObject->getCaptcha_code();
            
$as_code '';
            if (isset(
$_SESSION['as_contacts_code'])) {
                $as_code 
$_SESSION['as_contacts_code'];
            
}
            
            
if ((strtolower($captha_code) != strtolower($as_code))) {
                Mage
::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Введен Ð½ÐµÐ²ÐµÑ€Ð½Ñ‹Ð¹ ÐºÐ¾Ð´.'));
            
else {
//CAPTCHA
....
If someone needs all sources for contacts, register and lazymonk blog comment - PM me. I’ll publish it here

Can you please explain me how to implement this captcha in contact, register form.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Miller1975
Jr. Member
 
Total Posts:  2
Joined:  2012-09-18
 

Hi there,

i found for this a perfect and free extension called SPAM KILLER

Take a look at:

Works on all magento versions!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top