php functions to prevent hacks

Categories: php

Tags: hack, php

strip_tags() removes html and php tags from string. should be immediately called on data input and before any data processing functions.

nl2br() converts newlines to <br />. run strip_tags before calling this function or the <br /> will be stripped.

htmlspecialchars() convert special chars to html entities. & (ampesand) becomes &amp;, < becomes &lt; etc.


php: is gd library installed?

Categories: php

Tags: gd, image, php

to check if the server has gd library,

PHP:

if (extension_loaded("gd"))
    echo "yes!";

another way is to check any gd related function, eg

PHP:

if (function_exists("gd_info"))
    echo "yes!";

php: get image size

Categories: php

Tags: getimagesize, image, php

to access basic image information like width and height, call getimagesize, which returns data in array format such as

Code:

Array
(
    [0] => 640  // width
    [1] => 426  // height
    [2] => 2    // not sure what this is. anyone?
    [3] => width="640" height="426" // for <img> param
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg  // mime type
)

note: this function does not require gd library.


php: image manipulation

Categories: php, unix

Tags: fedora, gd, image, php, ubuntu

the gd image library provide functions to manipulate image files.

on some installations, this does not come as default and needs to be installed.

on fedora, the command is

yum install php-gd

on ubuntu, the command is

sudo apt-get install php5-gd


php: increase memory limit

Categories: php, unix, apache2

Tags: memory, php

errors like ‘allowed memory size of x bytes exhausted’ or ‘zero size reply’ usually means there’s not enough memory allocated for php. you need to increase the value.

if you have admin access, you can edit /etc/php.ini, locate and update ‘memory_limit’ to values like 16M, 32M, 64M. this change is global to the system. restart apache after update.

if not, add this in php script:

PHP:

ini_set("memory_limit""32M");

1 2 »

Affliates

Spreadfirefox Affiliate Button