PHP Expert Programmer

Just another WordPress.com weblog

Posts Tagged ‘PHP’

Short open tags not work in php5.2.0 and not in php5.2.3

Posted by sarapeter on April 20, 2009

Short open tags not work in php5.2.0 and not in php5.2.3

My advice was to put short_open_tag = On in the php.ini file.

The server status is short_open_tag = Off

I set this to short_open_tag = On in the php.ini file in the root of the site.

This did not work but when I renamed the php.ini file to php5.ini this corrected this issue.

However I did not immediately notice but this blocked out the server php.ini file – this start a major component working as it also blocked out zend optimizer.

I have read more about the issue of the short tag.

I have in my limited experience of php coding always used <!–p and not <?
There appears to be a number opinions that setting short_open_tag = On can cause problems where there are xml files as attempts will be made to parse these.

I have been through every php file in the component – I can’t find a short code anywhere in the files.

Posted in Php Codes, Php Errors, Php Security | Tagged: , , , | Leave a Comment »

Fatal error: Allowed memory size of 8388608 bytes exhausted

Posted by sarapeter on March 26, 2009

Php Fatal error: Allowed memory size of 8388608 bytes exhausted

This error message can spring up in a previously functional PHP script when the memory requirements exceed the default 8MB limit. Don’t fret, though, because this is an easy problem to overcome.

To change the memory limit for one specific script by including a line such as this at the top of the script:

ini_set("memory_limit","12M");

The 12M sets the limit to 12 megabytes (12582912 bytes). If this doesn’t work, keep increasing the memory limit until your script fits or your server squeals for mercy.

You can also make this change permanently for all PHP scripts running on the server by adding a line like this to the server’s php.ini file:

memory_limit = 12M

Keep in mind that a huge memory limit is a poor substitute for good coding. A poorly written script may inefficiently squander memory which can cause severe problems for frequently executed scripts. However, some applications are run infrequently and require lots of memory like importing and processing a big data file.

Posted in Php Codes, Php Errors, Php Functions, Php Methods, Php Security | Tagged: , , , , | Leave a Comment »

Resize an image on the fly & keep its aspect ratio

Posted by sarapeter on March 25, 2009

Resize an image on the fly & keep its aspect ratio

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

In other words, imagecopyresampled() will take an rectangular area from src_image of width src_w and height src_h at position (src_x ,src_y ) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x ,dst_y ).

If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst_image is the same as src_image ) but if the regions overlap the results will be unpredictable.

<?php
// The file
$filename 'test.jpg'
;
$percent 0.5
;

// Content type
header('Content-type: image/jpeg'
);

// Get new dimensions
list($width$height) = getimagesize($filename
);
$new_width $width $percent
;
$new_height $height $percent
;

// Resample
$image_p imagecreatetruecolor($new_width$new_height
);
$image imagecreatefromjpeg($filename
);
imagecopyresampled($image_p$image0000$new_width$new_height$width$height
);

// Output
imagejpeg($image_pnull100
);
?>

Posted in Php Codes, Php Functions, Php Methods | Tagged: , , , , | Leave a Comment »

Php extract method

Posted by sarapeter on March 1, 2009

“extract()” Method


Experimentally I found that calling extract() also shows the number of keys if the key is set and is not numeric ! Maybe there was a better definition than mine  . Please have a look to this scripts :
<?PHP
$var
["i"] = “a”
;
$var["j"] = “b”
;
$var["k"] = 1
;
echo
extract($var);
// returns 3
?>

<?PHP
$var2
["i"] = “a”
;
$var2[2] = “b”
;
$var2[] = 1
;
echo
extract($var2);
// returns 1
?>

 

(Arash Moslehi)

Posted in Php Functions, Php Methods | Tagged: , | Leave a Comment »

Sending a Simple Text Email

Posted by sarapeter on February 8, 2009

At first let’s consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. The mail() function returns True if the message is sent successfully and False otherwise. Have a look at the example:

<?php
//define the receiver of the email
$to = ‘youraddress@example.com’;
//define the subject of the email
$subject = ‘Test email’;
//define the message to be sent. Each line should be separated with \n
$message = “Hello World!\n\nThis is my first mail.”;
//define the headers we want passed. Note that they are separated with \r\n
$headers = “From: webmaster@example.com\r\nReply-To: webmaster@example.com”;
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print “Mail sent”. Otherwise print “Mail failed” 
echo $mail_sent ? “Mail sent” : “Mail failed”;
?>

As you can see, it very easy to send an email. You can add more receivers by either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers. If you don’t receive the test mail, you have probably installed PHP incorrectly, or may not have permission to send emails.

Posted in Php Codes | Tagged: , , | Leave a Comment »

PHP MySQL Database Class

Posted by sarapeter on July 29, 2008

A class for very basic MySQL database connectivity. Written to reduce redundant code in my own projects aswell as aid in debugging and error reporting during the developement phase. Currently connects to a database, execute external files containing SQL commands, insert and update from an array of key => value pairs, insert and update with sql command, query (one result), query (multiple rows), and dump a select query to a table. The zip file contains the class aswell as a demonstration script.

Download Version 1.0.4: db-1.0.4.zip

Posted in Php Database, Php MySql | Tagged: , , , , , , , , , , , | Leave a Comment »

PHP Captcha Security Images for Anti Spamming

Posted by sarapeter on July 4, 2008

<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

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['height'] < 600 ? $_GET['width'] : '120';
$height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6';

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

?>
Download Link

Posted in Php Security | Tagged: , , , , | Leave a Comment »