I have the following code:
if (include_once(dirname(__FILE__).'/file.php')
|| include_once(dirname(__FILE__).'/local/file.php')
)
{
This causes an error because PHP tries to include "1" (presumably dirname(__FILE__).'/file.php' || dirname(__FILE__).'/local/file.php')
Commenting out the second line makes this work as intended, other than the fact it won't use the second file. Do I really need to use elseif and code duplication here, or is there a way to get this working?
$ php --version
PHP 5.2.6-3ubuntu4.2 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 21 2009 19:14:44)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
Group the include statements:
if ( (include_once dirname(__FILE__).'/file.php')
||
(include_once dirname(__FILE__).'/local/file.php')
)
See example #4 in the manual page:
<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
echo 'OK';
}
// works
if ((include 'vars.php') == 'OK') {
echo 'OK';
}
?>
Try using parentheses to subdue the operator precedence.
if ( (include_once('xml.php')) || (include_once('xml.php')) ) {
echo 'bah';
}
Related
In PHP, its possible to register shutdown functions, which (sometimes gets ignored, however) is definetely called in my scenario, see below.
PHP/libxml supported by the DOMDocument class in PHP does not play along well w/ my registered shutdown functions, if I want to call ->save() (->saveXML() works fine) after user abort (e.g. from registered shutdown function or a class instance destructor). Related is also the PHP connection handling.
Let the examples speak:
PHP version:
php --version
PHP 7.1.4 (cli) (built: Apr 25 2017 09:48:36) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
To reproduce user_abort I'm running the php through python2.7 run.py:
import subprocess
cmd = ["/usr/bin/php", "./user_aborted.php"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
# As this process exists here and the user_aborted.php
# has sleeps/blocks in for-cycle thus simulating a user-abort
# for the php subprocess.
The php script user_aborted.php to try saving XML in shutdown function :
<?php
ignore_user_abort(false);
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml_track = $xml->createElement( "Track", "Highway Blues" );
$xml->appendChild($xml_track);
function shutdown() {
global $xml;
$out_as_str = $xml->saveXML();
fwrite(STDERR, "\nout_as_str: " . var_export($out_as_str, true) . "\n");
$out_as_file = $xml->save('out.xml');
fwrite(STDERR, "\nout_as_file: >" . var_export($out_as_file, true) . "<\n");
fwrite(STDERR, "\nerrors: \n" . var_export(libxml_get_errors(), true) . "\n");
}
register_shutdown_function('shutdown');
$i = 2;
while ($i > 0) {
fwrite(STDERR, "\n PID: " . getmypid() . " aborted: " . connection_aborted());
echo("\nmaking some output on stdout"); // so user_abort will be checked
sleep(1);
$i--;
}
Now, if I run this script w/o user abort (simply calling PHP) with:
php user_aborted.php the XML gets saved properly.
However when calling this through python2.7 (which simulates the user abort by exiting the parent process), python2.7 run.py the weirdest things happen:
the out_as_str value is fine, looks the XML I wanted
BUT the file out.xml is an empty file
ALSO the libxml_get_errors reports FLUSH problems
The output w/ python looks:
python2.7 run.py
PID: 16863 aborted: 0
out_as_str: '<?xml version="1.0"?>
<Track>Highway Blues</Track>
'
out_as_file: >false<
errors:
array (
0 =>
LibXMLError::__set_state(array(
'level' => 2,
'code' => 1545,
'column' => 0,
'message' => 'flush error',
'file' => '',
'line' => 0,
))
)
Sorry for the long post, but I was looking through PHP/libxml2 code the whole day w/o any success. :/
Reason:
It turns out this is due to a fix for a previous bug.
Links:
previous PHP bug ticket which fix introduced the deffect
commit that introduces the deffect (GitHub)
The linked php_libxml_streams_IO_write function is the writecallback (set in ext/libxml/libxml.c) for the buffer of the docp object, which is handed over for the libxml call on ext/dom/document.c. Ending up in libxml xmlIO.c where the buffer is NULL hence the file given over for ->save(*) does not get written.
Workaround:
Use the ->saveXML() to get the XML representation in string and write it using file_put_contents(*) by "hand":
$xml_as_str = $xml->saveXML();
file_put_contents('/tmp/my.xml', $xml_as_str);
I want to look where the function is called.
Is the function called inside a echo or sprintf? Then return, otherwise echo the content.
I got this code (test.php):
<?php
function __($str = '')
{
// who is my parent?
$parent=debug_backtrace();
if ( isset ( $parent[1] ) )
$parent = $parent[1]['function']; //Did it!
else
$parent = debug_backtrace()[1]['function']; //Try
if ( empty ( $parent ) )
$parent = "ERROR!";
return sprintf("[%s] %s.%s", $parent, $str, PHP_EOL);
}
__('does nothing');
echo __('test from echo #1');
echo(__('test from echo #2'));
echo sprintf(__('test from sprintf #1'));
echo(sprintf(__('test from sprintf #2')));
?>
When I type it at the terminal all I get is:
WDGMBP:Test Wes$ php test.php
[ERROR!] test from echo #1.
[ERROR!] test from echo #2.
[ERROR!] test from sprintf #1.
[ERROR!] test from sprintf #2.
(p.s. from web the same)
My PHP version is:
WDGMBP:BIHappyV3 Wes$ php -v
PHP 5.5.27 (cli) (built: Aug 22 2015 18:20:44)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
You're confused about how debug_backtrace works. It returns the functions that were executing to make the call, not the function that will be called with the result. E.g. if you have:
function testit() {
someFunc(__('something'));
}
then $parent[1]['function'] will contain testit, not someFunc.
I don't think there's any way to get someFunc. That function isn't anywhere on the stack, because it doesn't get called until after __() returns.
I'm trying to build a Unit Testing environment for a CodeIgniter application. I'm following this book and got PHPUnit to work.
I'm at a point where I'm testing some code in a model:
public function testPresent()
{
$tracker = new Tracker_model();
$tableObj = (object)array( 'type' => $this->expectedConstants['TABLE']);
$visitsObj = (object)array( 'type' => $this->expectedConstants['VISITS']);
$tableObj = $tracker->present($tableObj);
$visitsObj = $tracker->present($visitsObj);
$this->assertThat($tableObj, $this->isInstanceOf('Tracker_TablePresenter'));
$this->assertThat($visitsObj, $this->isInstanceOf('Tracker_VisitsPresenter'));
}
The code being tested is this:
public function present($obj)
{
if( isset($obj->type) )
{
switch($obj->type)
{
case self::VISITS: $type = 'Visits'; break;
case self::TABLE:
default: $type = 'Table'; break;
}
$className = 'Tracker_'.$type.'Presenter';
$obj = new $className($obj, 'tracker');
}
return $obj;
}
It should load the class Tracker_TablePresenter which is in the file presenters/tracker/TablePresenter.php. I can't explain all the logic behind this mechanism because it would be too long and complex, but I know it works, because on another computer with the exact same code it's passing the test. On my computer, instead, I get this error:
.......PHP Fatal error: Class 'Tracker_TablePresenter' not found in /home/.../application/models/tracker_model.php on line 42
More information on the error:
PHP Stack trace:
PHP 1. {main}() /.../vendor/phpunit/phpunit/composer/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /.../vendor/phpunit/phpunit/composer/bin/phpunit:63
PHP 3. PHPUnit_TextUI_Command->run() /.../vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /.../vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /.../vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
PHP 6. PHPUnit_Framework_TestSuite->run() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705
PHP 7. PHPUnit_Framework_TestSuite->runTest() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
PHP 8. PHPUnit_Framework_TestCase->run() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
PHP 9. PHPUnit_Framework_TestResult->run() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:776
PHP 10. PHPUnit_Framework_TestCase->runBare() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
PHP 11. PHPUnit_Framework_TestCase->runTest() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:831
PHP 12. ReflectionMethod->invokeArgs() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
PHP 13. Tracker_modelTest->testPresent() /.../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
PHP 14. Tracker_model->present() /.../application/tests/models/Tracker_modelTest.php:38
I'm working on Ubuntu, while the other guy who has the code working is using a Mac. This is the output of my php -v
PHP 5.4.9-4ubuntu2 (cli) (built: Mar 11 2013 16:09:26)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
with Xdebug v2.3.0dev, Copyright (c) 2002-2013, by Derick Rethans
I also tried downgrading php to 5.3, since he is using the 5.3, but nothing. Same error.
Can you help me understanding what's going on?
Edit:
This is the content of the file TablePresenter.php
<?php
class Tracker_TablePresenter extends Presenter
{
public function values()
{
$this->ci =& get_instance();
if (!isset($this->values)) {
$this->ci->load->model('value_model', 'value');
$this->values = $this->ci->value->getManyForTracker($this->tracker->id);
}
return $this->values;
}
}
?>
Edit 2:
I changed the code in the present function as suggested by JoDev like this:
public function present($obj)
{
if( isset($obj->type) )
{
switch($obj->type)
{
case self::VISITS: $type = 'Visits'; break;
case self::TABLE:
default: $type = 'Table'; break;
}
//use include, but require is to force an internal error if the file isn't founded!
require_once('presenters/tracker/'.$type.'Presenter.php');
$className = 'Tracker_'.$type.'Presenter';
$obj = new $className($obj, 'tracker');
}
return $obj;
}
Now I get this error:
PHPUnit 3.7.21 by Sebastian Bergmann.
Configuration read from .../phpunit.xml
.......PHP Fatal error: Class 'Tracker_TablePresenter' not found in .../application/models/tracker_model.php on line 61
PHP Stack trace:
PHP 1. {main}() .../vendor/phpunit/phpunit/composer/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() .../vendor/phpunit/phpunit/composer/bin/phpunit:63
PHP 3. PHPUnit_TextUI_Command->run() .../vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() .../vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() .../vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
PHP 6. PHPUnit_Framework_TestSuite->run() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705
PHP 7. PHPUnit_Framework_TestSuite->runTest() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
PHP 8. PHPUnit_Framework_TestCase->run() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
PHP 9. PHPUnit_Framework_TestResult->run() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:776
PHP 10. PHPUnit_Framework_TestCase->runBare() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
PHP 11. PHPUnit_Framework_TestCase->runTest() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:831
PHP 12. ReflectionMethod->invokeArgs() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
PHP 13. Tracker_modelTest->testPresent() .../vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
PHP 14. Tracker_model->present() .../application/tests/models/Tracker_modelTest.php:37
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: include(presenters/tracker/TablePresenter.php): failed to open stream: No such file or directory</p>
<p>Filename: models/tracker_model.php</p>
<p>Line Number: 58</p>
</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: include(): Failed opening 'presenters/tracker/TablePresenter.php' for inclusion (include_path='.../vendor/phpunit/php-text-template:.../vendor/phpunit/phpunit-mock-objects:.../vendor/phpunit/php-timer:.../vendor/phpunit/php-token-stream:.../vendor/phpunit/php-file-iterator:.../vendor/phpunit/php-code-coverage:.../vendor/phpunit/phpunit:.../vendor/symfony/yaml:.../vendor/pdepend/pdepend/src/main/php:.../vendor/phpmd/phpmd/src/main/php:.:/usr/share/php:/usr/share/pear')</p>
<p>Filename: models/tracker_model.php</p>
<p>Line Number: 58</p>
</div>%
Please, can you try to include the class manualy by using :
public function present($obj)
{
if( isset($obj->type) )
{
switch($obj->type)
{
case self::VISITS: $type = 'Visits'; break;
case self::TABLE:
default: $type = 'Table'; break;
}
//use include, but require is to force an internal error if the file isn't founded!
require_once('Presenters/Tracker/'.$type.'Presenter.php');
$className = 'Tracker_'.$type.'Presenter';
$obj = new $className($obj, 'tracker');
}
return $obj;
}
Thank's to this test, you will be able to know if the error is in the autoloader or anywhere else!
And tell me what appends...
[EDIT #13/06/2013 10:00]
Where is the file calling TablePresenter in the tree?
To fix the problem, you have (in my opinion) three possibilities :
-1) Add parent directory of Presenter in the include_path of the serveur!
(to retrieve actual includings, use get_include_path())
-2) Be sure to use good URL to include the file
-3) Use absolute URL
I bet it has to do with file permissions on your machine. The script can't find the file because Apache can't open it.
Sounds like a case-sensitiveness issue (#jospratik 's question). As far as i know OSX is not case sensitive. So this is an issue you would have when working on linux vs mac/win.
Change the folder names to start with uppercase char like
Presenters/Tracker/VisitsPresenter.php
seems to me like a problem with Class 'Tracker_TablePresenter' which couldnt be found. did you use the correct namespace?
I have a script to limit the execution time length of commands.
limit.php
<?php
declare(ticks = 1);
if ($argc<2) die("Wrong parameter\n");
$cmd = $argv[1];
$tl = isset($argv[2]) ? intval($argv[2]) : 3;
$pid = pcntl_fork();
if (-1 == $pid) {
die('FORK_FAILED');
} elseif ($pid == 0) {
exec($cmd);
posix_kill(posix_getppid(), SIGALRM);
} else {
pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
sleep($tl);
posix_kill($pid, SIGKILL);
die("TIMEOUT_KILLED : $pid");
}
Then I test this script with some commands.
TEST A
php limit.php "php -r 'while(1){sleep(1);echo PHP_OS;}'" 3
After 3s, we can find the processes were killed as we expected.
TEST B
Remove the output code and run again.
php limit.php "php -r 'while(1){sleep(1);}'" 3
Result looks not good, the process created by function "exec" was not killed like TEST A.
[alix#s4 tmp]$ ps aux | grep whil[e]
alix 4433 0.0 0.1 139644 6860 pts/0 S 10:32 0:00 php -r while(1){sleep(1);}
System info
[alix#s4 tmp]$ uname -a
Linux s4 2.6.18-308.1.1.el5 #1 SMP Wed Mar 7 04:16:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
[alix#s4 tmp]$ php -v
PHP 5.3.9 (cli) (built: Feb 15 2012 11:54:46)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
Why the processes killed in TEST A but not in TEST B? Does the output impact the SIGKILL?
Any suggestion?
There is a PIPE between php -r 'while(1){sleep(1);echo PHP_OS;} (process C) and it's parent (process B), posix_kill($pid, SIGKILL) sends KILL signal to process B, then process B is terminated, but process C doesn't know anything about the signal and continues to run and outputs something to the broken pipe, when process C receives the SIGPIPE signal but has no idea how to handle it so it exits.
You can verify it with strace (run php limit.php "strace php -r 'while(1){sleep(1); echo PHP_OS;};'" 1), and you will see something like this:
14:43:49.254809 write(1, "Linux", 5) = -1 EPIPE (Broken pipe)
14:43:49.254952 --- SIGPIPE (Broken pipe) # 0 (0) ---
14:43:49.255110 close(2) = 0
14:43:49.255212 close(1) = 0
14:43:49.255307 close(0) = 0
14:43:49.255402 munmap(0x7fb0762f2000, 4096) = 0
14:43:49.257781 munmap(0x7fb076342000, 1052672) = 0
14:43:49.258100 munmap(0x7fb076443000, 266240) = 0
14:43:49.258268 munmap(0x7fb0762f3000, 323584) = 0
14:43:49.258555 exit_group(0) = ?
As to php -r 'while(1){sleep(1);}, because there is no broken pipe occurs after it's parent dies, so it continues to run as expected.
Generally speaking, you should kill the whole process group but not only the process itself if you want to kill it's children too, with PHP you can add process B to its own process group, and kill the whole group then, here is the diff with your code:
--- limit.php 2012-08-11 20:50:22.000000000 +0800
+++ limit-new.php 2012-08-11 20:50:39.000000000 +0800
## -9,11 +9,13 ##
if (-1 == $pid) {
die('FORK_FAILED');
} elseif ($pid == 0) {
+ $_pid = posix_getpid();
+ posix_setpgid($_pid, $_pid);
exec($cmd);
posix_kill(posix_getppid(), SIGALRM);
} else {
pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
sleep($tl);
- posix_kill($pid, SIGKILL);
+ posix_kill(-$pid, SIGKILL);
die("TIMEOUT_KILLED : $pid");
}
You send the kill signall to your forked process, but that does not propagate to it's children or grandchildren. As such they are orphaned and continue running until something stops them from doing so. (In this case, any attempts to write to stdout should cause an error that then forces them to exit. Redirection of output would also probably result in indefinitely-running orphans.)
You want to send a kill signal to the process and all it's children. Unfortunately I lack the knowledge to tell you a good way to do that. I'm not very familiar with the process control functionality of PHP. Could parse the output of ps.
One simple way I found that works though is to send a kill signal to the whole process group with the kill command. It's messy, and it adds an extra "Killed" message to output on my machine, but it seems to work.
<?php
declare(ticks = 1);
if ($argc<2) die("Wrong parameter\n");
$cmd = $argv[1];
$tl = isset($argv[2]) ? intval($argv[2]) : 3;
$pid = pcntl_fork();
if (-1 == $pid) {
die('FORK_FAILED');
} elseif ($pid == 0) {
exec($cmd);
posix_kill(posix_getppid(), SIGALRM);
} else {
pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
sleep($tl);
$gpid = posix_getpgid($pid);
echo("TIMEOUT_KILLED : $pid");
exec("kill -KILL -{$gpid}"); //This will also cause the script to kill itself.
}
For more information see: Best way to kill all child processes
I'm working on a project and I need to user translation for it. So I decided to use gettext in php but it's working sometimes.
So I have a folder named lng and in this on I have a php file who call my lang file to translate my page.
Here is the code :
<?php
if(isset($_GET['lang']) != '')
{
setcookie('lang',$_GET['lang'], time() + 365*24*3600, null, null, false, true);
$_SESSION['lang'] = $_GET['lang'];
$language = $_GET['lang'];
}
else if(isset($_SESSION['lang']) != '' )
{
$language = $_SESSION['lang'];
}
else if(isset($_COOKIE['lang']) != '')
{
$_SESSION['lang'] = $_COOKIE['lang'];
$language = $_SESSION['lang'];
}else{
$language = 'fr';
}
putenv("LANG=$language");
setlocale(LC_MESSAGES, $language);
$domain = 'trad';
bindtextdomain($domain, 'locale/');
textdomain($domain);
?>
So I can check my $_SESSION and $_COOKIE, no problem he give me 'en' or 'fr' but he doesn't translate my file and I don't know why.
For the folder in lng it's : locale/en/LC_MESSAGES/trad.po (or .mo).
I try with LC_ALL and LC_MESSAGES but that doesn't change the result.
Did I miss something or made a wrong stuff?
Thanks a lot!
J.
I've got the same problem. Restart Apache Service solved it for me
Aren't you using windows? If so, you must use windows locale names. Here is part of my class working for me on linux and also on windows (it simply has more options of locale names):
...
private function setLocaleByLang($lang)
{
$map = array(
'cs' => array('cs_CZ.UTF-8', 'cs_CZ', 'cs', 'czech'),
'en' => array('en_US.UTF-8', 'en_US', 'en', 'english'),
'de' => array('de_DE.UTF-8', 'de_DE', 'de', 'german'),
'pl' => array('pl_PL.UTF-8', 'pl_PL', 'pl', 'polish'),
'sk' => array('sk_SK.UTF-8', 'sk_SK', 'sk', 'slovak')
);
$locale = key_exists($lang, $map) ? $map[$lang] : $lang;
setlocale(LC_ALL, $locale);
putenv('LC_ALL=' . $lang); // for windows and gettext
}
...
I'm running into the same error. My case is a bit different, and I'm starting to think it has something to do with threading, despite it happens since I changed my code.
I have a language bar:
<?php
include_once (dirname(__FILE__) . "/resources/config.php");
?>
<div id='language_bar'>
<a style="margin-left:50px" href="./index.php?locale=es_ES">
<img src='<?php echo $config['paths']['images']['lang']?>/es_ES.gif'/>
</a>
<a href="./index.php?locale=en_UK">
<img src='<?php echo $config['paths']['images']['lang']?>/en_UK.gif'/>
</a>
<a href="./index.php?locale=de_DE">
<img src='<?php echo $config['paths']['images']['lang']?>/de_DE.gif'/>
</a>
</div>
And a config file with:
if (isset($_GET['locale'])) {
$locale = $_GET['locale'];
setcookie('locale', $locale, time() + 60 * 60 * 24 * 30);
} else {
if(isset($_COOKIE['locale'])) {
error_log('En _COOKIE');
$locale = $_COOKIE['locale'];
}
else {
$locale = $config['localization']['default_locale'];
setcookie('locale', $locale, time() + 60 * 60 * 24 * 30);
}
}
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
error_log('setlocale->'.setlocale(LC_ALL, "0")." Env ->". getenv("LC_ALL"));
error_log(_("Submit"));
My main page has some divs interacting via jQuery, and reloading in cascade. Sometimes, some of them (randomly) give a default string value.
by default, it is in spanish (es_ES), and after a few clicks forcing div refreshes, some string is printed in english (en_UK original string). And what is more. If I switch to german (de_DE), after a first refresh where I get every string in german, gettext starts to reurn strings in spanish, and after a while, in english.
Note that I added debug lines to php log. They are really interesting:
When things go right:
[Thu May 31 00:28:51 2012] [error] [client ::1] setlocale->es_ES Env ->es_ES
[Thu May 31 00:28:51 2012] [error] [client ::1] Aplicar, referer: xxxxxxxx/index.php
When do not:
[Thu May 31 00:29:45 2012] [error] [client ::1] setlocale->es_ES Env ->es_ES, referer: xxxxxxxxx/index.php
[Thu May 31 00:29:45 2012] [error] [client ::1] Submit, referer: xxxxxxxx/index.php
So I guess it's _() function failing (I'm always using the alias). Just in case, I looped for 10.000 times over the function, and it gave or 10.000 hits or 10.000 mistakes while translating, so it fails for a whole http request, or doesn't.
My apologies for writing so much, but I would really appreciate some help pointing me in the right direction. (This error occurs not just # my localhost, but also at my online test server)
¿might have anything to do with the fact that I'm setting the locale for every connection?
My online "playground" is:
Linux server8.nixiweb.com 2.6.32-71.29.1.el6.x86_64 #1 SMP Mon Jun 27 19:49:27 BST 2011 x86_64
My server:
Linux filete 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64
PHP Version 5.3.10-1ubuntu3.1
Note that both are 64bits
I had the same problem. Sometimes the strings were translated and sometimes don't. I have 3 servers for this application and 5 developers' machines, all with the same problem.
I solve it by removing:
bindtextdomain("domain", "/locale");
And linking the .mo file directly on the gettext default folder:
sudo ln -sf /myproject/locale/en/LC_MESSAGES/domain.mo /usr/share/locale/en/LC_MESSAGES/domain.mo
I am using Ubuntu 14.04.