Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Post PHP Script Works on One Domain but Not the Other

    I have a PHP script that prints a random number for use as a captcha code. However, for the life of me, I can't figure out why it works on one domain but not the other, even though the servers have the same exact specs.

    Displays a captcha code: http://www.unitedfleetparts.com/php-/captcha_code.php

    Displays NOTHING, even though it's the same exact script: http://www.uatparts.com/php-/captcha_code.php

    Please help me figure out how I can get this PHP script to work on my second domain. Any help is appreciated.

    PHP Script:

    Code:
    <?php
    session_start();
    $image_width = 120;
    $image_height = 40;
    $characters_on_image = 6;
    $font = './monofont.ttf';
    
    $possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
    $random_dots = 0;
    $random_lines = 20;
    $captcha_text_color="0x142864";
    $captcha_noice_color = "0x142864";
    
    $code = '';
    
    $i = 0;
    while ($i < $characters_on_image) { 
    $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
    $i++;
    }
    
    $font_size = $image_height * 0.75;
    $image = @imagecreate($image_width, $image_height);
    
    $background_color = imagecolorallocate($image, 255, 255, 255);
    
    $arr_text_color = hexrgb($captcha_text_color);
    $text_color = imagecolorallocate($image, $arr_text_color['red'], 
        $arr_text_color['green'], $arr_text_color['blue']);
    
    $arr_noice_color = hexrgb($captcha_noice_color);
    $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
            $arr_noice_color['green'], $arr_noice_color['blue']);
    
    for( $i=0; $i<$random_dots; $i++ ) {
    imagefilledellipse($image, mt_rand(0,$image_width),
     mt_rand(0,$image_height), 2, 3, $image_noise_color);
    }
    
    for( $i=0; $i<$random_lines; $i++ ) {
    imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
     mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
    }
    
    $textbox = imagettfbbox($font_size, 0, $font, $code); 
    $x = ($image_width - $textbox[4])/2;
    $y = ($image_height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
    
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['6_letters_code'] = $code;
    
    function hexrgb ($hexstr)
    {
      $int = hexdec($hexstr);
    
      return array("red" => 0xFF & ($int >> 0x10),
                   "green" => 0xFF & ($int >> 0x8),
                   "blue" => 0xFF & $int);
    }
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Does the failing site have the monofont.ttf file uploaded?

  • #3
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Yes, everything is there in both domains. The font is at http://www.uatparts.com/php-/monofont.ttf

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    Your code is experiencing a fatal php runtime error, after the session_start() statement, but before the header() statement.

    To debug what it is doing, add the following two lines of code immediately after the <?php tag -
    Code:
    ini_set("display_errors", "1");
    error_reporting(-1);
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Post

    CFMaBiSmAd, thanks for your help. I went ahead and added the two lines of code to the script and reloaded the page, but no error messages were given:

    Code:
    <?php
    ini_set("display_errors", "1");
    error_reporting(-1);
    
    session_start();
    $image_width = 120;
    $image_height = 40;
    $characters_on_image = 6;
    $font = './monofont.ttf';
    
    $possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
    $random_dots = 0;
    $random_lines = 20;
    $captcha_text_color="0x142864";
    $captcha_noice_color = "0x142864";
    $code = '';
    
    $i = 0;
    while ($i < $characters_on_image) { 
    $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
    $i++;
    }
    
    $font_size = $image_height * 0.75;
    $image = @imagecreate($image_width, $image_height);
    
    $background_color = imagecolorallocate($image, 255, 255, 255);
    
    $arr_text_color = hexrgb($captcha_text_color);
    $text_color = imagecolorallocate($image, $arr_text_color['red'], 
    		$arr_text_color['green'], $arr_text_color['blue']);
    
    $arr_noice_color = hexrgb($captcha_noice_color);
    $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
    		$arr_noice_color['green'], $arr_noice_color['blue']);
    
    for( $i=0; $i<$random_dots; $i++ ) {
    imagefilledellipse($image, mt_rand(0,$image_width),
     mt_rand(0,$image_height), 2, 3, $image_noise_color);
    }
    
    for( $i=0; $i<$random_lines; $i++ ) {
    imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
     mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
    }
    
    $textbox = imagettfbbox($font_size, 0, $font, $code); 
    $x = ($image_width - $textbox[4])/2;
    $y = ($image_height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
    
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['6_letters_code'] = $code;
    
    function hexrgb ($hexstr)
    {
      $int = hexdec($hexstr);
    
      return array("red" => 0xFF & ($int >> 0x10),
                   "green" => 0xFF & ($int >> 0x8),
                   "blue" => 0xFF & $int);
    }
    ?>

  • #6
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,437
    Thanks
    11
    Thanked 304 Times in 303 Posts
    You might remove the "@" from this, just in case its generating an error that you're not catching.
    Code:
    $image = @imagecreate($image_width, $image_height);

  • Users who have thanked tracknut for this post:

    JAG (01-13-2014)

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    You need to use the script inside an img tag as the src for it to work properly, otherwise you are at the mercy of the browser and the server as to how it appears, or if it appears at all.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #8
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Post

    DrDOS, I made a page that should display it using an image tag, but all it shows is a broken image link instead of the captcha code:

    http://www.uatparts.com/php-/1.php


    tracknut, if I take out the @ it gives me the following error:

    Fatal error: Call to undefined function imagecreate() in /home/uatparts/public_html/php-/captcha_code.php on line 25

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    The GD library is not installed/enabled on your server.

    And, please don't put @ error suppressors in ANY code, ever.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    JAG (01-13-2014)

  • #10
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Post

    CFMaBiSmAd, thanks for the good lead. We don't have access to edit the PHP ini files so is there a way we can enable the GD library through the .htaccess file or another way?

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Contact your host to get GD installed.

    For future reference, the @ symbol surpresses error messages and should never be used or if it is, it should be used sparingly.

    I've seen scripts for some hi-end php scripts use it all over the place - amateurs!
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    JAG (01-13-2014)

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Save the little script below as "test.php" and then upload it to both websites.

    Run it and compare the PHP settings between the two sites:

    PHP Code:
    <?php
    phpinfo
    ();
    ?>

  • Users who have thanked mlseim for this post:

    JAG (01-13-2014)

  • #13
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks to all of you for helping me out. I went ahead and took mlseim's advice of running <?php phpinfo(); ?> on both servers and found out that indeed the GD library had been disabled on the uatparts.com server, which is why the imagecreate() wasn't creating the captcha image. To make it worst, it was written as "@imagecreate()" in the script which was suppressing the error message. I learned a lot about debugging in PHP from this thread. I now know that I have to contact my host and ask him to enable the GD library on uatparts.com. I thank all of you!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •