Resize image in PHP
Uploaded images from user can be of any size. If you need to use these images, they need to be resized upon upload. Here is a helper class to manage resizing of the images.
Let me know if you find any bugs.
Enable register_global through .htaccess
I would not recommend to enable register_globals, but if you need to enable the register_globals for a particular web folder, you can edit the .htaccess file and append the following to it
php_flag register_globals on
Restart apache and you are on your way.
PHPImpact: 30 useful PHP classes
PHPImpact has posted 30 useful PHP classes here.
Which ones do you use? I have used the following:
Still a lot more interesting ones which I haven’t tried.
PHP single quotes Vs double quotes
Any time you put something in “double” quotes, you are asking PHP interpreter to check that content for a variable. So even though the line do not contain variables within the double quotes, PHP will waste precious computing time scanning them anyway.
$sql = 'select * from employee';
will be much faster than
$sql = "select * from employee";
mysql_insert_id() returns NULL or 0
If you have multiple mysql connections (i.e. mysql_connect() or mysqli_connect()) on the page you will need to specify the the connection you are using when calling this function.
example:
mysql_insert_id($MY_CONN);
Secure file upload
No matter what web based technology you are using, these tips will always be helpful when it comes down to uploading files:
- Keep uploaded files where they cannot be directly accessed by the users via a direct URL. This can be done either by storing uploaded files outside of the web root or configuring the web server to deny access to the uploads directory.
- Use system-generated file names instead of the names supplied by users when storing files on the file system. This will prevent local file inclusion attacks and also make any kind of file name manipulation by the user impossible. More information can be found here.
Downloading a page in PHP
If you ever need to download a HTTP/FTP or any page, try not to use fopen() and fread() functions. They work but will take enormous amounts of time as compared to curl functions. Try doing as follows:
To GET a page using a url
<?
$url=”http://any.url”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
$xml = curl_exec ($ch);
curl_close ($ch);
?>
To POST to a page using values and fields:
<?
$url=”http://any.url”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, “fieldname=fieldvalue&fieldname=fieldvalue&”);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
$content = curl_exec ($ch); # This returns HTML
curl_close ($ch);
?>
For more information, check the curl function on PHP website.
PHP Data Objects
I was looking for a quick reference/tutorial to implement Data Objects with PHP, very similar to what exists in Java. Found a nice explanation at PHP Data Object
