Cara menggunakan php 5.6 deprecated

Pada seri tulisan kali ini saya akan berbagi tools yang saya gunakan untuk memeriksa aplikasi-aplikasi berbasis PHP yang saya kembangkan, apakah kompatibel dengan PHP 7 atau tidak. Tentunya jika dilakukan secara manual, akan menguras tenaga sangat banyak.

Kita harus mengerti apa saja fitur yang deprecated, yang tidak didukung, dihapus dll di PHP 7 sekaligus memeriksa baris demi baris di aplikasi berbasis PHP yang kita kembangkan.

Tool yang saya gunakan adalah PHP Code Fixer dari om Wap Morgan dari Russia. Bisa diliat-liat disini: https://github.com/wapmorgan/PhpCodeFixer. Hebatnya, tidak hanya PHP 7 yang didukung oleh Class ini, tapi juga PHP 5.x.

Instalasi

untuk instalasi dari PHP Code Fixer sendiri, sangat mudah jika dilakukan dengan Composer. (Untuk apa itu composer dan cara installnya silakan baca disini).

[sourcecode]
composer require wapmorgan/php-code-fixer
[/sourcecode]

Kemudian lakukan composer update:
[sourcecode]
composer update
[/sourcecode]

atau kalau malas menggunakan composer, silakan download repository-nya sebagai file Zip di https://github.com/wapmorgan/PhpCodeFixer/archive/master.zip

Penggunaan

Dari dokumentasinya didapatkan penjelasan sbb:
[sourcecode]
Usage: phpcf [–target VERSION] [–max-size SIZE] FILES…

Options:
-t, –target VERSION target php version [default: 7.1]
-s, –max-size SIZE sets max size of php file. If size of file is above this value, file will be skipped [default: 1mb]
[/sourcecode]

dengan parameter target (versi PHP yang dituju) dan size (ukuran file PHP).

Versi PHP yang didukung saat ini:

  • PHP versi 7.1
  • PHP versi 7.0
  • PHP versi 5.6
  • PHP versi 5.5
  • PHP versi 5.4
  • PHP versi 5.3

Contohnya:
[sourcecode]
php bin/phpcf –target 7.1 /path/ke/folder/sisteminformasi
[/sourcecode]

Contoh hasil:

~/tools/vendor/bin$ php phpcf --target 7.1 /home/subdomain/psbonlinefree/
Max file size set to: 1.000 MiB
Scanning /home/subdomain/psbonlinefree/ ...
 PHP |             Type |                      File:Line | Issue
 5.5 | function         | ...onlinefree//modul/10.php:34 | Function mysql_query is deprecated.
 5.5 | function         | ...onlinefree//modul/10.php:50 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...onlinefree//modul/11.php:30 | Function mysql_query is deprecated.
 5.5 | function         | ...onlinefree//modul/11.php:31 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...onlinefree//modul/12.php:38 | Function mysql_query is deprecated.
 5.5 | function         | ...onlinefree//modul/12.php:40 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...onlinefree//modul/14.php:31 | Function mysql_query is deprecated.
 5.5 | function         | ...onlinefree//modul/14.php:47 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...onlinefree//modul/15.php:33 | Function mysql_query is deprecated.
 5.5 | function         | ...bonlinefree//modul/2.php:41 | Function mysql_query is deprecated.
 5.5 | function         | ...bonlinefree//modul/2.php:43 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...bonlinefree//modul/3.php:70 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...bonlinefree//modul/3.php:70 | Function mysql_query is deprecated.
 5.5 | function         | ...onlinefree//modul/3.php:118 | Function mysql_query is deprecated.
 5.5 | function         | ...bonlinefree//modul/5.php:44 | Function mysql_query is deprecated.
 5.5 | function         | ...bonlinefree//modul/5.php:45 | Function mysql_num_rows is deprecated.
 5.5 | function         | ...bonlinefree//modul/5.php:48 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...bonlinefree//modul/8.php:31 | Function mysql_query is deprecated.
 5.5 | function         | ...bonlinefree//modul/8.php:47 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...bonlinefree//modul/9.php:33 | Function mysql_query is deprecated.
 5.5 | function         | ...nefree//modul/fungsi.php:51 | Function mysql_fetch_array is deprecated.
 5.5 | function         | ...nefree//modul/fungsi.php:51 | Function mysql_query is deprecated.
 5.5 | function         | ...e//modul/konfigurasi.php:48 | Function mysql_connect is deprecated.
 5.5 | function         | ...e//modul/konfigurasi.php:51 | Function mysql_select_db is deprecated.

Replace Suggestions:
1. Don't use function mysql_connect. Instead use mysqli::__construct
Peak memory usage: 1.536 MB

Disana ada banya fungsi deprecated di PHP 7.1, jika ingin tetap dimigrasi, ada pula saran yang diberikan.

If some of your virtual servers are using deprecated versions of PHP, a warning will appear on the My Zones Overview page.

 

Cara menggunakan php 5.6 deprecated

 

Clicking the View button at the top of the page will take you to an overview page showing the deprecated PHP hosts (main and subdomains) on the server.

As PHP version changes can cause error messages to appear on the website and version changes can take up to 3 minutes, it is recommended to test the functionality of the website before making a change.

In order to test, click on the desired address in the list:

 

Cara menggunakan php 5.6 deprecated

 

A test page will open with a selection of PHP versions in the header. Choosing between them will show you the result with different PHP versions. For example:

PHP 5.6:

Cara menggunakan php 5.6 deprecated

PHP 8.1:

Cara menggunakan php 5.6 deprecated

 

The minimum version of PHP that Zone allows you to choose is PHP 5.6, but be aware that it is also deprecated – it no longer has any developer patches for security vulnerabilities, although it can still technically be supported. It is recommended to upgrade to PHP 8.0, as PHP 5.6 is also outdated and no longer receiving security updates.

Modern web applications like WordPress supports latest PHP versions and upgrading to PHP 8.0 or 8.1 is suggested after updating WordPress plugins and themes.

Upgrading non-updated web applications to to PHP 8.x can be tricky as the differences between 5.6 and 7.x are large.

Known upgrade issues

Umlauts replaced with question marks

Cara menggunakan php 5.6 deprecated

 

Default PHP 5.6 configuration adds UTF-8 encoding to HTTP response header and older websites that use other encodings in website text or meta may need to remove this header.

Easiest solution is adding

Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
0 with following setting to webservers root folder
Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
1:

default_charset = ''

Error message Fatal error […]

The website is not displayed and there is a message at the top of the page, for example:

Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]

This is a call to a function that has been removed from the PHP language, or that cannot be registered due to an update problem.

The exception is

Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
2 which has been removed due to a change in PHP syntax.

In order to update the application to a newer version of PHP, the exact cause of the error has to be determined and the code has to be fixed.

Error message Fatal error: Incompatible file format

The website is not displayed and an error message appears at the top of the page:

Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0

May occur with applications encrypted/licensed with Zend Guard, especially Saurus. PHP to version 5.6 is only possible by replacing and updating the application code (Saurus paid support is provided by Bonefarm). The web can be converted to static HTML, or migrated to modern content management like WordPress.

Error message: Cannot modify header information […]

This warning is displayed if other warnings or error messages are generated during page generation, which cannot be used to send HTTP request headers:

Warning: Cannot modify header information -
headers already sent by (output started at [...]wp-db.php:57)
in [...]

It has no independent meaning, can be ignored.

Error message Warning […]

When you open a webpage, a message appears at the top of the page or in one of its components:

Warning: Creating default object from empty value in [...]
Warning: file_put_contents(): Filename cannot be empty in [...]
Warning: get_class() expects parameter 1 to be object, string given in [...]
Warning: getimagesize(): Filename cannot be empty in [...]
Warning: Illegal string offset 'face' in [...]
Warning: Invalid argument supplied for foreach() in [...]

The parameter type or value of some functions does not match the expected one, which means that some of the functionalities of a web application may work differently than intended (mostly by simply not delivering the desired content).

If the functionality is not important you can ignore and disable

Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
3 messages by adding a 
Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
0 file to your web root directory
Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
1 with the with following settings:

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED & ~E_WARNING

Zone default setting is:

Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
6

Error message Deprecated […]

When you open a website, a message appears at the top of the page or in one of its components:

Deprecated: Assigning the return value of new by reference is deprecated in [...]
Deprecated: Function set_magic_quotes_runtime() is deprecated in [...]
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in [...]
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in [...]

The referenced function, parameter or syntax will disappear in subsequent versions of PHP, but this will not affect the operation of the selected version.

In Zone Virtual Servers, the output of

Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
7 messages is disabled by default, most likely due to a configuration change in the application code, less likely via global
Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
8 or
Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
0 (see the example in the
Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
3 message).

A command line search can be used to locate the setup location:

grep -r E_ALL .

For example, in the case of OpenCart 1.5, this will result in the following files:

./vqmod/vqcache/vq2-system_startup.php:error_reporting(E_ALL);
./system/startup.php:error_reporting(E_ALL);
./php.ini:;error_reporting = E_ALL;

The PHP code should be changed to

Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
1 and
Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
2 in files
Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
6

Database connection fails

If, after changing PHP to version 5.6+, the connection to the database fails, check that the database user is not using the old 6-digit password (the old cryptographic password).

The solution is to change the password of the database user. The new password will also work retroactively with PHP versions older than 5.6.

If navigation between pages does not work, or the requested action does not occur after submitting a form, this may be due to the assumption that the super-global GET, POST, Cookie (EGPCS) arrays are registered as normal variables.

This PHP feature was deprecated in PHP 5.3.0 and removed in PHP 5.4.0.
For details, see the PHP documentation:  Using Register Globals, Migrating from PHP 5.3.x to PHP 5.4.x.

For example, a web page menu has links in the query string of the form

Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
4 and in the PHP file that contains the page load logic, the 
Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
5 variable is used instead of the super-global
Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
6 array.
Similarly for form submission, where the form element name
Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
7 is not checked against the super-global
Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
8 array, but directly against the
Fatal error: Incompatible file format: The encoded file has
format major ID 1, whereas the Loader expects 7
in /data[...]/htdocs/index.php on line 0
9 variable.

How to test which PHP version is in use?

Changing PHP version in server takes up to 3 minutes. To verify which version is currently in use add a .php file with random name to your websites’s root folder

Fatal error: Call to undefined function session_register() in [...]
Fatal error: Call to undefined function sqlite_open() in [...]
Fatal error: Call to undefined function wp_script_add_data() in [...]
Fatal error: Call-time pass-by-reference has been removed in [...]
1, containing:

<?php
phpinfo();

You can view the output by visiting

Warning: Cannot modify header information -
headers already sent by (output started at [...]wp-db.php:57)
in [...]
1 (for security reasons we recommend deleting this file).