How can I check a PHP script for structure errors? - php

I have a lot of ajax that calls php files to do tasks, like write things to the server. I use load calls so anything the php echoes when it gets into trouble comes back to me. But if I've done something really dumb, like leave off a ")", the PHP doesn't even start and all I get is a cryptic
POST http://www.mysite.com/publishPage.php 500 Internal Server Error
Isn't there something that will tell me "missing ) at line 119" ?
I've placed
ini_set('display_errors', 1);
error_reporting(E_ALL);
at the top of the PHP scripts but that doesn't seem to be helping me here.
Thanks

Syntax errors can not be "checked" if you missed the code, will have to fix before trying to run it.
If you have a syntax error in java, the code will not be compiled. php is not compiled, then this is the solution

Related

PHP ERROR 500 but execution ok

I have a simple self-made MVC structure composed by Controllers, Models and Views.
It was working fine, but now one controller is performing very strange behaviour...
Script gives error 500 (if you check headers) but execution and output is ok.
No error messages, no error log, nothing... but let me tell you the most strangest thing:
if I write debug directives for log information like these
error_reporting(E_ALL);
ini_set('display_errors', 1);
Then error 500 disapears... instead of displaying error information, and work like a charm.
Can't understand this situation. Any help?
Regards
Problem solved. I was using a PHP include which contain some # symbols, for example: #file_get_contents... This is a not recommended practice, now I know why :)
Thanks for your interest

PHP - Weird behaviour after calling functions in a map

I'm experiencing quite a strange reaction from the browser when invoking this PHP script.
<?php
$map = array(
'a' => function(){
print_r('a');
},
'b' => function(){
print_r('b');
}
);
$map($_GET['v']);
?>
I already noticed that there is a mistake there. The syntax of the call is wrong, as it should be like this:
$map[$_GET['v']]();
The thing is that the reaction of the browser to this mistake is not what it should be.
The result of running this script is a 'The connection was reset' message. The server is up and running correctly, as other PHP files (and this one after correcting the mistake) run perfectly.
But what is actually puzzling me is what the navigation bar of the browser does. When I punch in the URL
localhost/cerdo.php?v=a
the content of the bar changes to
www.localhost.com/cerdo.php?v=a
The www.localhost.com part seems to happen only in Firefox. I've tried it on Chromium and, despite showing a similar message ('No data received') the URL stays the same.
What is happening? Does this make any sense? Shouldn't PHP be reporting a syntax error? And why on earth would Firefox redirect to www.localhost.com?
Shouldn't PHP be reporting a syntax error?
No. If PHP is not reporting $map($_GET['v']); as syntax error is because it is expecting the code to be syntactically valid. So it ends up executing some very weird stuff that you are not expecting. This results in redirecting your browser to some unexplainable location.
The key here is to understand what $map($_GET['v']); actually means.
The redirection doesn't have anything to do with your code. How is your environment setup, e.g. are you using Xampp etc.? In that case make sure there is any index.php or .htaccess or anything else that contains a redirection script in your web root.
If it's not your first time with you dev enviroment, please ignore this:
I think your problem is: the OS couldn't recognise the domain name (and the OS extends it).
Under Windows you can find the 'hosts' file here:
"%SYSTEMROOT%\System32\drivers\etc\"
Add this line to the end of the file:
127.0.0.1 localhost
(Maybe you should restart your computer.)
It should solve your

php, ajax error reporting is not working

i have this file and on a click event it calls a php function through $.post{}
i have used alert(data) to recognized errors. But unfortunately its not allerting anything except it shows an error on firebug console with relevant url "Internal server error 500" and then i tried to access the code through browser pasting the full url, I have put error_reporting(E_ALL);
ini_set("display_errors", 1); , but shows a blank page. I'm lost here no idea how to solve this without any errors displaying.. Help much appreciated.
p.s in php info error reporting is off. i tried htaccess on my subfolder without no luck either.
Make sure of the following:
error_reporting(E_ALL) is right after <?php tag
There is indeed a PHP error - try to deliberately make one. Maybe the error is elsewhere, not your PHP but the .htaccess file (since you're getting 500), or maybe it's a logical error.

PHP website does not work, and nothing logged in the error log

I have a problem with PHP. There is an error that I have never encountered before. The home.php, which is the main page of my site, cannot be viewed. And there is nothing in error log. After there is an inclusion of a php page (a class that is a part of my API). Till that line the holl HTML (javascript and css inclusions) are echoed from php and successfully rendered in browser, but after the php kind of stops suddenly. When I delete that line(php page inclusion line), website works, but before, this inclusion didn't make any problem for me. I removed the code parts which after them I encountered this problem, but nothing works.
I am using Apache2, PHP5 with Ubuntu 11.10.
Any help will be appreciated, thanks in advance.
My first hints would be to check the following:
In your script set ini_set('display_errors', '1'); and error_reporting(E_ALL); to display all errors.
Turn on the php error log and check it for errors.
run php -l home.php on the command line to check your php file for syntax errors.
Check Apache's error log, sometimes error messages go there.
If nothing helps use a debbugger like XDebug to see where the script terminates, or alternative insert statements like die("here"); and move them around in your code, to see which parts of your scripts are passed.
Greetings and good luck.

Errors in PHP project when shifted from IIS to Apache server

I developed a php project in WebMatrix on IIS. In this project I accidently initialed same "session variable" twice.
Example: File - a.php
<?php
$_Session['one'];
include 'b.php';
...........
...........
...........
...........
?>
Example: File - b.php
<?php
$_Session['one'];
...........
...........
...........
...........
?>
When I run this project from Webmatrix(IIS server) this error wasn't shown but when I ran this project on Apache this error was displayed and I corrected my flaws.
Warning on Apache:
Notice: A session had already been started - ignoring session_start() in D:/path/.
My question is that why this error was not shown earlier? Is it something related to IIS server or WebMatrix. I always need to be aware of the errors or warnings in the code so that I can get rid of them and the efficiency of the code is sustained, please do suggest me some ideal php develeopment tools which can catch even minor errors in my code.
Ensure errors are being displayed and then try turning error_reporting all the way up at the beginning of the application.
ini_set('display_errors', 1);
error_reporting(E_ALL);
Also, be aware that Windows is not case-sensitive. You could say include 'A.php' or include 'a.php' and it would not care. However, when you put it on a Linux-based server, which is case-sensitive, it may not be able to find the file A.php if its actually a.php.
It's possible you got errors because the first server was running PHP 4 and the second is PHP 5. Double-including a file containing functions will get different results in those versions. See the text just before example 6 in this link: http://php.net/manual/en/function.include.php
EDIT: I believe the error messages generated in your edited post indicate something of a different nature.
For files that you want to make sure they are only included one time, use include_once(). http://www.php.net/manual/en/function.include-once.php
There are some situations where you may legitimately want to include() twice from a file. But because those situations are rare, it's best to use include_once().
Warning on Apache:
Notice: A session had already been started - ignoring session_start() in D:/path/.
You can't start session again which is already started.
You need 1 session start at a.php when you include b.php it can use started session.
Its a warning and warning means its not good but php can fix this error and continue working.
Its all about error reporting level.
It is hard to tell if the error didn't occured on IIS or not. Only because it was not displayed or logged, does not mean it didn't occur. Please compare the error reporting php.ini configuration between both systems.
Another thing you should compare in configuration is if session auto-start had been enabled on the old or new system or not. This might be triggering your error, but it's only a guess. As written the error might have been already on the old system but just was unnoticed.

Categories