Category Archives: php

php related posts

how to install imagick for php on fedora-redhat-centos

Hi,

recently i moved some scripts to a new server, so i was needed to reinstall everything from the old one. this has been also a change of platform, as the old was on a debian lenny, and this new one was a RHEL 6.0. in resume, here are the instructions of how to install imagick and support it on your php installation for any redhat based distribution:

yum install ImageMagick.i386

yum install ImageMagick-devel.i386

pecl install imagick

echo “extension=imagick.so” > /etc/php.d/imagick.ini

service httpd restart

of course, change i386 with your architecture (like x86_64) and also, you need to install php-pear package to be able to use pecl and install the php extension.

i hope you find this useful :)

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

Solution for like and tweet button with adsense ads

Hi,

recently i was needed to add some like and tweet button to a site. i supposed will be a 5 minuted task, but i discovered something really weird, that for some reason, the button not only was screwing up the load of the adsense ads, but also, sometimes the ads appeared inside the iframe of the buttons. looking for a solution on the web, i found something for the facebook like button and i applied something seamless for the twitter button.

Facebook Like Button

the steps to solve this are the following:

1)go to this page and create an application: https://developers.facebook.com/setup/

2) paste this code where you want the like button and edit with the correct paths and the appID you generated before

<!--  like button  -->

<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like layout="button_count" width="100"></fb:like>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId  : 'XXXXXXXXXXXX',   // ID generated for WEBSITE provided above.
xfbml  : true,  // parse XFBML
channelUrl  : 'http://www.YOUR_SITE_NAME.com/facebook/channel.html'  // new file custom channel
});
};

(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>

<!--  like button  -->

3)create the channel.html file on the path of your server and paste this only line:

<script src="http://connect.facebook.net/en_US/all.js"></script>

and that’s it, now you have a like button and will not screw up your adsense ads

Tweet Button

for the tweet button, the solution is more simple, i basically pasted the code inside a file and called it from a iframe. is not the most cleanest way, i know, but is the only thing i figured out to solve it.

i hope this help you in some way :)

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

Multilanguage AJAX with the serialize function

Hi,

weeks ago i posted this function that basically let you serialize object and arrays in javascript and is compatible with the unserialize funcion in php. recently i discovered a problem in this function related with the management of the weird, other language characters. the problem is that when you save a word with, let say, russian characters, the count of the chars is different as how php do it, so when you try to unserialize the string you get a offset errors. to solve this, you need to convert the string into unicode, serialize it and then send it to the php.

the function to convert any string into unicode with javascript is the following:

function convertToEntities(tstr) {

  var bstr = '';

  for(i=0; i127)

    {

      bstr += '&#' + tstr.charCodeAt(i) + ';';

    }

    else

    {

      bstr += tstr.charAt(i);

    }

  }

  return bstr;

}

and in the php, you process the string like this:

$unserliazed= html_entityt_decode(unserialize($string),ENT_NOQUOTES, "UTF-8");

is very important that you set the UTF-8 charset to the decode function, so you get the original string.

also, remember that the database need to be support unicode-utf8, here you will find a quick guide for it

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

Paypal Express Checkout integration

Hi,

a few days ago i started to make a paypal express checkout integration at the project i’m working on. i started to look at the developer documents to know how it works. all the most valuable info is here, here here and the samples of code for several languages are here.

How express checkout works?

EC works in this way: in your website you have a shopping cart or a buy now button. with the api signature or certificate that grant you access to the paypal API, you make a call to the NVP service SetExpressCheckout. this service will register the order at the paypal server. to this service you send all the order data, as the price of each item, the taxes, if need to be shipped or not etc.. paypal will return to you a access token, that you will use to redirect the user to the paypal website to authorize the payment. when the user authorize the payment, ger redirected to the returnurl you defined at the SetExpressCheckout call. in that moment you will ask the user any necessary data that you may need (as the shipping address or any other personal data) and put a button to confirm the order. when the user press the button, you call to the DoExpressCheckout service at paypal, sending the same parameters like the last call adding the toke and payerid that paypal give to you. and that’s it, the order has been processed, paypal with return to you if the order has been processed or not and the transaction id that you should keep in a safe place

now i will give you some tips that could help you at the moment of the integration:

Get test account from the sandbox environments

one of the docs i linked here, give you some api username, password and signature ready to use, but i tested it with them and i just got the general error of the timeout, so basically they are not valid. so go to developer site and sign up for a account. remember to put a email and user names for the test account that will not have numbers, if not you will get a error at creating the test accounts.

3dr party billing

one of the best features of EC is the 3dr party billing. with this you can use your own account api access to make the order but will be credited to someone else. this is useful when you have a project that offers the users a way to charge for the service or something like that. the money charges goes directly to the specified account. to make this you need to add a SUBJECT parameter to the call, with the client paypal email and also the client need to add your API username in the list of their api access. there is no fees and don’t have any cost for your account to do this.

Timeout error

this is a general error that you can get trying to make the calls at paypal, most of the errors are descriptive in their problem, just print the return of paypal to figure it out. if you get this error could be for several reasons, like a really timeout why the paypal server are down or your connection have some problem. or the api credentials are wrong, or you miss or have some parameter wrong in the call. unfortunately is a try and error task to solve this.

i hope it help you in some way to get working your EC integration easy and fast :D

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

Improve Performance Tips

Hi,

today i wanted to give some common and not so common ideas of how to improve the performance of your web app. as you know, when you have a busy website, the performance (and along with the stability) is a key factor to have your project up and running, in this cases, you need to try to improve every aspect of your app, that is the idea, to help you to identify what can be or not be done in your case.

Database Optimization

Build a efficient database schema is not something easy, it cost time, study and testing to do it. at the time to build a efficient DB schema you need to take in consideration the following:

*Use the minimum columns as possible, and choose wisely the type of data that the database will store (if you will store number of 3 digit max, set a int(3) and not a varchar, text or a int(20))

*use the relational model, for dynamic or multiple values, use a index pattern where you have the definition of the data in 1 table and the data itself inserted by the user in other. do not create tons of columns as value_1, value_2 etc..

*add indexes to your tables

*use the primary key and unique key for columns that should have unique values to avoid data corruption or misbehavior.

in cases of the query, try to group the query’s in the less quantity of calls, use JOIN’s,  select only the columns that you will really going to use, don’t make generals select’s. in case you need to return the number of something, use functions as SUM() and not mysql_num_rows().

Clean and re-usable code

you need to be careful at coding too, even that php bring a big liberty whiteout having to worry about types and variable types, you need to be responsible for what you write. the php code get messy when you do messy stuff. organize variables, functions and classes as much as you can. i’ll recommend to use a MVC model if is possible (even that before i rejected it) and a framework will also make you life easier when you learn how to use it.

there is always a good practice separate the logic from the view (i mean the main php code from the design), for this you can grab any framework, most of them already support this or get a template engine. i have always used smarty, and is very flexible and you can extend it with custom functions and modifiers, also it support time cache and others types too. even that i’m happy with it, and i’ll recommend to you, i have realized with the time that is a waste if time. it will never be faster than the normal php code, so if you want to go really well, i’ll recommend you to get a or build a class that will just load the php script as templates and that’s it. you will save time from learning a template script and save process in the meantime

cache everything

this is one of the most important thing of all. the cache is the base of any optimization. let say true, most of the pages of any app doesn’t change and if they change dynamically, is usually for a user action. if this happen you can use a selective caching, where you cache at the event if a action. make this type of cache is not a easy task, takes time and testing, but really it make great results on the practice and worth the time invested on it. the cache of css and js files is also a good technique, as well the compress of them.

use the power of javascript

i learning caching a lot of stuff to use the power of javascript. i have cached pages that needed to be refreshed each 20 seconds. surely you are asking how i made it, so basically i just cached them once and filtered the results with javascript after the ajax load of the filter using JSON and the html code from other call. if you are wondering if this is really a performance advantage, well yes, you will see a 50% of difference from a normal php processing. anyway, be careful with this type of technique, remember that javascript run on the client side, so the information passed there can be manipulated in evil ways. is a powerful tool that need to be used very careful. also, i will recommend you to use a javascript framework, they are cross browser compatible for almost all their functions, with a big community and plugins available, and heavily optimized.

i hope this help you in some way at the moment to optimize your web app’s :D

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

Secure Passwords: phppass

Hi,

in any web app (or desktop app) the security and store of password is a critical subject. there is a lot of solution out there for desktop and if you are around in the web development you will know the usual solutions as well. the hashing of the password is *usually* the most popular option (is discarded any attempt to store the password in plain text of course) and even that mysql have in-built functions as AES_ENCRYPT and AES_DECRYPT, i still think is insecure have any option to get the encrypted password in some way.

for the hash of the password, there are lot of popular functions, as md5(), sha1() etc.. and the big problem with all this functions is the advance of the processing units of our computers have made easier for the cracker to figure out the real password behind the hash.

recently, with the attacks that pletyoffish.com and other important webs have suffered i started looking for the strongest hash encryption i could find (in the past i used a combination of sha2 with a unique salt and a second pass of sha2). in this search, i realized that bcrypt was the best option here. knowing that, i looked for a class that could wrap that process and make it easier. then is when i found phpass.

phpass is a wrapper, that will use the strongest hash function available (in this case bcrypt) and if not, will go down with a combination of functions. is really easy to use, as you will see in the following examples:

Encrypt password:

$hasher = new PasswordHash(8, FALSE);
$hash = $hasher->HashPassword($pass);

Check password at login

if ($hasher->CheckPassword($pass, $hash))

in this example, you need to  provide the plain password and the hashed password that is keep on the DB.

if you have already a hashed password system for login, the transition should be painless, if not, i recommend you do it as soon as possible. this is better to any system you could make, really will make you sleep better at night and don’t wake up at night with the notice that your site has been hacked.

you will find more info in their tutorials and in their home page

i hope it help you in some way :)

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

PHP resize solution: phpThumb

Hi,

on the web app i have been working this past months, we allow the users to upload avatar photos, and some other pictures that are shown at the site. so, i implemented the code i always use to resize the images. it have done the job well on the testing phase but we found that we have bad quality images when the users uploaded their photos. so i looked for a solution and i found phpThumb. this class/script generate resized images in a quickly and easy way. it comes in 2 flavors, in a ready to use script where you pass the parameters of where is the source image, destination, measures etc.. and will print you the image (can be used in img tags as source). and also you have the class where you can manually convert the files and save it on a file or print it etc..

the class use as input the location of the source image, the binary data or a GD resource. also, phpThumb will try to use imagemagick if is installed on the server (that will give you a better quality image) and if is not available, will use GD automatically. i have tried it and i need to say that the best configuration i made to get the best quality image possibly was:

 $phpThumb->setParameter('output_interlace',true);
 $phpThumb->setParameter('config_output_format', 'png');
 $phpThumb->setParameter('fltr', 'q|95');
 $phpThumb->setParameter('config_imagemagick_path', '/usr/bin/convert');

i have tried to use jpeg format, but sometimes i get some ugly and pixelated grey background at the thin lines of the image (like words etc..) so the best option was to use png.

you can download phpThumb from here

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

How To: install murmur and MumPI on debian lenny

Hi,

this time i will write you a mini-howto install murmur and a administration panel, called MumPI.

What is murmur?

murmur is the server part of the open source voip communication program called Mumble. Mumble is a low latency voip program, that allow you communicate with several people on the same channel. murmur is the server part, the app that handle the communication between users.

Murmur installation

in debian is pretty easy to install it, just run the following apt-get

apt-get install mumble-server

that line will install murmur and set up as a service  (like apache, mysql etc..) and run each time the machine booth. as any service, you can stop it, restart it, doing a /etc/init.d/mumble-server start|restart|reload|stop

now we need to install that will be the middle man between mumble server and MumPI, the php administration panel.

for this, just use this apt-get line:

apt-get install icecpp libzeroc-ice32 php-zeroc-ice lzma

for the current version of mumble and MumPI, check that you are installing ice 3.4, if not, go to ICEphp for a updated deb package. after the installation, open with your favorite editor:

/etc/mumble-server.ini

and look for a string like this:

ice="tcp -h 127.0.0.1 -p 6502"

if you don’t have it, add it after the dbus declaration.

now come the last step, go to MumPI website and download/extract the last version on your web end directory (i assume you already have apache and php configured for some domain)

after the extraction, just open the location with your browser and MumPI will be installed.

now, open

/etc/php5/conf.d/IcePHP.ini

and change the declaration of ice.slice to:

ice.slice=/path/to/MumPI/libs/Murmur122_fixed.ice

and that all folks ! now you can manage users, create virtual servers and enjoy of one of the best open source voip app out there :)

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

Simulate User Testing: Selenium IDE

Hi,

long time since my last post right? sorry about that, too much work and things happening lately :) .

this time i’m here to share the discover of a great tool that a friend of mine told me some days ago. is called selenium IDE. this tools  comes in several formats, but what i’m interested in the FF addon version. this addon let you record user actions in a website, as login into a site, submit a form, click a link etc.. all of this can be recorded and reproduced anytime. this is useful why you can create a test suite of diferent’s actions for your app, and after you added some change or feature, you can run the test’s and know if something is not working.

selenium also accept scripting,in a markup language like xml where you can manually define actions, as waiting for some DOM object to show up, or some value to get set to  launch an action.

i recommend you to take a look at it, will save you time and headaches in the future :)

you can download it from here.

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post

Creating images with GD in php

Hi,

GD is a php extension that let you create images using code.between the images format are jpg, gif, pnf, swf,tiff and jpeg200. GD let you create images from 0, you can grab a image (from a full path or from a url) and edit them, draw things in it or mix it with other images, also you can write strings in different colors and fonts (it support ttf fonts).

i will share some examples manipulating images using GD:

1 color image:

 $im  = imagecreate (150, 30); // create a blank image
 $bgc = imagecolorallocate ($im, 255, 255, 255); // create a background color
 imagefilledrectangle ($im, 0, 0, 150, 30, $bgc); // draw a rectangle of the size of the image with the background color

resize image:

// Load the images
$thumb = imagecreate($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize the images
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Showing the resized image
imagejpeg($thumb);

Mix 2 images:

// Create image instances
$dest = imagecreatefromgif('php.gif');
$src = imagecreatefromgif('php.gif');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 47, 75);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);

i hope this help you in some way :D

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Post