Requests PHP Library

Recently i came across a fantastic PHP library for making HTTP calls to external URLs or web services.
The library is: Requests.

It can support HTTP calls of type: GET, POST, PUT, DELETE, etc. Here i am presenting an example of GET and POST, using the library. The best thing i liked about this library is that we can get the status code of the response and use it to determine the success of our request. If status code is 200, it means the request is Successful.

FacebookTwitterGoogle+LinkedInEmailShare

Add php support to Eclipse IDE and debug php scripts

If you are an experienced php developer, you might feel the need to debug your code and also develop the application code with the help of auto suggestions of an IDE. Eclipse is one of such good IDEs, which also supports PHP development, apart from Java (for which it is widely used).

Here i explain how can you add php support to Eclipse IDE and how to enable debugging of your php code using the Xdebug extension. I am using Eclipse Luna.

So first download Eclipse (https://eclipse.org/downloads) and open the Eclipse application. Follow the steps as highlighted in below screenshots (Click on images to enlarge them).

eclipse1

eclipse2

eclipse3

eclipse4

eclipse5

eclipse6

eclipse7

eclipse8

Now you have the PHP IDE environment ready for you!

In order to debug your code, you need to install an addon called XDebug for Eclipse.

Go to: http://xdebug.org/wizard.php, and paste your phpinfo() output there. Then when you analyse your phpinfo() output, it will provide few instructions similar to the below lines. You need to follow them to install the Xdebug support for PHP.

1. Download php_xdebug-2.3.3-5.4-vc9.dll
2. Move the downloaded file to \xampp\php\ext
3. Edit C:\xampp\php\php.ini and add the line
4. zend_extension = \xampp\php\ext\php_xdebug-2.3.3-5.4-vc9.dll
5. Restart the webserver

The following links might be useful:

https://wiki.eclipse.org/Debugging_using_XDebug
https://docs.joomla.org/Configuring_Xdebug_for_PHP_development

So once you have XDebug ready with your PHP (Look in your phpinfo() output), go ahead and configure that in your Eclipse.

eclipse9

Done :)

FacebookTwitterGoogle+LinkedInEmailShare

wordpress make user super admin from database

Here i am listing the steps to make a normal user of your wordpress site into a super administrator, from the backend (say, using phpmyadmin).

First, note down the user id of the user, whom you are trying to make the super admin. Then get his user level. Each user is assigned a level, based on his role. Reference.

Now you need to update the user level to 8, 9 or 10, in order for the user to have the administrator role.

Next, determine the current capabilities of the user.

It would be something like: a:1:{s:11:”contributor”;b:1;}

Now you need to run the below query, to set the user capabilities to that of a super admin user.

Done :)

FacebookTwitterGoogle+LinkedInEmailShare

php captcha code validation

Here is a simple code to display a form with captcha code validation in place. The code is divided into 2 files, one file with the form and the other file that generates random text as captcha image.

You can download the 2 files as a zip archive, from here.

And here is the Demo.

FacebookTwitterGoogle+LinkedInEmailShare

Install and uninstall LAMP packages on CentOS

Install LAMP packages:
# yum install httpd -y
# service httpd start
# chkconfig httpd on
# yum install mysql mysql-server -y
# service mysqld start
# chkconfig mysqld on
# mysql_secure_installation
# yum install php -y
# service httpd restart
yum install php-mysql -y
# service httpd restart
# vi /var/www/html/test.php > phpinfo();

Uninstall LAMP packages:
service httpd stop
service mysqld stop
yum erase httpd httpd-tools apr apr-util
yum remove php
yum remove php-common
yum remove mysql-server
yum remove mysql

FacebookTwitterGoogle+LinkedInEmailShare

mysql create user and grant permissions to a database

Here is the syntax for creating a new user in mysql, via command console:

Grant privileges for the ‘testusername’ user to the ‘wordpress’ database:

Finally flush the privileges:

FacebookTwitterGoogle+LinkedInEmailShare

github create project

I am sure most of the web developers would have heard about the famous repository service, Github. I have been using github for my projects lately and would like to share some quick and useful info on how to setup a project in github, and how to sync it with your local project.

I assume that you are using a windows machine, have xampp/wampp installed on it, along with GIT GUI/Git Bash softwares (you can download from: Here), and you have a github account online. So lets begin with creating a new project on github.

Steps:

1. Login to Github and create a new repository. Do not initialize the new repository with README, license, or gitignore files for now. You can add them later.
Create project on Github
2. Create a new php project in your localhost, with a couple php/html files.
3. Now right click inside your local project folder and select “Git Bash Here”. You will get a Git command prompt.
4. Initialize the local project directory as a Git repository, by typing: git init
5. Add all the files in your new local repository, in order to make them ready to be pushed to remote git repository: git add .
6. Commit the files that you have added above: git commit -m ‘First commit’
7. Copy the HTTPS clone URL of your remote repository on github.
8. In the Command prompt, add the URL for the remote repository where your local repository will be pushed: git remote add origin REMOTE_REPOSITORY_URL
9. You can verify the new remote URL, by typing: git remote -v
10. Push the changes in your local repository to GitHub repository: git push origin master

At this stage, you can check your remote github repository, and it should contain all the files of your local repository.

Done :)

Below are some of the tips for using Git:

> Clone Git repo to local folder:
1. Go to htdocs folder
2. Right click and select ‘Git Bash Here’
3. Type: git clone HTTPS_clone_URL_of_your_GIT_repository
4. Enter github credentials and you get GIT repo folder in htdocs
———————————————
> Force GIT to override all remote files:
git push -f origin master
———————————————
> Set git author & email:
git config –global user.name Tester
git config –global user.email tester@example.com
———————————————
> Upload all changes from local repo to github repo:
1. From the local repository folder, right click and select ‘Git GUI Here/Git Commit Tool’.
2. There, select the files you want to upload, under ‘Unstaged Changes’ and click ‘Stage Changed’ button. (You can initially click on ‘Rescan’ button to check what files are modified and not uploaded yet.)
3. Then write a Commit Message and click the ‘Commit’ button.
4. Now right click in the folder again and select ‘Git Bash Here’.
5. Type: git push origin master and enter your credentials. Done.
Note: You may need to do a ‘git pull origin master’ prior to push.
———————————————

FacebookTwitterGoogle+LinkedInEmailShare

php convert time from one timezone to another

I have cloned a git repository recently, which contains a php class with a function to convert the time between 2 different timezones.

So if you belong to India and want to know the time at Melbourne now (18:10, when i am writing this post in India), just substitute the 3 variables in the example file at: github.

Such an easy way to get the time at other timezone.
Note: You can define the timezone constants and the time differences in the file: Dates.php :)

FacebookTwitterGoogle+LinkedInEmailShare

could not create directory during wordpress plugin install

It is sometimes a big challenge for most of the wordpress developers or admins to update the plugins or themes or the core wordpress version of their site.

There are many forums and discussion threads out there, that address this issue, and most of the solutions do not work for everyone. Here i am listing a solution that worked for me when trying to update plugins of a wordpress site, for which i have root access.

Note that you either need to setup ftp server for your wordpress site and configure its credentials in the wp-config.php file, or enable proper permissions for the folders inside wp-content and ensure apache user ownership on them.

Method 1:

  • Login as root to your wordpress server.
  • Check that vsftpd (ftp server) is installed and running.
  • Configure the ftp user credentials on the server
  • Open wp-config.php file and mention the ftp settings:
  • Method 2:

  • Ensure that the directories inside wp-content have 755 permissions set. This can be achieved by running the following commands from putty, navigating to wp-content folder:
  • Ensure that the apache user has proper ownership of the directories inside wp-content.
    This can be achieved by running the following commands from putty, navigating to wp-content folder:
  • You may also need to include the below line in wp-config.php file:
  • So i have tried both the above methods and they worked for me. Hope it helps someone in the future.

    FacebookTwitterGoogle+LinkedInEmailShare