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 172.16.xxx.xx] PHP Warning: ssh2_connect(): Unable to connect to 172.18.xx.xxx on port 22 in /var/www/html/connection.php on line 9
[Sat Dec 03 19:30:35 2011] [error] [client 172.16.xxx.xx] PHP Warning: ssh2_connect(): Unable to connect to 172.18.xx.xxx 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?

header("Location: http://dirtyhandsphp.blogspot.com/");

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:

"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 = "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.";
    $pdf->Write(5,$text);
     /* 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. */

    $pdf->Ln();

    $link = "http://dirtyhandsphp.blogspot.com/";
    $pdf->SetTextColor(0,136,60); //set color of the text using R,G,B combination
    $pdf->Write(5,$link,$link);
    $pdf->Ln();

    $pdf->SetTextColor(0,0,0);
    $pdf->Cell(10,5,"User Id");
    $pdf->Cell(350,5,"Username");
    $pdf->Ln();
    $pdf->SetFont('Arial','',6);
        $pdf->Cell(450,3,"-------------------------------------------------------------------------------------");

    $pdf->Ln();
    //Get data from table.
    $result=mysql_query("select * from users order by user_id");
    while($row=mysql_fetch_array($result))
    {
        $pdf->Cell(10,5,"{$row['user_id']}");
        $pdf->MultiCell(350,5,"{$row['user_name']}");
    }
   
    $pdf->Ln();
    $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 :)

Gmail: Download blocked file

Hi, today we are going to learn, how to download blocked file in gmail. Sometimes you see following message in gmail: and you can...