I have a weird PHP error in a current Symfony2 project:
unexpected T_STRING in /blahblah/Foo/BarBundle/Entity/User.php on line 1
This is a pretty standard error, usually linked to a mismatched pair of " or '.
But here is the code of the file User.php
<?php
namespace Foo\BarBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #ORM\Entity(repositoryClass="Foo\BarBundle\Entity\UserRepository")
*/
class User extends \FOS\UserBundle\Entity\User
{
// classic user entity
The line <?php is line #1. There is no quotes, and the weird thing comes from the fact that this error only appear on my staging server: on 2 development machines with local copies of the code, it behaves as expected with no error or warning.
The file is the correct one, the cache was emptied. I thought that it might be an encoding error but it does not seem to be this. I also thought of namespace issues, but the PHP version on the server is correct (5.3.16)
Do you have any idea what this error can stem from, or in which direction I could search ? Thanks in advance.
Most coding conventions that I worked with strictly require using LF ('Unix style', '\x0A') line endings in the scripts. And whoever managed to submit code with CRLF or, god forbid, CR had to endure a royal share of pain. )
It may seem not such a big deal, yet it can save you hours of searching for a weird error - such as in this case.
I think it's an encoding problem of your file. If your project is encoded UTF8 for example, open your file with your text editor and choose the option "Encoding" -> UTF-8 without BOM.
Related
Suddenly, an application isn't any longer able to output ZIP files. An inspection revealed the cause: The first character of the ZIP is a blank, which breaks the ZIP format spec.
To track down this problem, I enabled CStatementTracer, which prints each line of executed code to a log file. Didn't help. [Remark: declare(ticks=1); doesn't seem to trap each line of executed code]
I then set an output handler like so:
function callback( $buffer ) {
$deb = print_r( debug_backtrace(), TRUE );
file_put_contents( './statementTrager.log', $deb );
return $buffer;
}
ob_start("callback", 1 );
Unfortunately, this handler isn't called at all.
Q: Does a generic / canonical solution exists, which identifies the file / line of PHP-code, which emits the first character.
A solution, that finds the loc whatever other code gets executed.
Remarks:
Not even a single PHP file is closed using ?>
Meanwhile I found the suspicious like of code: A blank in front of a starting
Still, I'd like to get hints regarding a programmatic solution. Preferrably a solution written in pure PHP.
https://linux.die.net/man/1/strace is probably the most reliable tool to find out where the output comes from. Assuming you are on Linux. There must be similar tools for other platforms.
Although it will not give you the line of the php code, you can analyse the context of system calls made before and after the offensive character was sent. Usually it is enough to identify where the problem originates.
It is quite time consuming process though. Should be used as the last resort.
I have the following PHP code snippet written in an attempt to use COM to access Crystal Reports XI (Version 14.0.4.738 RTM):
$ObjectFactory = new COM("CrystalReports14.ObjectFactory.1") or die("CR loading failed");
$crapp = $ObjectFactory->CreateObject("CrystalReports14.ObjectFactory.1");
$creport = $crapp->OpenReport($rptFile, 1);
...
The first two lines seems to be ok on the surface. The third line throws an error, however:
Fatal error: Call to undefined method variant::OpenReport() in ...
I am not 100% certain that I have done the CreateObject() call (second line) correctly; it seems oddly redundant that I am using the same string for the first line and second line. But maybe that's the way it is.
After searching on the web for quite literally 2 or 3 hours, I am starting to think that there is no documentation on this, but refuse to think that nobody has ever done this before! Does anyone have any experience with this?
Thanks in advance!
It is strange that you use ObjectFactory in the ProgID to create the COM.
Object factories, in COM, have a specific meaning and usually they are not applications.
I have found a question related to Crystal Reports and there they use a string that seems to me much likely to be the application ID. You should try it:
Crystal Reports & VBScript - Could Not Locate Automation Class
New to PHP and changed some code further down than line 1 on a WordPress site.
Using Dreamweaver CC to upload. Getting:
Parse error: syntax error, unexpected T_IF in
/home/jodyrein/public_html/www.writersblogfinder.com/wp-content/themes/rockthebook/functions.php
on line 1
at www.writersblogfinder.com
Here is the first few lines of code:
<?php
/**
* rockthebook functions and definitions
*
* #package rockthebook
*/
/**
* Set the content width based on the theme's design and stylesheet.
*/
if ( ! isset( $content_width ) )
$content_width = 640; /* pixels */
Check that you have not edited your file using a horrible editor, like notepad. Your if() statement is reported as line 1, prrobably because of the lineends. Save the file in another editor, that uses newline instead or carriage return.
I've had similar problems before with hidden/non standard characters creeping into the file thanks to bugs / quirks in the editor. Best thing to do - copy the whole lot of the code into something like notepad (PC) or textmate (mac) and save the file out again, re-upload and re-test it... usually resolves it for me.
Seems Dreamweaver was loading the php text all as one line. I pasted the php into the Wordpress editor and and it's fine
copying to Notepad editor saving and uploading fixed my problem.
I just wanted to mention after hours of trying to figure this out, re-saving from Notepad worked!
I use Sublime Text 3 as my daily text editor, so I find it quite odd that this issue wasn't being picked up, but copying the code from ST3, pasting in to Notepad and then saving over functions.php fixed the error.
I should also note that I was only seeing this error on the server, not on my localhost.
Thank you for this advice!
I'm attempting to write a bit of code that will allow users to change their expired Active Directory passwords via a PHP web interface. Due to limitations with PHP's ldap library's*, it seems the only way to do this is by generating an ldif and then passing this directly to ldapmodify.
The code I've come up with (minus the vars) is:
ldapmodify -H {$ad_server} -D '{$dn}' -w {$old} <<!
dn: {$dn}
changetype: modify
delete: unicodePwd
unicodePwd:: {$oldPassword}
-
add: unicodePwd
unicodePwd:: {$newPassword}
-
!
The code appears to work fine when I paste the generated code straight in to my console, but so far I've had no luck running it from PHP.
I originally tried passing the code to exec, only to get exitcode 247(which doesn't appear to be a real thing)
I then attempted to use proc_open instead, which provided the current error
ldapmodify: invalid format (line 5) entry: " ... "
So far as I can see the only thing on line 5 is a "-". So I'm a bit stuck as to what could be wrong.
P.S. I also read this post LDIF file error?? Invalid Format? which reported a similar problem, although assuming the encoding of the "-" character is the issue, I'm not sure what I can really do with it from with PHP (mb_string_encoding the whole string into utf-8 doesn't appear to have any effect)
This is also running on a solaris machine which may also be a factor.
*PHP is unable to perform two actions within a single command, somthing that is required in order to do a user password change in AD. (so far as I'm aware)
Edit: No sure why this is getting downvotes, but I'd be happy to be told I'm an idiot if I'm doing something patently stupid without noticing (so long as you point out what that is, as I've been stuck on this for a while now)
Thanks to some help from the #ldap channel on freenode it turns out I am indeed an idiot (especially considering that I've been poking and prodding this for most of the day).
It seems ldapmodify does not like it when an LDIF contains a windows new line characters after the "-" Switching line endings from windows to unix in sublime has fixed the problem for me*.
I'm trying to debug a plugin-bloated Wordpress installation; so I've added a very simple homebrew logger that records all the callbacks, which are basically listed in a single, ultimately 250+ row multidimensional array in Wordpress (I can't use print_r() because I need to catch them right before they are called).
My logger line is $logger->log("\t" . $callback . "\n");
The logger produces a dandy text file in normal situations, but at two points during this particular task it is adding something which causes my log file to no longer be encoded properly. Gedit (I'm on Ubuntu) won't open the file, claiming to not understand the encoding. In vim, the culprit corrupt callback (which I could not find in the debugger, looking at the array) is about in the middle and printed as ^#lambda_546 and at the end of file there's this cute guy ^M. The ^M and ^# are blue in my vim, which has no color theme set for .txt files. I don't know what it means.
I tried adding an is_string($callback) condition, but I get the same results.
Any ideas?
^# is a NUL character (\0) and ^M is a CR (\r). No idea why they're being generated though. You'd have to muck through the source and database to find out. geany should be able to open the file easily enough though.
Seems these cute guys are a result of your callback formatting for windows.
Mystery over. One of the callbacks was an anonymous function. Investigating the PHP create_function documentation, I saw that a commenter had noted that the created function has a name like so: chr(0) . lambda_n. Thanks PHP.
As for the \r. Well, that is more embarrassing. My logger reused some older code that I previously written which did end lines in \r\n.