We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45732
    • 1 Posts
    Hello,

    I'm already searching for a solution a couple of hours.

    Maybe you can/will help me.

    I'm created my site in modx but I have an external php(captcha) file where I'm using session variables.

    this is my external captcha php file:
    <?php
    //require_once ($_SERVER["DOCUMENT_ROOT"] . '/manager/includes/config.inc.php');
    //startCMSSession();
    session_start();

    class CaptchaSecurityImages {

    var $font = 'monofont.ttf';

    function generateCode($characters) {
    /* list all possible characters, similar looking characters and vowels have been removed */
    $possible = '23456789bcdfghjkmnpqrstvwxyz';
    $code = '';
    $i = 0;
    while ($i < $characters) {
    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
    $i++;
    }
    return $code;
    }

    function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
    $code = $this->generateCode($characters);
    /* font size will be 75% of the image height */
    $font_size = $height * 0.75;
    $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
    /* set the colours */
    $background_color = imagecolorallocate($image, 255, 255, 255);
    $text_color = imagecolorallocate($image, 20, 40, 100);
    $noise_color = imagecolorallocate($image, 100, 120, 180);
    /* generate random dots in background */
    for( $i=0; $i<($width*$height)/3; $i++ ) {
    imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }
    /* generate random lines in background */
    for( $i=0; $i<($width*$height)/150; $i++ ) {
    imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
    }
    /* create textbox and add text */
    $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
    $x = ($width - $textbox[4])/2;
    $y = ($height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
    /* output captcha image to browser */
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['security_code'] = $code;
    }

    }
    $width = isset($_GET['width']) ? $_GET['width'] : '120';
    $height = isset($_GET['height']) ? $_GET['height'] : '40';
    $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

    $captcha = new CaptchaSecurityImages($width,$height,$characters);

    ?>








    And this is my snippet that I'm using: the 'CaptchaSecurityImages.php' returns an image with the code


    session_start();
    $output ="<form id='loginForm' name='loginForm' method='POST' action='[~[*id*]~]'>
    <table border='0' align='center' cellpadding='2' cellspacing='1'>

    <tr>
    <td></td>
    <td><input type='radio' name='Keuze' value='Algemene info' checked />Ik wens algemene info</td>
    </tr>
    <tr>
    <td></td>
    <td><input type='radio' name='Keuze' value='Andere vraag' />Andere vraag</td>
    </tr>

    <tr>
    <td><b>Naam</b></td>
    <td><input name='Naam' type='text' class='textfield' maxlength='60' id='Naam' size='52' value='". $_POST['Naam'] ."' /><font color='red'>*</font></td>
    </tr>

    <tr>
    <td><b>Telefoon</b></td>
    <td><input type='text' name='Telefoon' maxlength='60' size='52' value='". $_POST['Telefoon'] ."' /></td>
    </tr>

    <tr>
    <td><b>E-mail</b></td>
    <td><input type='text' name='Email' maxlength='60' size='52' value='". $_POST['Email'] ."' /><font color='red'>*</font></td>
    </tr>

    <tr>
    <td><b>Opmerkingen</b></td>
    <td><textarea name='opmerkingen' cols='45' rows='6'>". $_POST['opmerkingen'] ."</textarea><font color='red'>*</font></td>
    </tr>
    <tr>
    <td><img src='CaptchaSecurityImages.php' alt='' /> <b>Security Code:</b> </td><td><input id='security_code' name='security_code' type='text' /></td>
    </tr>
    <tr>
    <td> </td>
    <td><input type='submit' name='submit' value='Verzenden' /></td>
    </tr>
    </table>";


    I have the issue that when I do a
    print "Code: " . $_SESSION['security_code'];
    it's not filled up in the external script.

    Do you have any idea what I'm doing wrong? I'm a newbie at Modx...

    As you also can see I already tried this startCMSSession() :
    //require_once ($_SERVER["DOCUMENT_ROOT"] . '/manager/includes/config.inc.php');
    //startCMSSession();
    But without succes.

    Thanks you already,
    Filip