Skip to main content


Showing posts from 2011

PHP ssh2_connect() not working?

Most of the times we use ssh2_connect() function to connect and download files from remote server without using ftp. Sometimes it doesn't work. Please note some useful tips: 1) Make ensure that openssl is enabled and libssh2 is installed on your server. 2) Even after that when calling ssh2_connect() function if you are getting errors like following: [Sat Dec 03 19:30:35 2011] [error] [client] PHP Warning: ssh2_connect(): Unable to connect to on port 22 in /var/www/html/connection.php on line 9 [Sat Dec 03 19:30:35 2011] [error] [client] PHP Warning: ssh2_connect(): Unable to connect to in /var/www/html/connection.php on line 9 It means apache is not allowed to send packets using tcp. Then run the following command from your server console: //Allow HTTPD scripts and modules to connect to the network using any TCP port. # setsebool -P httpd_can_network_connect 1 here P stands for permanent. Sometimes th

PHP redirection through header() function not working?

header ( "Location:" ); While using header function sometimes you got the error like: ' Cannot add/modify header information – headers already sent by…'  To avoid such situation you can use output buffering or just make sure that nothing was sent to the client.   Remember also that headers MUST be sent before any output is started. There are no exceptions from this rule, in fact you cannot even sent a blank line to the output, executing code like this: echo ""; 

How to write PDF file in PHP?

PHP is a dynamic web development language with some surprising capabilities. It can even create PDF documents on the fly, if right third party library is available. Here in this post we are using FPDF library. You can download the same from here . Following is the example how you can write a simple pdf file using this library: "pdf.php" <?php     //Connection to the database     $link = mysql_connect("localhost","root","");     if (!$link) {         die('Could not connect: ' . mysql_error());     }     mysql_select_db("practice");     //end of database connection code     require('fpdf.php');     $pdf=new FPDF();     $pdf->AddPage();     $pdf->SetFont('Arial','B',10);     $pdf->Cell(50,3,"Learn how to write PDF Files in PHP");     $pdf->Ln();     $pdf->Ln();     $pdf->SetFont('Arial','',6);     $text = "A

CakePHP url rewriting problem?

If you are facing url rewriting problem in cakephp even though you have enabled rewrite module .....Here is the solution.... - Just go to the httpd.conf file. - Find the following text groups   <Directory />        Options FollowSymLinks        AllowOverride None        Order deny,allow        Deny from all   </Directory> and   # AllowOverride controls what directives may be placed in .htaccess files.   # It can be "All", "None", or any combination of the keywords:  # Options FileInfo AuthConfig Limit  AllowOverride None. - Replace 'AllowOverride None' with 'AllowOverride All' on both places. - Save file and Restart Apache Refresh Page and the problem is resolved. :)

SMS Gateway Integration in PHP

To Integrate SMS Gateway in PHP you have to signup for an account with SMS service providers. To find any one out of them just google for it and you will get a list of providers. All of them are paid service providers and some of them provide you just few free credits so that you can test their service. Following is the example of such a provider After logged in it provides us an API that we would have to use for sending sms. You can connect to the gateway using HTTP/s, SMPP, SMTP, FTP, XML, SOAP etc... I have opted for HTTP/s Following are the step to connect via HTTP:     1) When you sign up for an HTTP/S account, you will be given a username, password and api_id: keep these at hand!     2) Once your registration has been activated you will receive 10 free credits with which to test our service.     3) Have the number you wish to send to ready in international format e.g. 448311234567.     4)Open your browser (e.g. Internet Explorer), a

How to access remote server using PHP SSH?

To remotely access a server in PHP provides a full library of functions called SSH2 functions. You can get the manual from the link . You'll get most of the information from the manual itself but it also leaves the user with some questions which are unanswered. Here i am just trying to explain the same in some better way.. Simple Example :  <?php     if (!function_exists("ssh2_connect"))         die("function ssh2_connect doesn't exist");     if(!($con = ssh2_connect("", 22(port number)))){         echo "fail: unable to establish connection\n";     } else {         if(!ssh2_auth_password($con, "username", "password")) {             echo "fail: unable to authenticate\n";         } else {             echo "okay: root logged in...\n";             $stream = shell_exec("mkdir /var/www/shiv");         }     } ?> Important Notes : 1) If you are logged i

CakePHP-MultivalidatableBehavior: Using many validation rulesets per model.

If you are looking for action specific validation on a model then here it goes like that : Following code examples are based on the following scenario: 1) We have a model Member. 2) We wanna two type of validation sets on that.      a) First in case of Login (default)      b) Second in case editing Profile.  3) How to use these validation sets in action. Step 1 : <?php      class MultivalidatableBehavior extends ModelBehavior {     /**      * Stores previous validation ruleset      *      * @var Array      */     var $__oldRules = array();     /**      * Stores Model default validation ruleset      *      * @var unknown_type      */     var $__defaultRules = array();     function setUp(&$model, $config = array()) {         $this->__defaultRules[$model->name] = $model->validate;     }     /**      * Installs a new validation ruleset      *      * If $rules is an array, it will be set as current validation ruleset,      * otherwise it will look into Mode

Automatic Startup of Apache on Linux Server?

To start Apache automatically, follow these steps: Copy Apache startup file to the startup directory: cp /usr/local/apache/bin/apachectl /etc/init.d/             In my case ‘apachectl’  was at location /usr/sbin/apachectl so I have used             cp /usr/sbin/apachectl /etc/init.d/  Edit /etc/init.d/apachectl by inserting these 2 lines in bold :            #!/bin/sh # # chkconfig: - 345 85 15 # description: Apache is a Web server used to serve HTML files and CGI. # # Copyright 2000-2005 The Apache Software Foundation or its licensors, as # applicable. . . . # Apache control script designed to allow an easy command line interface # to controlling Apache.  Written by Marc Slemko, 1997/08/23 . . . Enable httpd to startup automatically:            # chkconfig –-add httpd To start mysql on startup too, use:          # chkconfig –-add mysqld Reboot

Installing GUI under CentOS 6

If you installed using a text login and wish to switch to a graphical login, follow this procedure. 1) If you are not already root, switch users to the root account: su - Provide the administrator password when prompted. 2) If you have not already done so, install the X Window System and a graphical desktop environment. For example, to install the GNOME desktop environment, use this command: yum groupinstall "X Window System" Desktop To install the KDE desktop environment, use: yum groupinstall "X Window System" "KDE Desktop" This step may take some time as your Linux system downloads and installs additional software. You may be asked to provide the installation media depending on your original installation source. 3) Run the following command to edit the /etc/inittab file: vi /etc/inittab 4) Press the 'i' key to enter insert mode. 5) Find the line that includes the text initdefault . To change from a console to a grap

How to install Linux – CentOS 6 for Servers & Desktops

1) Set your computers BIOS to boot form CD / DVD 2) Insert the CentOS 6 CD or DVD 3) When presented with the following screen press enter on Install or upgrade an existing system. 4) You will now be presented with a CD Check, I always skip this step… I live life on the edge when it comes to media checking on Linux installs. However you might want to check the media. 5) You will now see some black and white text on your screen as your computer loads the X11 environment for the CentOS install, after 30 seconds or so you will be presented with the CentOS GUI installer, click next. 6) Select your Language, I am British (please no jokes about drinking tea on Twitter…) so I would select English (English). 7) Select your keyboard type 8) Storage type, as I am installing on my local hard drive I selected “Basic storage type” if you are using iSCSI or similar technology now would be the time to set it up. 9) As this is a clean install and the drive has never

PHP Reference Sheet (Basics)

Data Types integer, float, boolean, string, array, object, resource, NULL Variable Declarations $variablename = <value>; $anothervariable =& $variablename; (Assign by Reference) Declare Array $arrayname = array(); Initialize Array $arrayname = array(<value1>, <value2>, <value3>); $arrayname = array(<key> => <value>, <key> => <value>); (Define Keys) $multiarray = array(<key> => array(<value1>,<value2>)); (Multi-dimensional) Common Array Functions sort(<array>); (Sort array assigns new keys) asort(<array>); (Sort array maintain keys) rsort(<array>); (Sort array in reverse, new keys) arsort(<array>); (Sort array in reverse, maintain keys) count(<array>); (Count elements) count(<array>,COUNT_RECURSIVE); (Count multidimensional array) array_push(<array>,<value>); (Push item onto end of array) array_pop(<array>


Last week, when using the GROUP_CONCAT() function on a MySQL database, I got an unexpected result. :( Indeed, instead of getting my result as VARCHAR types, I got it as BLOB types! For information, a BLOB is a binary large object that can hold a variable amount of data: Because BLOB values are treated as binary strings, it is not easy to use. This is why we would prefer to have VARCHAR values. So the question is how to get around this frustrating problem? The answer is, for once, very simple! :D You simply need to: Open your my.ini or my.cnf file; Change the value of the group_concat_max_len system variable to 512 (no ‘k’ suffix); Restart the mysql service To verify if the value has been successfully updated, execute the following command in your mysql client: mysql> show variables like "%concat%"; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | group_

Different type of Cookies?

What are Temporary Cookies? Temporary , or session , cookies are removed from your computer after you close the browser. Websites use them to store temporary information, such as items in your shopping cart.   What are Persistent Cookies? Persistent , or saved , cookies remain on your computer after you close the browser. Websites use them to store information, such as your sign-in name and password, so that you don't have to sign in each time you go to a particular site. Persistent cookies can remain on your computer for days, months, or even years. What are First-party Cookies? First-party cookies come from the website that you're viewing and can be either persistent or temporary. Websites might use these cookies to store information that they'll reuse the next time you go to that site. What are Third-party Cookies? Third-party cookies come from other websites' advertisements (such as pop-up or banner ads) on the website that you're

Cloud Computing vs. Virtualization

Cloud computing and virtualization are both technologies that were developed to maximize the use of computing resources while reducing the cost of those resources. They are also mentioned frequently when discussing high availability and redundancy. While it is not uncommon to hear people discuss them interchangeably; they are very different approaches to solving the problem of maximizing the use of available resources. They differ in many ways and that also leads to some important considerations when selecting between the two. Virtualization: More Servers on the Same Hardware Virtualization It used to be that if you needed more computing power for an application, you had to purchase additional hardware. Redundancy systems were based on having duplicate hardware sitting in standby mode in case something should fail. The problem was that as CPUs grew more powerful and had more than one core, a lot of computing resources were going unused. This obviously cost companies a great d

Connection between PHP (server) and Android (client)

Nowadays, PHP and Android are widely used platforms, while PHP is used for dynamic web development, Android is an OS for mobiles, tablets etc. To develop Android applications we can use the benefits of PHP to communicate it with MySQL database and get required data from there. We can use JSON and HTTP as an intermediate of communication between Android and PHP. The whole process can be divided into five steps: 1)       An HTTP request is sent from Android(Client) to PHP(Server). 2)       PHP runs it’s script to make connection with database, and send request to MySQL database for required data. 3)       MySQL responds back to PHP with requested data. 4)       PHP script analyse the date and coverts it into information. The whole information is then converted into JSON format and sent to the Android (Client). 5)       Android script reads the data, perform its operations and shows it on it’s screen. The Whole process has also been described in the following