cancel
Showing results for 
Search instead for 
Did you mean: 

Problems using CaptchaSecurityImages since PN PHP upgrade

netman
Grafter
Posts: 27
Registered: ‎10-08-2007

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Hi alanru
I got this on your second comment...
Server error!
The server encountered an internal error and was unable to complete your request.
Error message:
Premature end of script headers: session2.php
If you think this is a server error, please contact the webmaster.
Error 500
netman
Grafter
Posts: 27
Registered: ‎10-08-2007

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Hi to both alanru and Gabe,
Here are my 3 instances of code
1. The call from the html file to the php security file
2. The php security file
3. the php Post and redirect file
I cannot see what's wrong  Crazy
1.
******************************************************************************************************************************************************************************
<img src="http://ccgi.netman.plus.com/cgi-bin/CaptchaSecurityImages.php" /><br />
<label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" value=""/><br />
                <p><br>
                  <input type="submit" name="submit" id="submit" class="button" value="Send message" tabindex="6" />
******************************************************************************************************************************************************************************
2.
******************************************************************************************************************************************************************************
<?php
//session_id("sec"); 
//session_start();
session_id("sec"); 
ini_set('session.save_path', '/files/home3/netman/cgi-bin/SESSIONS');


class CaptchaSecurityImages {
var $font = 'arial.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);
?>
******************************************************************************************************************************************************************************
3.
*****************************************************************************************************************************************************************************
<?php
//session_save_path('/files/home3/netman/cgi-bin/SESSIONS/');
//session_id("sec"); 
session_id("sec"); 
ini_set('session.save_path', '/files/home3/netman/cgi-bin/SESSIONS');
session_start();
`chmod 700 $fn`;
if( isset($_POST['submit'])) {
  // get_security_picture($_SESSION['random']) 
  if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {


//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'You have a message from Netmans Website';
// Your email address. This is where the form information will be sent.
$emailadd = 'web@netman.plus.com';
$emailaddfrom = 'Netman web feedback form';
// Where to redirect after form is processed.
$url = 'http://www.netman.plus.com/thankyou.html';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "Message from Netman Web email/feedback form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 100)
{echo "Name of form element $key cannot be longer than 100 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailaddfrom.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';


unset($_SESSION['code']);
  } else {
// Insert your code for showing an error message here
//echo "Sorry, you have provided an invalid security code";

  header('Location:http://www.netman.plus.com/whoops.html');
  }
} else {
}
?>
*******************************************************************************************************************************************
Thanks for all your help guys
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Hi Alan,
Try
<?php
ini_set('session.save_path', '/files/homeX/username/SESSION');
session_id("test");
session_start();
?>

suitably edited for your path. Call it in a few browsers. Then look in /SESSION and count the sessions.
Gabe
alanrm
Newbie
Posts: 4
Registered: ‎05-04-2010

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Do we have a happy ending?
Both my original scripts without any alterations are now working again - Plusnet have been busy.
Netman - have you tried your original scripts again?
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Hi Netman,
You appear to have taken the session_start(); out of your image generator.
try just

ini_set('session.save_path', '/files/home3/netman/cgi-bin/SESSIONS');
session_start();

Gabe
alanrm
Newbie
Posts: 4
Registered: ‎05-04-2010

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Apologies - please ignore call to confirm happy ending - seems I cleared the wrong browser cache!
But the main point still applies.
Call session_start(); on your local server & you will note a session id cookie set up with a lengthy
ref. The call fails on Plusnet's implementation.
The workaround is to specifically set a short session id to avoid PHP generating a long reference
which Plusnet cannot handle.
Any script calling session_start() on Plusnet is bound to fail - the captcha image routines which
most content management systems employ make use of this function & will fail to show any image.
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Hi Netman,
And the next problem is with the font. Try:
<?php
ini_set('session.save_path', '/files/home3/netman/cgi-bin/SESSIONS');
session_start();

class CaptchaSecurityImages {
  var $font = '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.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.6;
      $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);
?>

That should deliver a captcha.
Gabe
netman
Grafter
Posts: 27
Registered: ‎10-08-2007

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Hi Abie
Cc alanru
Grin  Smiley  Wink  Cool  Roll_eyes  Tongue Kiss (well maybe not the last one)
So the font line var $font = '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf';

was the cause of the problem
Thank you all very much for your time and paitence
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Problems using CaptchaSecurityImages since PN PHP upgrade

Yup, the problem with CaptchaSecurityImages was it couldn't find arial. If Plusnet don't pick up this thread you might want to drop them a line to request a few basic .ttf fonts for use with GD.
The problem with the security.lib.php script was that it didn't set the session variable so needed to be edited or run through a wrapper, like in #5.
The problem with session_id("test"); is that all your visitors end up in the same session. There's a reason for the long hash of numbers in a session id. So, if you haven't already, do remove that line.  Wink Roll_eyes
Good luck.
Gabe