Define URL in PHP File throw error in featured PHP Version - php

I have a file called web.php inside that file I store code for define base URL address, assets folder.... File have next code:
<?php
define('BASE', 'https://example.com/');
define('ASSETS', BASE . 'assets/');
?>
So if we call function from above <?php echo BASE; ?>home.php i get error that says, check above.
All works fine but get an error that says, you need to change `BASE` it will throw an error in featured PHP Versions. I try to define different names, but same error. Can someone explain to me, how I can resolve this issue?

Im trying to repeat your Error
And as i see there no Errors at all
My PHP version is 7.4.3
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define('BASE', 'https://example.com/');
define('ASSETS', BASE . 'assets/');
echo BASE;
echo "No errors";
Is it really php throw or may by it comes from your Framework?

Related

HTTP ERROR 500 If their is any Error in PHP Code

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");
?>

Error reporting in PHP withi '.ini' files

So basically I have tried to setup an error reporting system that logs any PHP, HTML or any other error into a text file called 'errors.txt' located in a specific directory. However, the file is not being created.
Here is the code:
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
ini_set('log_errors',TRUE);
ini_set('error_log','/xampp/xampp-portable-win32-1.8.3-2-VC11/xampp/htdocs/EstateAgent/logs/errors.txt');
ini_set('display_errors',FALSE);
The errors are still be displayed to the user and they error log isn't being created inside of the directory specified.
I have also tried changing the standard values in my 'php.ini' file as suggested on other websites and I have still had no luck.
Any help would be greatly appreciated!
Thanks :)
For anyone who has similar problem - make sure you don't have any other error_reporting and ini_set in other files you use in your project. In those cases it may seem that error reporting is not working properly but in fact it this.
Try to change:
ini_set('log_errors',TRUE);
into
ini_set('log_errors','1');
and
ini_set('display_errors',FALSE);
into
ini_set('display_errors','0');
If it doesn't help, add at the very top beginning of your file:
<?php
error_reporting(0);
ini_set('display_errors','0');
echo $testVariable;
echo "Just testing";
?>
and check if any warning about this variable is displayed before "Just testing".
Now change above code into:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
echo $testVariable;
echo "Just testing";
?>
and check if any warning about this variable id displayed before "Just testing".
As above 2 sample codes works as expacted, it's likely that those settings are change in other php files of user or php libraries used by user. You should scan your code and libraries code do track where changes are made and try to solve this issue either removing your code changing your desired reporting or commenting / setting properly (if there is such possibility in constructor or method) reporting in external library
You need to provide a fullpath for the 'error_log' property:
<?php
ini_set('error_log', 'c:/xampp/logs/php_error.log');
?>
You need to make sure that the following folder exists and is "writable": c:/xampp/logs/
If you're using Linux, then the fullpath will look like this: /xampp/logs/php_error.log
Also, use the following:
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
ini_set('log_errors', '1');
?>

Use of undefined constant notice

I've read up on this problem a little, mainly from articles on here. It appears that it is typically generated by someone trying to do $foo[bar] instead of $foo['bar'], but I have checked several times around where the error is occuring in my script and this is not the case.
I have a php file, which contains the following script:
define("APP_PATH", "http://localhost/foobar");
require_once APP_PATH . "/classes/controller.php";
This appears to be executing fine. Inside controller.php I have this code:
require_once APP_PATH . "/classes/factory.class.php";
$factory = new factory;
This, to my knowledge, should execute perfectly fine. However, I am getting the following error: Notice: Use of undefined constant APP_PATH - assumed 'APP_PATH' in C:\wamp\www\foobar\classes\controller.php on line 3. Line 3 is the call to require_once.
I have checked, I am fairly sure that this should not be causing an error. I've also checked my spelling. The same line also triggers a warning and a fatal error about failing to open the stream, it is returning APP_PATH/classes/factory.class.php as the path.
Any help would be much appreciated.
The problem is that you are including from a remote place.
Let's say that APP_PATH.'/classes/controller.php' is as follows:
<?php
class Some_Controller extends Controller {
// ...
}
echo 'TEST!';
When you include it through HTTP the PHP interpreter will parse the file before sending it back to be included:
<?php
include APP_PATH.'/classes/controller.php';
// This will print "TEST!" to the page because PHP has
// parsed the code and the only thing in the output
// buffer is "TEST!" (from the "echo 'TEST!';")
In order to fix this you need to include from the local environment. In Linux it would be some like
/path/to/web/classes/controller.php
In Windows it would be something like:
C:\path\to\web\classes\controller.php

Force Smarty to show PHP errors

I've been working with PHP for a while, but fairly new to Smarty.
I'm working with Prestashop and I've noticed Smarty seems to eat up all PHP errors - when there's an error in the PHP code, the .tpl file just outputs a blank page. I've been trying but I can't get Smarty to display whatever the PHP code outputs, even if there's an error.
PHP error reporting is set to show errors.
So, for instance, let's say this is the example.php file:
<?php
//included classes etc go here, irrelevant for this issue
error_reporting(E_ALL ^ E_NOTICE);
echo obvious wrong syntax"
?>
This file is connected to example.tpl which fits the output in a template block.
Obviously, it should throw an error. How do I make Smarty actually display that error?
To activate debug mode, go to config/config.inc.php
Find the following lines and chage off to on for the first one and set to true the second
/* Debug only */
#ini_set('display_errors', 'on');
define('_PS_DEBUG_SQL_', true);
This will display PHP and SQL errors (this would probably be enough for you to resolve "blank page").
There is also a blog post on prestashop site about p() and d() methods and how to track exceptions
To activate templates debug in Prestashop version older than 1.5,go to config/smarty.config.inc.php
Find the following line and set it to true
$smarty->debugging = true;
When you refresh your page, themes/debug.tpl should be rendered.
To activate templates debug in Prestashop 1.5+ you can enable Smarty debugging via Admin panel
Preferences > Performance > Smarty
and set Always open console but console will be opened for everyone ( not good for live site :) )
or set Open console with URL parameter (SMARTY_DEBUG) and add ?SMARTY_DEBUG to the end of your URL to see console
Hope this helps.
I've seen #Sergei Guk's answer and off-course, it's a pretty good answer. However, prestashop has since released version 1.6.
So if you want to show all the errors in prestashop v 1.6.0.6, you just need to go config/defines.inc.php
Replace define('_PS_MODE_DEV_', false); with define('_PS_MODE_DEV_', true);
What it actually does is set a constant and in the next line it checks if "_PS_MODE_DEV_" is true then it will show all kinds of errors in prestashop
if (_PS_MODE_DEV_)
{
#ini_set('display_errors', 'on');
#error_reporting(E_ALL | E_STRICT);
define('_PS_DEBUG_SQL_', true);
}
else
{
#ini_set('display_errors', 'off');
define('_PS_DEBUG_SQL_', false);
}
I have tested it and it works fine.
Set the $error_reporting variable.
See http://www.smarty.net/docsv2/en/variable.error.reporting.tpl

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.

Categories