Archive

Archive for the ‘php’ Category

installing EasyPHP

June 27, 2016 Leave a comment

Problem
I wanted to contribute to a PHP project but under Manjaro I couldn’t test it locally. I got a “Fatal error: Call to undefined function curl_init()” message that I didn’t manage to resolve, though the line “extension=curl.so” was present in my php.ini :(

Solution
After a few hours of trial and error, I decided to develop this project under Windows. I have Windows in VirtualBox, I put the PHP project in a shared folder, so my idea was to edit the source under Linux and visualize the result under Windows.

I chose EasyPHP and installed the latest EasyPHP Devserver that provides a complete development environment.

When I opened the project I got the same error since curl was not enabled by default. On the dashboard I could edit the php.ini file and uncommented the line “extension=php_curl.dll“. However, after restarting the webserver I got another error: libssh2 is missing. I found the solution here (ken’s comment):

"I had to also also copy libssh2.dll into my Apache24 folder 
for this to work with my PHP 5.6.2 installation. So altogether 
I had to do the following:

Move to Windows\system32 folder:
libssh2.dll, php_curl.dll, ssleay32.dll, libeay32.dll

Move to Apache24\bin folder
libssh2.dll

Uncomment extension=php_curl.dll"

I found all these files in the install folder of EasyPHP. There are two versions of each, one in a “…vc11…” folder, while the other one in a “…vc14…” folder. I worked under PHP 5.6 so I copied the vc11 versions of each file mentioned above.

After this Apache restarted without any error.

Categories: php, windows Tags: , , ,

Convert HTML to PDF

February 17, 2011 4 comments

In this thread you will find a list of “HTML to PDF” converters, e.g.:

WKhtmlToPDF

Simple shell utility to convert html to pdf using the webkit rendering engine, and qt.

You can get its binary executable here. Then, you can convert an html to pdf like this:

wkhtmltopdf-i386  test.html  test.pdf

The result will be similar to exporting the page in PDF in a browser. This is not a surprise, wkhtmltopdf contains the WebKit rendering engine.

Command line scripting in PHP

February 17, 2011 Leave a comment

When talking about command line scripting, most people think of Python or Perl. But it’s good to know that you can do something similar with PHP too!

Example (hello.php):

#!/usr/bin/php
<?php
   # /etc/php5/cli/php.ini
   # The configuration file for the CLI version of PHP.

   print "Hello, World!\n";
?>

You can execute it with “./hello.php” or “php hello.php“.

Notice that the command line version of PHP uses a different php.ini file.

Consult this page to learn more.

Categories: php Tags:

phpMyAdmin: No activity within 1440 seconds; please log in again

February 10, 2011 12 comments

Problem

phpMyAdmin’s “No activity within 1440 seconds; please log in again” message is slowly but definitely driving you crazy. How to remain sane?

Solution

Increase the time limit. Open the file /etc/phpmyadmin/config.inc.php and add the following line to its end:

$cfg['LoginCookieValidity'] = 60 * 60 * 8;  // in seconds (8 hours)

Here I set 8 hours, but you can change that.

When you log in again in phpMyAdmin, this new value will be taken into account.

Categories: php Tags: , , ,

Embed JpGraph in web pages

January 30, 2011 7 comments

Problem

In PHP, you use JpGraph to produce graphs. You would like to include (embed) such a graph in a web page but you get the following error: “The image cannot be displayed, because it contains errors.

Explanation

Let’s see the following basic example (example0.php):

<?php // content="text/plain; charset=utf-8"
require_once ('lib/jpgraph/src/jpgraph.php');
require_once ('lib/jpgraph/src/jpgraph_line.php');

// Some data
$ydata = array(11,3,8,12,5,1,9,13,5,7);

// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');

// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');

// Add the plot to the graph
$graph->Add($lineplot);

// Display the graph
$graph->Stroke();
?>

Actually, this code will produce a PNG file, thus it will set the header to ‘Content-type: image/png‘. Once the header is set, it cannot be changed. Now let’s see what happens if you try to mix it with HTML:

<p>
Beginning of the HTML page. Normal text.
</p>
<?php
   // the previous code of example0.php, I won't repeat that
?>

Since the page starts with normal HTML, the header will be set to ‘Content-type: text/html‘. Then the JpGraph PHP block will try to set ‘Content-type: image/png‘, so there is a conflict.

Solution #1

The easy way is to make a PHP file that produces a graph and include this file as if it were a normal image:

<p>
This is normal HTML text.
</p>
<img src="example0.php">

What happens if you want to pass some data to the JpGraph PHP file, regarding how to produce the graph? If the data are simple, you could pass them via GET parameters:

This is normal HTML text.
<img src="example0.php?label1=...&label2=...&data1=...&data2=...">

However, if the data is complicated, it would be better to include JpGraph directly in your PHP. This way, JpGraph has access to the necessary data structures from which it can construct the graph. I will explain it in the next section.

Solution #2

Let’s see how to include JpGraph directly in our PHP script:

<p>
Normal HTML at the beginning of the page.
</p>
<?php
   // produce $ydata ...
?>
<?php
   define('PREFIX_DIR', 'tmp.jpgraph');   // images will be created here
   define('PREFIX', 'jpgraph');   // prefix for the images, can be anything
   define('TIME_LIMIT', 3 * 60);    // 3 minutes
   $tmpfname = tempnam(PREFIX_DIR, PREFIX);   // create temp filename
?>
<?php // content="text/plain; charset=utf-8"
require_once ('lib/jpgraph/src/jpgraph.php');
require_once ('lib/jpgraph/src/jpgraph_line.php');

// Some data
$ydata = array(11,3,8,12,5,1,9,13,5,7);

// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');

// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');

// Add the plot to the graph
$graph->Add($lineplot);

// Display the graph
$graph->Stroke($tmpfname);
clean_old_tmp_files();
?>
<?php
   ?><img src="<?php echo get_relative_path($tmpfname); ?>"><?php
   #unlink($tmpfname);   // wouldn't work correctly
?>
<?php
   function get_relative_path($filename) {
      return preg_replace("/^.*\/(".PREFIX_DIR."\/.*)/", "$1", $filename);
   }

   function clean_old_tmp_files()
   {
      foreach (glob(PREFIX_DIR.'/'.PREFIX."*") as $filename)
      {
         #printf ( "Name: %s; age: %d<br>\n", $filename, time() - filemtime($filename) );

         $age = time() - filemtime($filename);
         if ($age > TIME_LIMIT) {
            unlink($filename);
         }
      }
   }
?>

As you can see, at the beginning of the page we show some HTML text that is followed by the graph. The data of the graph is produced by our PHP, and JpGraph’s PHP has direct access to it, thus no parameter passing is necessary.

The idea is the following: the graph is written to the file system. Then, this file is shown using the <img src="..."> tag. A time limit is set (here 3 minutes). When the script is called again, it’ll automatically remove images that are older than the limit.

Unfortunately, the image cannot be deleted right after the <img src="..."> tag. When your browser sees this tag, it’ll launch a thread to fetch the image and steps on. That is, the image would be deleted immediately after the creation, and the browser couldn’t show it. So the image file must be left there. My solution is to set a time limit, and when the PHP page is called again, it’ll remove the old images. This way you’ll always have some temporary images in the temp folder, but not many.

Preparation:

I suppose you have your PHP script in your ~/public_html directory. Here create a temporary directory for the images called tmp.jpgraph and set its permission to 777 (chmod 777 tmp.jpgraph).

Categories: php Tags:

Drawing graphs in PHP with JpGraph

January 28, 2011 19 comments

JpGraph is an Object-Oriented Graph creating library for PHP >= 5.1 The library is completely written in PHP and ready to be used in any PHP scripts (both CGI/APXS/CLI versions of PHP are supported).

If you want to draw graphs in PHP, JpGraph is a good choice. Very easy to use yet powerful.

Installation

Here I suppose you have a public_html directory in your HOME and your PHP scripts are in public_html.

Download the latest jpgraph archive, put it in ~/public_html/lib and extract it. Now you will have a directory like ~/public_html/lib/jpgraph-3.5.0b1. Put a symbolic link on it with a simpler name:

cd ~/public_html/lib
ln -s jpgraph-3.5.0b1 jpgraph

Verify if the directory jpgraph has the correct permissions. Directories must be 755, while files must be 644. To correct permissions, you can use this script:

#!/bin/bash

# setjog_here.sh
#
# place it in ~/public_html/lib/jpgraph and launch it
# it'll set the permissions recursively

find . -type d -print0 | xargs -0 chmod 755
find . -type f -print0 | xargs -0 chmod 644
chmod 755 .
chmod u+x $0

Test an example

Copy ~/public_html/lib/jpgraph/src/Examples/example0.php to ~/public_html. Modify the include lines:

# original:
#require_once ('jpgraph/jpgraph.php');
#require_once ('jpgraph/jpgraph_line.php');

# new:
require_once ('lib/jpgraph/src/jpgraph.php');
require_once ('lib/jpgraph/src/jpgraph_line.php');

Now call this file from your browser. The URL must be similar to this: http://localhost/~jabba/example0.php.

Troubleshooting

You might get an error saying “The function imageantialias() is not available in your PHP installation. Use the GD version that comes with PHP and not the standalone version.

Don’t worry. Open the file ~/public_html/lib/jpgraph/src/gd_image.inc.php, find the function SetAntiAliasing and comment this line out like this:

// JpGraphError::RaiseL(25128);//('The function imageantialias() is not available in your PHP installation. Use the GD version that comes with PHP and not the standalone version.')

Update (20110215): Comment out just this line, not the whole function :)

This is a quick fix; we disabled antialiasing.

This subsection was updated, the original text was not clear. Thanks to Jon R. for pointing that out.

Categories: php Tags: , , , ,

Multiple inserts with MySQL/PHP

January 13, 2011 Leave a comment

Problem

You want to insert multiple records in a MySQL table (in a loop, for instance). How to improve the performance?

Solution #1

One way is to regroup all the inserts in one SQL command:

INSERT INTO x (a,b)
VALUES
 ('1', 'one'),
 ('2', 'two'),
 ('3', 'three')

Credits go here.

Solution #2

Another way is to regroup the inserts in a transaction:

mysql_query("START TRANSACTION");
mysql_query("INSERT ...");
mysql_query("INSERT ...");
mysql_query("INSERT ...");
mysql_query("COMMIT");   // or "ROLLBACK" if you changed your mind

Credits go here.

Categories: mysql, php Tags: , ,
Follow

Get every new post delivered to your Inbox.

Join 91 other followers