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: PHP, Php5, Short Open Tags, short_open_tag | Leave a Comment »
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: error, exhausted, Fatal error, memory, PHP | Leave a Comment »
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 )
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, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
Posted in Php Codes, Php Functions, Php Methods | Tagged: fly, image, imagecopyresampled, PHP, resize | Leave a Comment »
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
$to = ‘youraddress@example.com’;
$subject = ‘Test email’;
$message = “Hello World!\n\nThis is my first mail.”;
$headers = “From: webmaster@example.com\r\nReply-To: webmaster@example.com”;
$mail_sent = @mail( $to, $subject, $message, $headers );
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: PHP, Php Email, Simple Text Email | Leave a Comment »
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: class, Database, free, MySQL, object, oop, PHP, script, source, source code, sql, Web Development | Leave a Comment »
Posted by sarapeter on July 4, 2008
<?php
session_start();
http://www.white-hat-web-design.co.uk/articles/php-captcha.phphttp://www.gnu.org/licenses/gpl.html
class CaptchaSecurityImages {
var $font = 'monofont.ttf';
function generateCode($characters) {
$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 = $height * 0.75;
$image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
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);
}
$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');
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: Anti Spamming, Captcha, Images, PHP, Security | Leave a Comment »