Call-back icon  Enterprise Sales: +1.310.775.2674 (N. America)   +44 20.3286.4137 (UK)

Magento

eCommerce Software for Online Growth

Magento Forum

   
SPAM protection - captcha/magento session data problem
 
peerus
Member
 
Total Posts:  45
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?

 Signature 

Magento fan concerned by
Multi Images import http://www.magentocommerce.com/boards/viewthread/6220/
Customer group pricing http://www.magentocommerce.com/boards/viewthread/108/

 
Magento Community Magento Community
Magento Community
Magento Community
 
peerus
Member
 
Total Posts:  45
Joined:  2008-09-14
 

Parallel thread

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

 Signature 

Magento fan concerned by
Multi Images import http://www.magentocommerce.com/boards/viewthread/6220/
Customer group pricing http://www.magentocommerce.com/boards/viewthread/108/

 
Magento Community Magento Community
Magento Community
Magento Community
 
peerus
Member
 
Total Posts:  45
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

 Signature 

Magento fan concerned by
Multi Images import http://www.magentocommerce.com/boards/viewthread/6220/
Customer group pricing http://www.magentocommerce.com/boards/viewthread/108/

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2009 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
108779 users|300 users currently online|199886 forum posts