Saturday, 10 December 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 the above command doesn't work. Then for temporary solution use this one..

# setsebool httpd_can_network_connect 1

It would work like charm :)

Sunday, 4 December 2011

PHP redirection through header() function not working?


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 ""; 

Saturday, 3 December 2011

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:



    //Connection to the database
    $link = mysql_connect("localhost","root","");
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    //end of database connection code


    $pdf=new FPDF();


    $pdf->Cell(50,3,"Learn how to write PDF Files in PHP");

    $text = "As a PHP programmer on any day you may need to write PDF file using PHP at run time. Here i have just tried to include each and everything required to write pdf file. Please go through the following blog link.";
     /* While you are using text we would recommend to use this function only because This method prints text from the current position. When the right margin is reached (or the \n character is met) a line break occurs and text continues from the left margin. Upon method exit, the current position is left just at the end of the text. */


    $link = "";
    $pdf->SetTextColor(0,136,60); //set color of the text using R,G,B combination

    $pdf->Cell(10,5,"User Id");

    //Get data from table.
    $result=mysql_query("select * from users order by user_id");
    $pdf->Image('images/php.jpg', 50, 50, 55, 35, 'JPG'); // inserts the image
    $pdf->Output("shiv.pdf","F"); // It would automatically call $pdf->Close();


Download the library and check more commands you can use to create an effective pdf as per the requirements.

Thanks!!!!!!!!!! Enjoy Programming :)