HTTP ERROR 500 If their is any Error in PHP Code - php

See this Screenshot http://prntscr.com/fmcnp7
If my PHP Code have any bug, like missing semicolon, then i get this error. Instead of this, i am supposed to get a error line by PHP right? What's wrong?

HTTP ERROR 500 can be spotted as error when you miss something in PHP Code but not semi colon.If you miss semi-colon from PHP, the page won't display anything.
The better way is to share your code and then we can take a look on it.

first of all your question is incomplete but that's not your problem.. you are learning we are all human.
You can check where the error is coming from in two different ways.
the first and inconvenient one is opening the logs file which is in your root directory of your website and search for the latest time-stamp and read through where your error is coming from.
The other is is to include these lines of code on top of your index webpage
ini_set("display_errors",1);
ini_set("display_startup_errors",1);
error_reporting(E_ALL);
?>
then you are set.
For me i wrap this code inside a class with static method
// errors.php
<?php
class errorReporting{
public static function displayerrors($switch){
if($switch == "on"){
ini_set("display_errors",1);
ini_set("display_startup_errors",1);
error_reporting(E_ALL);
}
return false;
}
}
?>
//index.php
<?php
include_once "errors.php";
errorReporting::displayerror("on");
?>

Related

php require_once modify the local variable?

In my web page, I wrote:
<?php
//define('__PUBLIC__', $_SERVER['DOCUMENT_ROOT'].'/public');
$doc_public = $_SERVER['DOCUMENT_ROOT'].'/public';
echo "Before include...<==============>$doc_public";
?>
<?php require_once($doc_public.'/inc/head.php'); ?>
<?php echo "After include...<==============>$doc_public"; ?>
And the page shows:
This firstly happened when I notice the fatal error in the footer, but the head is fine.
Although I can implement define or constant variable to avoid this, I am still curious how it happens.
P.S.: I run this under Apache with a port 8001. This is set in 【apache\conf\extra\httpd-vhosts.conf】. I am running more than one webapp under this site. I just share this information, as I am not sure this has anything to do with this case.
Thanks!
When you require a file, if a variable is modified it affects the original script as well, that's how it's designed. Require doesn't create a secondary environment separated from the including file, it just adds the PHP code in sequence, exactly like if you had written the code in the initial file.
Have a look at the official PHP documentation, the first example is exactly the same as your case
http://php.net/manual/en/function.include.php
(include is the same as require, the latter just throws an error. For more info about differences between include and require http://php.net/manual/en/function.require.php)

Including not working , is it too many includes?

Hi StackOverFlow members.
I need a favor regarding my programming script.
I'm stunned trying out different various ways to make my code more simpler.
Before I tried to make things simpler . The code works great and it able to do the function.
Here goes:
Detection PHP which worked on a HTML previously before I tried to tweak it a little:
<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
header('Location: http://facebook.com');
exit(0);
}
?>
<b> Testing / This is not a supported page for mobile </b>
This code works like a charm. When I'd use the browser to access the following page.
It shows me the message "Testing / This is not a supported page for mobile ".
When I'd used a mobile phone to access it , it works like a charm and redirect me back to Facebook.
So this is the new problem that I faced .
I just removed the PHP from the HTML and put it in a file called "detection.php"
<?php
include 'detection.php';
?>
<b> hello </b>
<p> testing on anarchy </p>
But this time , when I used the web browser to access the site , it showed me the message
"Testing on anarchy" and when I used the mobile as well. It still redirect me to the same page
"Testing on anarchy".
How do we make it in a way where you just need to include the PHP files on the HTML header rather than writing the PHP code on the HTML page.
Any idea?
Try Using: you have to use the brackets ('page');, and you should not repeat any code as you are already calling the code from another page, so all you add is:
<?php
include('Mobile_Detect.php');
?>
and on the mobile_detect.php page add:
<?php
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
header('Location: http://facebook.com');
exit(0);
}
?>
Always use "require" instead of "include".
"require" throws a Fatal Error if the function doesn't find the file, and the script is halted.
"include" throws a Warning if the function doesn't find the file and the script continue to run, which means that you might not notice this bug if you turned off the PHP errors.
Also rely on "require_once" to avoid problems such as "Cannot redeclare function" or "Cannot redeclare constant" etc.
Also, if you're using "header()" you need to make sure you're not using "print" or "echo" prior to the "header" function. Otherwise, you'll get the "headers already sent" error.
To debug your problem, you should use "require_once" and then you should check if it fails. If it fails, then you probably have a problem with your path.

PHP Call to undefined function

I am trying to call a function from another function. I get an error:
Fatal error: Call to undefined function getInitialInformation()
in controller.php on line 24
controller.php file:
require_once("model/model.php");
function intake() {
$info = getInitialInformation($id); //line 24
}
model/model.php
function getInitialInformation($id) {
return $GLOBALS['em']->find('InitialInformation', $id);
}
Things already tried:
Verified that the require_once works, and the file exists in the specified location.
Verified that the function exists in the file.
I am not able to figure this out. Am I missing something here?
How to reproduce the error, and how to fix it:
Put this code in a file called p.php:
<?php
class yoyo{
function salt(){
}
function pepper(){
salt();
}
}
$y = new yoyo();
$y->pepper();
?>
Run it like this:
php p.php
We get error:
PHP Fatal error: Call to undefined function salt() in
/home/el/foo/p.php on line 6
Solution: use $this->salt(); instead of salt();
So do it like this instead:
<?php
class yoyo{
function salt(){
}
function pepper(){
$this->salt();
}
}
$y = new yoyo();
$y->pepper();
?>
If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.
This was a developer mistake - a misplaced ending brace, which made the above function a nested function.
I see a lot of questions related to the undefined function error in SO. Let me note down this as an answer, in case someone else have the same issue with function scope.
Things I tried to troubleshoot first:
Searched for the php file with the function definition in it. Verified that the file exists.
Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the require/include is correct.
Verified that the filename is spelled correctly in the require statement.
Echoed a word in the included file, to see if it has been properly included.
Defined a separate function at the end of file, and called it. It worked too.
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
I already defined a simple print function at the end of included file. I moved it to just above the "undefined function". That made it undefined too.
Identified this as some scope issue.
Used the Netbeans collapse (code fold) feature to check the function just above this one. So, the 1000 lines function above just collapsed along with this one, making this a nested function.
Once the problem identified, cut-pasted the function to the end of file, which solved the issue.
Many times the problem comes because php does not support short open tags in php.ini file, i.e:
<?
phpinfo();
?>
You must use:
<?php
phpinfo();
?>
Your function is probably in a different namespace than the one you're calling it from.
http://php.net/manual/en/language.namespaces.basics.php
I happened that problem on a virtual server, when everything worked correctly on other hosting.
After several modifications I realized that I include or require_one works on all calls except in a file.
The problem of this file was the code < ?php ? > At the beginning and end of the text.
It was a script that was only < ?, and in that version of apache that was running did not work
This is obviously not the case in this Q,
but since I got here following the same error message I though I would add what was wrong with my code and maybe it will help some one else:
I was porting code from JS to PHP and ended up having a class with some public method.
The code that was calling the class (being code that originated from JS) looked something like:
$myObject.method(...)
this is wrong because in PHP it should look like this:
$myObject->method(...)
and it also resulted with "PHP Call to undefined function".
change to use -> and the problem was solved.
Presently I am working on web services where my function is defined and it was throwing an error undefined function.I just added this in autoload.php in codeigniter
$autoload['helper'] = array('common','security','url');
common is the name of my controller.
Please check that you have <?PHP at the top of your code. If you forget it, this error will appear.

Call to undefined function from another php file

Alright this is what my code looks like
index.php
require_once($WebsiteRoot . "/include/testfile.php");
TestFunction();
/include/testfile.php
function TestFunction()
{
echo "It Works";
}
And it gives me the error:
Fatal error:
Call to undefined function TestFunction() in /path/index.php on line 49
Any idea what i'm doing wrong?
Thanks
You haven't included a <?php tag in the included file, so it's just interpreted as plaintext input.
Remember... there's no such thing as a PHP script. There's only files which contain PHP code blocks. Without at least one <?php opening tag, the PHP interpreter will never be invoked and the file's contents will simply be treated as output.
try calling another function from testfile.php, if this is'nt working, its something with the include. Add the code:
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
to the top of index.php and refresh the browser to see your errors, try debugging from there.
The problem that i can forsee is that you are using a URL instead of a path, your $websiteRoot variable should contain a path like:
$websiteRoot = "/var/www/html/websiteName";
OR
$websiteRoot = "C://xampp/htdocs/websiteName";
instead of a URL like:
$websiteRoot = "http://www.somesite.com";
I had a similar issue. I dug into the PHP in the included file and found an invalid PHP tag. I had <? instead of <?php. PHP 7.2 and earlier forgave that, but PHP 7.3 was throwing that same error you faced.
Make sure you're including the file you think you are. If your index.php page looks exactly like you've stated, then it won't return anything.
If you want to link to the same location from anywhere on the site without worrying about relative locations, then at the beginning of the file, put:
$WebsiteRoot=$_SERVER['DOCUMENT_ROOT'];
And it should work fine, provided your file would be located at http://mywebsite.com/include/testfile.php
Try renaming the included file.
I had an included file with the name "system.php". It looked as if the include command was just skipped. Even with the most strict error reporting there was no message and even an echo command in the main body of the included file did not produce output. It had worked ok under PHP 5 but after the upgrade to a 7.2 environment these problems arose. After much effort - I forgot how - I managed to get an error message. It said there was a conflict with a PEAR class with the name "system". Yet my file didn't contain any class, just variables and functions. Anyway, giving the file another name than "system.php" worked for me.
I hope someone else can add a more technical comment on what was going wrong here.

Syntax error on closing brace after opening a php tag

i'm getting trouble with a function that looks pretty much like this:
<?php
function my_function() {
if(!empty($variable)) {
//Do some stuff
}
else {
?>show a message<?php
}
}
?>
The problem is i'm getting a parse error:
Parse error: syntax error, unexpected ‘}’ in /www/bla/bla/bla.php on line 8
I know for a fact that i'm not missing or have an extra '}' brace because the code works just fine on my local server and i've run a validator to make sure the syntax is correct, however, when i port the code to the online server, i get the error.
I believe it has something to do with the php installation not supporting the closing and reopening of php tags between the condition but i'm not sure how would i go about fixing it.
I know i could just do echo 'message'; instead, but this is not the only place where the script uses that kind of syntax to display messages, so fixing it here would just mean i'd get the error on another line, and then another.
Thanks.
As it stands that piece of code runs just fine as is on PHP 5.2.14.
When you pasted in the code are you sure you pasted this line exactly as-is?:
?>show a message<?php
The only thing I can think of is that the code on the server is using a short open tag <? but SHORT_OPEN_TAG is turned off in the server's php.ini, for example :
?>show a message<?
I had a similiar issue with my file. This was throwing an error in my file:
<?php } ?>
This may seem silly, I added a comment which seemed to fix the error:
<?php
//
}
?>

Categories