This script prints some non readable characters insted of image

Discussion in 'PHP' started by Sangeetha.M, Jan 1, 2009.

  1. #1
    Hi all,

     session_start();
    
    $width  = 120;
    $height =  40;
    $length =   5;
    
    $baseList = '0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
    $code    = "";
    $counter = 0;
    
    $image = @imagecreate($width, $height) or die('Cannot initialize GD!');
    
    for( $i=0; $i<10; $i++ ) {
       imageline($image, 
             mt_rand(0,$width), mt_rand(0,$height), 
             mt_rand(0,$width), mt_rand(0,$height), 
             imagecolorallocate($image, mt_rand(150,255), 
                                        mt_rand(150,255), 
                                        mt_rand(150,255)));
    }
    
    for( $i=0, $x=0; $i<$length; $i++ ) {
       $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1);
       $x += 10 + mt_rand(0,10);
       imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, 
          imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155)));
       $code .= strtolower($actChar);
    }
       
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    
    $_SESSION['securityCode'] = $code;
    
    ?> 
    Code (php):
    this is the code to print random image but it prints some non readable characters...
    could anyone please tell, what's wrong with this code...:confused:
     
    Sangeetha.M, Jan 1, 2009 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Works fine for me. Link to where you have the script running?
     
    zerxer, Jan 2, 2009 IP
  3. azlanhussain

    azlanhussain Active Member

    Messages:
    640
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #3
    works fine for me too.. what PHP version are you running on?
     
    azlanhussain, Jan 2, 2009 IP
  4. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    what is your charset on the header?
     
    baris22, Jan 2, 2009 IP
  5. Sangeetha.M

    Sangeetha.M Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm using WAMP5....
     
    Sangeetha.M, Jan 2, 2009 IP
  6. Sangeetha.M

    Sangeetha.M Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Do i need to do anything with php.ini file...
     
    Sangeetha.M, Jan 2, 2009 IP
  7. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You shouldn't... the fact that it's printing out "non readable characters" means that the imagejpeg() is outputting the image properly. Make sure there's NO whitespace above your opening PHP tag (<? or <?php). Meaning, make sure the opening PHP tag is on the VERY first line with nothing to the left of it. That's the only thing I can think of.. that the header() isn't able to set the content-type as an image.
     
    zerxer, Jan 2, 2009 IP
  8. Sangeetha.M

    Sangeetha.M Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    But how to recognize whether the user entered the correct string sequence or not...
     
    Sangeetha.M, Jan 3, 2009 IP
  9. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    On your next page, the page that they submit the data to through the POST form, make sure you put session_start(); at the VERY top again (remember, no blank lines above the opening PHP tag). Then, check if the code they entered is the same. Here's an example, assuming the text input box they type the code into is named 'code':

    
    if(empty($_POST['code']) || $_POST['code'] != $_SESSION['securityCode']) {
        //Oh no! They either left the field blank or they did not enter the correct code!
    } else {
       //They entered it correctly!
    }
    $_SESSION['securityCode'] = null;
    unset($_SESSION['securityCode']);
    //Always best to just kill off security session values like this so they can't just hit back and keep trying or something like that
    
    PHP:
     
    zerxer, Jan 3, 2009 IP
  10. Sangeetha.M

    Sangeetha.M Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    But The image is not getting displayed when i include it in form....could you say why?
     
    Sangeetha.M, Jan 5, 2009 IP
  11. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #11
    Do you have gd installed?
     
    Kaizoku, Jan 5, 2009 IP
  12. Sangeetha.M

    Sangeetha.M Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The above scripts displays the image but when i include it in form its not displaying...
     
    Sangeetha.M, Jan 5, 2009 IP
  13. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #13
    How are you including it in form? You wouldn't do include() through PHP or anything.. just do <img src="location/to/script.php" /> and it'll work fine.
     
    zerxer, Jan 5, 2009 IP
  14. Sangeetha.M

    Sangeetha.M Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thankyou so much zerxer, it works perfectly...
     
    Sangeetha.M, Jan 5, 2009 IP