How to check PCNTL module exists - php

I write simple requirements checking script. It checks all required PHP modules installed. I need to check if pcntl is installed. But this module is accessible only in cgi environment and invisible for web queries. extension_loaded('pcntl') and function_exists('pcntl_fork') both return false. How can I perform this check?

Running php -i | grep pcntl will return the following if pcntl is enabled.
pcntl
pcntl support => enabled

If it is installed, the code bellow returns true
var_dump (extension_loaded('pcntl'));

Create a file called cli_supports.php
<?php
$supports = array();
if (function_exists("pcntl_fork")) $supports[] = "pcntl";
echo implode(",", $supports);
?>
Then from your feature detection scripts do.
$cli_features = explode(",", shell_exec("/usr/local/bin/php ".escapeshellarg(_DIR_."/cli_supports.php")));

If you're sure it's supported (or shared object exists) then check your php.ini and make sure it's loaded as an extension.
extension_loaded() should work (and what I prefer over function_exists()), and the only reason I can imagine it wouldn't is you not loading the shared object.

Related

PHP 7.1+ Windows readline extension not all functions exist

One of the improvements in PHP7.1 is that in Windows the readline extension is available out of the box. I'm having trouble using all of the functions though, as they don't all exist. The following code:
$functions = [
'readline_add_history',
'readline_callback_handler_install',
'readline_callback_handler_remove',
'readline_callback_read_char',
'readline_clear_history',
'readline_completion_function',
'readline_info',
'readline_list_history',
'readline_on_new_line',
'readline_read_history',
'readline_redisplay',
'readline_write_history',
'readline'
];
foreach($functions as $function) {
echo $function . (function_exists($function) ? ' exists' : ' does not exist') . PHP_EOL;
}
...produces the following output:
readline_add_history exists
readline_callback_handler_install does not exist
readline_callback_handler_remove does not exist
readline_callback_read_char does not exist
readline_clear_history exists
readline_completion_function exists
readline_info exists
readline_list_history does not exist
readline_on_new_line does not exist
readline_read_history exists
readline_redisplay does not exist
readline_write_history exists
readline exists
I can't find any reference in the PHP manual that only a subset of the readline extension's functions are available in Windows.
When I call php_info(), I get the following output:
readline
Readline Support enabled
Readline library WinEditLine
Is there some php.ini configuration setting (or CLI argument) that needs to be made in order to make all functions available? Alternately, is there some other way of making functions such as readline_callback_handler_install() available in Windows, or is the extension only half-baked?
Initially I thought that you might have been somehow falling through to an ancient PHP 5.0 which lacks those functions, but failing that I would have to guess that your PHP binary was compiled against an underlying library [or version thereof] that doesn't support the feature that those functions depend on.
Cross-referencing the list of functions that are missing with ext/readline/readline.c I would guess you're missing features corresponding to the constants/features HAVE_RL_CALLBACK_READ_CHAR and HAVE_LIBEDIT defined in ext/readline/config.m4.
TL;DR: Whoever compiled your PHP needs to figure it out. [probably]
For the record
PHP >= 7.4 to 8.1
yes:
readline_add_history
readline_clear_history
readline_completion_function
readline_info
readline_list_history
readline_read_history
readline_write_history
readline
no:
readline_callback_handler_install
readline_callback_handler_remove
readline_callback_read_char
readline_on_new_line
readline_redisplay
readline_list_history was added around during versions 7.2 and 7.3

Where to get SNMP class for php

I found this http://www.php.net/manual/en/class.snmp.php, but I can't find link to download this script. I tryed to search in the WEB, but the same result.
Or is the any methods with php extension to make one session and work with it?
It is in the tarball. You can download it from the official website
Take a look into it:
$ tar jtf php-5.4.13.tar.bz2 | grep snmp
php-5.4.13/ext/snmp/
php-5.4.13/ext/snmp/config.m4
php-5.4.13/ext/snmp/config.w32
php-5.4.13/ext/snmp/CREDITS
php-5.4.13/ext/snmp/php_snmp.h
php-5.4.13/ext/snmp/snmp.c
php-5.4.13/ext/snmp/snmp.dsp
php-5.4.13/ext/snmp/tests/
php-5.4.13/ext/snmp/tests/bug60749.phpt
php-5.4.13/ext/snmp/tests/bug64124.phpt
php-5.4.13/ext/snmp/tests/clean.inc
php-5.4.13/ext/snmp/tests/generic_timeout_error.phpt
php-5.4.13/ext/snmp/tests/ipv6.phpt
php-5.4.13/ext/snmp/tests/README
php-5.4.13/ext/snmp/tests/skipif.inc
php-5.4.13/ext/snmp/tests/snmp-object-errno-errstr.phpt
php-5.4.13/ext/snmp/tests/snmp-object-error.phpt
php-5.4.13/ext/snmp/tests/snmp-object-properties.phpt
php-5.4.13/ext/snmp/tests/snmp-object-setSecurity_error.phpt
php-5.4.13/ext/snmp/tests/snmp-object.phpt
php-5.4.13/ext/snmp/tests/snmp2_get.phpt
php-5.4.13/ext/snmp/tests/snmp2_getnext.phpt
php-5.4.13/ext/snmp/tests/snmp2_real_walk.phpt
php-5.4.13/ext/snmp/tests/snmp2_set-nomib.phpt
php-5.4.13/ext/snmp/tests/snmp2_set.phpt
php-5.4.13/ext/snmp/tests/snmp2_walk.phpt
php-5.4.13/ext/snmp/tests/snmp3-error.phpt
php-5.4.13/ext/snmp/tests/snmp3.phpt
php-5.4.13/ext/snmp/tests/snmp_get_quick_print.phpt
php-5.4.13/ext/snmp/tests/snmp_get_valueretrieval.phpt
php-5.4.13/ext/snmp/tests/snmp_getvalue.phpt
php-5.4.13/ext/snmp/tests/snmp_include.inc
php-5.4.13/ext/snmp/tests/snmp_read_mib.phpt
php-5.4.13/ext/snmp/tests/snmp_set_enum_print.phpt
php-5.4.13/ext/snmp/tests/snmp_set_oid_output_format.phpt
php-5.4.13/ext/snmp/tests/snmpd.conf
php-5.4.13/ext/snmp/tests/snmpget.phpt
php-5.4.13/ext/snmp/tests/snmpgetnext.phpt
php-5.4.13/ext/snmp/tests/snmprealwalk.phpt
php-5.4.13/ext/snmp/tests/snmpset-nomib.phpt
php-5.4.13/ext/snmp/tests/snmpset.phpt
php-5.4.13/ext/snmp/tests/snmpwalk.phpt
php-5.4.13/ext/snmp/tests/wrong_hostname.phpt
The "SNMP" class (introduced in PHP 5.4) is not a "script"; rather, it is an extension that is either compiled into PHP or shipped as a module that can be enabled or disabled. I suspect that either (1) you are using an older version of PHP, or (2) you do not have the SNMP extension installed or enabled.

Temporarily disable cURL for testing purposes

I am writing a class that detects whether cURL is available, does one thing if it is, and another if it isn't. I therefore need to know how to disable cURL temporarily to test this class. I do not want to change the PHP INI file. Any ideas much appreciated.
Just wondering, Im writing an alternative for if cURL is unavailble, how likely is this? Am I wasting my time. Is cURL usually available?
Curl is enabled / disabled in your php.ini. You can't enable and disable it any other way.
Open php.ini find the below and put a semi colon before it to comment it out:
extension=php_curl.dll
AFAIK there is no way to do this at run time, because modules are loaded during PHP startup, before any of you code is executed. The only way to do it is by disabling (or rather, not enabling) an extension in php.ini. You probably can't even do that with cURL, as it will probably be compiled in, not dynamically loaded.
Having said that - why not just change the check to test your "if not available" code - presumably you have a block something like this:
if (curl_exists()) { //obviously there is no such function, but you must have some condition that determines this
// Do stuff using curl
} else {
// Do something horrible
}
well, just change it to this temporarily:
if (!curl_exists()) {
// etc etc
I think the best option is to change your detection script to allow disabling it with a manual configuration.
You cannot disable function on the fly. You need to change php.ini for that.
http://www.php.net/manual/en/function.dl.php
dl — Loads a PHP extension at runtime
bool dl ( string $library )
Loads the PHP extension given by the parameter library.
Use extension_loaded() to test whether a given extension is already available or not. This works on both built-in extensions and dynamically loaded ones (either through php.ini or dl()).
Warning:
This function has been removed from some SAPI's in PHP 5.3.
<?php
// Example loading an extension based on OS
if (!extension_loaded('sqlite')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}
//this deals with sqlite but would be easy to figure out how to use it for cURL :)
?>
So you can comment out the loading of cURL extension in php.ini and then "dynamically load" it when needed.
HTH
probably the easiest way is by open file curl.ini, Im use ubuntu 12.04 and file located at
/etc/php5/apache2/conf.d/curl.ini
leave a comment by adding semicolon before extension=curl.so
You can see the location of curl.ini through phpinfo ();
dont forget to restart the Apache
sudo service apache2 restart
Curl is available as long its extension is loaded (which is mostly by default).
You can check what curl extension provides by the following command:
php --re curl
which gives you list of functions, classes and its methods.
To temporary disable curl extension, you can run PHP with -n to simply ignore your php.ini, for example:
$ php -n -r "print_r(curl_version());"
Fatal error: Call to undefined function curl_version() in Command line code on line 1
Here is working example:
$ php -r "print_r(curl_version());"
Array
(
[version_number] => 463623
...

How do I enable XSLT functions in PHP 5?

I'm wanting to print an xml file out as an HTML nested list using xslt, and as far as I know the code is correct, however I'm getting this error
Fatal error: Call to undefined function xslt_create()
Which I presume means the xslt functions havn't been enabled. How do I enable these functions within PHP? Is there a php file I need to include (like the way Javascript libraries work) or is it something more complicated? I'm hosted with MediaTemple.
Here's the php code I'm using:
<?php
// Allocate a new XSLT processor
$xh = xslt_create();
// Process the document, returning the result into the $result variable
$result = xslt_process($xh, 'armstrong.xml', 'familyToNestedList.xsl');
if ($result) {
print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
print " variable, the \$result variable has the following contents\n<br>\n";
print "<pre>\n";
print $result;
print "</pre>\n";
}
else {
print "Sorry, sample.xml could not be transformed by sample.xsl into";
print " the \$result variable the reason is that " . xslt_error($xh) .
print " and the error code is " . xslt_errno($xh);
}
xslt_free($xh);
?>
Thanks in advance!
Depending on your distro of linux, you may be able to just install the php5-xsl module, add the line "extension=php_xsl.so" to your php.ini file, and restart your apache2 server.
This worked for me on Ubuntu 11.10. You can input "sudo apt-get install php5-xsl" into the command line to install php5-xml.
You have to compile PHP with the support for it and make sure to have all the required dependencies. Please see the corresponding chapters in the PHP Manual for XSLT.
From chapter Requirements
This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default. This extension uses Sablotron and expat, which can both be found at » http://freshmeat.net/projects/sablotron/. Binaries are provided as well as source. Enable by using the --with-xslt option with PHP 4.
From chapter Installation
On Unix, run configure with the --enable-xslt --with-xslt-sablot options. The Sablotron library should be installed somewhere your compiler can find it. Make sure you have the same libraries linked to the Sablotron library as those, which are linked with PHP. The configuration options: --with-expat-dir=DIR --with-iconv-dir=DIR are there to help you specify them. When asking for support, always mention these directives, and whether there are other versions of those libraries installed on your system somewhere. Naturally, provide all the version numbers.
The recommended extension for using XSL transformations with PHP5 is XSL. If all you need to do is transform two documents, as show in your example, consider this example from the PHP Manual:
$xml = new DOMDocument;
$xml->load('collection.xml');
$xsl = new DOMDocument;
$xsl->load('collection.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
The transformToXML method will return the transformed document or FALSE, so you can keep the if/else from your code. In any case, it should be trivial to upgrade your code.
Well, is very simple, but on php.net its could be explained more explicitly.
I'm using one docker container contain ubuntu base and using php-fpm.
The steps to install this extension in my context were:
First search xsl extension on linux repository
apt-cache search xsl
I ended up finding the php5-xsl, so it was only install
apt-get install php5-xsl
that the installation process the setup configuration is already added, if does not happen, just make yourself
vim /etc/php5/mods-available/xsl.ini
insert this content:
extension=xsl.so
(obviously the paths are according to your php configuration settings, but my example is the default configuration)
Restart you php fpm and done!

Alternatives to apache_get_modules() to get list of loaded apache modules

I'm trying to use php to get the list of loaded apache modules using apache_get_modules(), but I get an error that this function is undefined.
From searching it seems the problem is that
this only works when PHP is installed
as an Apache module. This will not
function when using PHP as CGI (ex:
suPHP)
I'm not sure if this is the case, but I'm on shared hosting. Any ideas how to find out the list of loaded apache modules, preferably with php, but I'm open to suggestions.
phpinfo() will tell you how PHP is installed, especially the Server API row.
You could parse the config files for Apache to find out which modules are configured.
You could run something like apache2 -t -D DUMP_MODULES to obtain a list of modules.
apache_get_modules() is only available when the PHP is installed as a module and not as a CGI. This is the reason why you cannot use this function and suffering from the error . The other reason would be caused by the disable_functions directive in your php.ini . you can use the following written php code to check whether rewrite module is enabled or not in your case while using CGI server API
<?php
if (is_mod_rewrite_enabled()) {
print "The apache module mod_rewrite is enabled.<br/>\n";
} else {
print "The apache module mod_rewrite is NOT enabled.<br/>\n";
}
/**
* Verifies if the mod_rewrite module is enabled
*
* #return boolean True if the module is enabled.
*/
function is_mod_rewrite_enabled() {
if ($_SERVER['HTTP_MOD_REWRITE'] == 'On') {
return TRUE;
} else {
return FALSE;
}
}
?>
To Check whether you are running Php Under CGI or Apache you can use procedure below
Create a check_phpinfo.php file and Write PHP Code Written Below -
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
Then Save the file and Navigate to Url like - www.example.com/check_phpinfo.php and It will show you php file in server
In Four line you can see "Server API CGI/FastCGI" That Denotes you are using PHP under CGI / Fast CGI
<?php
// Print Apache Modules
print_r(apache_get_modules());
?>

Categories