Load php extensions dynamically - php

I have a live server which I want to occasionally use for testing purposes. I only have access to FTP and some basic administration tools there.
Reading the documentation for dl() gives me hope I can load xDebug dynamically even though I can't add it to the loaded extension list. I have little idea how though.
Question: How to obtain the appropriate compiled version of xdebug (or any other PHP extension) which would be ready to be used with dl()?
BTW, AFAIK the OS is CentOS 4 in my case, but I'd appreciate a broader answer too - for future reference.

xdebug is a zend-engine extension and thus cant be loaded dynamically.
You can try with xhprof instead. That should be possible to load at run time (I haven't much experience with it though, so i cant offer you specifics)

I usually use php_uname to determine the server OS
function os_check() {
$os_string = php_uname('s');
if (strpos(strtoupper($os_string), 'WIN')!==false) {
return 'windows';
} else {
return 'linux';
}

Such information is in various places in phpInfo()
<?php
phpinfo();
?>

Related

How to get pthreads working in PHP?

I am using wampserver to test & run wordpress code in my local computer. In order to run pthread, I have followed the following steps:
1) I got the pthread zip file from http://windows.php.net/downloads/pecl/releases/pthreads/0.44/
(My machine has php 5.3.13 and downloaded the php_pthreads-0.44-5.3-ts-vc9-x86.zip file from the above link).
2) Extracted the zip file. Moved the php_pthreads.dll to the C:\wamp\bin\php\php5.3.13\ext directory.
3) Moved pthreadVC2.dll to the C:\wamp\bin\php\php5.3.13 directory.
4) Then Opened C:\wamp\bin\php\php5.3.13\php.ini and added the code extension=php_pthreads.dll at the begining of the file.
But when I try to run the following code:
<?php
class My extends Thread {
public function run() {
printf("%s is Thread #%lu\n", __CLASS__, $this->getThreadId());
}
}
$my = new My();
$my->start();
?>
It gives me the following error:
Fatal error: Class 'Thread' not found in C:\wamp\www\wp-admin\includes\post.php on line 2
Can you please tell me how to install pthreads in my computer to use with php? and do I have to install any other software?
I've noticed that wampserver has php.ini in two separate places. One place is in the /wamp/bin/php/php5... directory, and the other place is in the /wamp/bin/apache/apache.../bin directory (where "..." represents version numbers). The two files need to be identical, because apparently both are loaded at different times by the overall wampserver boot-up procedure.
(Note I only discovered this recently, and may be well "behind the curve" of doing fancy things with wampserver --maybe everyone else has been dealing with both files for a long time. So I don't know if this simple thing will fix your problem; I came here looking for info, myself, regarding doing some multi-threading stuff. :)
One other thing. According to this page: www.php.net/manual/en/pthreads.requirements.php
PHP has to be compiled with "--enable-zts" in order for pthreads stuff to work. I have not been able to find any evidence that the PHP part of wampserver was compiled that way.
(months later)
Having decided I didn't really immediately need to do any threading stuff, I went on to do other things, until the need actually arose. I now can say that the version of PHP compiled into WampServer does support the "pthread" extension, although some set-up work is needed, first. The instructions I saw mentioned putting a couple of .dll files (after a download and unZip) into certain places, but that didn't work for me. Copying them to the \Windows\System32 directory did work. (Putting them into the \apache...\bin directory also works; there are some other php .dll files in there.)
After that, much like what you did, it is necessary to define a "class" that extends the "Thread" class, in order to actually do something in another thread. The "run()" function in the Thread class is "abstract", and needs to be "realized" as an actual function in the extended class. Then the "new" operator can create an "instance", an object of that specified class, for actual use. Here's the class that I needed:
//Purpose: Use another thread to run the code in another php file, after a delay
class xT extends Thread
{ var $fil, $tim;
function savWhatWhen($f="", $t=0)
{ $this->fil = $f; //save What, file to process
$this->tim = $t; //save When, delay before processing file
return;
}
function run()
{ ini_set('max_execution_time', 600); //600 seconds = 10 minutes
sleep($this->tim); //do a delay; beware of max-exec-time!
include($this->fil); //load file-to-process, and process it
return;
} }
That "savWhatWhen()" function was created specifically for this extension of the basic Thread class. Here's some code for using that class:
$TH = new xT(); //prepare a separate Thread
$TH->savWhatWhen("d:/wamp/myscripts/test.php", 45);//file-name and delay time
$TH->start(); //after delay, process file
//the code that does this can terminate, while OTHER thread is doing a delay
Note for anyone copying this code, you might need to make sure your "open_basedir" setting in the php.ini allows access to the specified file.
More months later: With lots of things being worked on, I haven't put a lot of time into using my pthread object. I did encounter a peculiarity that makes me wonder about whether or not I can actually use pthreads the way I had hoped. Here is what I have observed:
1. An initial php file is called by AJAX, to do something.
2. The PHP processor on the Web Server does that thing.
3. Various data is supposed to be echoed to the browser.
4. The initial php file calls for the creation of another thread, and terminates.
5. The browser does not yet receive the echoed data!
6. The PHP processor on the Web Server does the work delegated to the second thread.
7. When the second thread terminates, NOW the browser receives the echoed data!
At this writing I'm thinking I missed something. Perhaps I need to do some forceful "flush" stuff when the first thread ends, so that the browser can receive the echoed data and the user can do things while the PHP processor on the server is also doing things.
Check for extension_dir = "ext" in you php.ini file. Make sure it points to the folder where your extensions reside and make sure it's not commented (it has a semicolon ; in front of it)
You have to add a require_once() with the path of the Thread class before extending it (if your framework don't use an autoload class system)
I've encountered the same problem, in my case placing the pthreadVC2.dll in
..wamp\bin\apache\Apache2.4.4\bin
(instead of ..\wamp\bin\php\php5.4.16 as the guide in php.net instructs) solved the problem
Wamp server has a separate php.ini config file for the browser and for the cli.
To use the pthreads module in the browser with WAMP Server you need to copy the "pthreadVC2.dll" into the apache "bin" directory also.
You should now have he "pthreadVC2.dll" in both of these folders (if installed in default location):
C:\wamp\bin\php\php[x.x.xx]\bin
C:\wamp\bin\apache\apache[x.x.x]\bin
You will also need to update the php.ini file within the php bin directory AND the apache bin directory to include:
extension=php_pthreads.dll
This now means you can use pthreads in the browser and in the cli with wamp server
After encountering the same problem, I noticed that I have installed the wrong Pthread version (3.1.6 : requires PHP7+) which wasn't compatible with my PHP version (5.5.12). Solved the problem with Pthread version 0.0.44. An earlier version should probably work well.
Here is the download page for Pthread and the installation page. Be careful about the both php.ini location as mentioned above (Apache folder=for Browser, PHP folder=CLI).

PHP Phar created with Phar::createDefaultStub running as CLI in browser

I have an application I want to package as a PHAR with both CLI and web interfaces (it acts as an API to wider system so I'd like to avoid reproducing the same libraries across two PHAR archives). The 'bug' im encountering is that the PHAR always acts like it's run from the command line when created with createDefaultStub.
<?php
try {
$phar = new Phar('myphar.phar');
$phar['cli.php'] = '<?php echo "CLI"; ?>';
$phar['web.php'] = '<?php echo "Web"; ?>';
$phar->setDefaultStub('cli.php', 'web.php');
// this is the same as:
// $phar->setStub($phar->createDefaultStub('cli.php', 'web/index.php'));
} catch (Exception $e) {
// handle errors
}
?>
If I run the phar created from the above code which was taken directly from (http://www.php.net/manual/en/phar.createdefaultstub.php from the command line with
php.exe myphar.phar
I see, "CLI" as expected, but I also see this from the web also.
There is a documented bug from 2010, but since it appears unsolved and not many cases available I'm wondering if its something I'm doing wrong at my end.
PHP Bug Report: https://bugs.php.net/bug.php?id=52322&edit=2
Things I've done to try and diagnose:
Tested on PHP 5.4.11, 5.3.21, 5.2.17
The phar was recreated under each version also.
Tried on a clean install of the latest XAMPP.
PHP.ini settings
detect_unicode = Off
phar.readonly = Off
phar.require_hash = Off
Debugged with echo from cli.php of:
$_SERVER['REQUEST_URI']
$_SERVER['REQUEST_METHOD']
Both return as expected, these are the values used in the default stub to check if it's running on the web.
I really am at a loss here and wondered if anybody had ideas about where to turn next?
Writing a custom stub is an option, but likely something is going wrong behind the scenes that will trip me up later on.
Any advice would be greatly appreciated :)

PHP4 supports DOMDocument? My code says yes?

When I check phpinfo(), I see /usr/local/php4/lib/php.ini
That means PHP4, right?
But When I execute the line of code below, it returns PHP5. WTF? I thought PHP4 did not have the DOMDocument class. I need to test for PHP4 and do a workaround, but this specific test is confusing me. Is there another more foolproof way to check for PHP4 in script?
if (is_admin() && class_exists('DOMDocument')) {
echo "PHP5";
} else {
echo "Awe PHP4";
}
The version of your PHP is listed right on top of any phpinfo() output. You can also determine it by echoing phpversion() or from CLI with php -v.
Apart from that, no. PHP4 does not support DOMDocument (at least not the one you are refering to). The old DOM XML extension has a similar named class though.
Try the following:
echo extension_loaded('domxml') ? 'old' : 'new';
phpinfo() is a cool function :)
edit, lots of similar functions here
http://www.php.net/manual/en/ref.info.php
you seem like you'd be interested in
phpversion() and version_compare()
The correct way to check your PHP version from phpinfo() is to read the header at the very top, but when doing checks in PHP code, checking php_version() is the way to do. There is also a predefined constant PHP_VERSION.
Well if phpinfo says you run php5 you are running php5. assuming by php_info you mean phpinfo.
you may wanna check the function phpversion
The path you are referring to is just the configured config file which could be anything.
You probably updated and kept it ages ago.

Temp-Files in Symfony-Cache folder

I'm using Windows on some production machines (IIS with FastCGI-PHP). Since the update of one SF-Project to 1.3.x I notice some strange problems. The Server is "collecting" Temp-Files in the config-cache folder of the applications. They are named like con1718.tmp and always containing the autoload-config-cache. The tmp-files are not generated for every request but I have 1 or 2 new files every half an hour or so. If the application is running some days/months there are a lot of these Temp-Files (Megabytes of them).
Machine-Details: - Windows Server 2008 - IIS 7 - ZendServer? with PHP 5.2.11
Project with SF 1.3.3
Any ideas what the problem can be?
I checked out symfony 1.4 and saw this in the code:
// Hack from Agavi (http://trac.agavi.org/changeset/3979)
// With php < 5.2.6 on win32, renaming to an already existing file doesn't work, but copy does,
// so we simply assume that when rename() fails that we are on win32 and try to use copy()
if (!#rename($tmpFile, $cache))
{
if (copy($tmpFile, $cache))
{
unlink($tmpFile);
}
}
This piece of code should be in sfConfigCache.php on line 354, could you check you have this lines? If not, consider updating, or patching, and if yes, you could log $tmpFile before unlinking it, just to see if there is an attempt to unlink these files or not.
To add more information log, you should try this code instead:
if (!#rename($tmpFile, $cache))
{
sfContext::getInstance()->getLogger()->info('attempt to renaming ' . $tmpFile . ' failed, trying copy');
if (copy($tmpFile, $cache))
{
sfContext::getInstance()->getLogger()->info('copy successful, now unlinking ' . $tmpFile);
unlink($tmpFile);
}
else
{
sfContext::getInstance()->getLogger()->err('probem with copy for file '.$tmpFile);
}
}
'con1718.tmp' looks like a temporary file name generated with tempnam PHP function:
tempnam('c:/tmp', 'con'); // produces something like c:\\tmp\con1234.tmp
I used grep on symfony sources to find such calls but didn't find any tempnam() usage with 'con'. Maybe it's one of the plugins you're using?
Any chance you're running the Windows Cache Extension for PHP (Windows Cache Extension 1.1 for PHP 5.2 in Web Platform Installer 2.0)? I've noticed that while this package tries to ape the behaviour of APC PHP Accelerator that is widely used, it does consume a lot of resources and do some odd things, especially file writes in odd places. I've yet to mash symfony and it together, but will be doing so in the next few weeks. Otherwise, my specs match yours quite closely.
Not a full answer maybe, but if it is installed, how about disabling it and retrying?

IIS7 + PHP + Zend - Not executing <= *pic*

I have PHP, IIS7, ReWrite Module for IIS and Zend all installed.
I can execute PHP pages just fine, even got PHPINFO showing up.
I setup a Zend quickstart app on IIS and when I open it this is what I see:
You can see from the source that it's not executing the <= portions.
Any idea what needs to change?
alt text http://www.gonrad.com/200902/zendiis.jpg
You need to enable shortags in your php.ini:
short_opentag = on
However, even though Zend's examples use the open tag, for portability you really should use the full echo statement as not all webhosts allow for short_opentag. The short tag might save a bit of time typing but may actually be worse in the long run.
make sure <? is enabled and not just <?php
if you use <?
php.ini --->short_opentag=On

Categories