Avoid notice in PHP display_error

Asked

Viewed 1,193 times

1

I have this snippet of code, but many others follow the same style:

if(!$_COOKIE[SITEINDEX]) {
    setcookie(SITEINDEX, max(explode('/', dirname($_SERVER[PHP_SELF]))));
    define(siteindex, '/' . max(explode('/', dirname($_SERVER[PHP_SELF]))) . '/');
} else {
    define(siteindex, '/' . $_COOKIE[SITEINDEX] . '/'); 
}

It causes when loading the page, create a cookie with the first URL URI so it doesn’t keep changing every time you press F5 or have to redo every time you press F5 and in this case I can’t assign any value to this cookie siteindex before the if or it would have no logic and that’s where the problem comes in. PHP displays notices "siteindex" not defined and prevents the application from being loaded, not only with this excerpt, as I have if of this style in several places, which does not have the variable declared for it to know if it exists and, if yes, do something, if not, only show, on online servers that does not have display_error they work but on localhost that has display_error they don’t work. I didn’t want to turn off the dislay_error, but fix it without declaring the variable with some value that will prevent it from working.

How would you do that?

An example from Notices is this

<?php
    define(siteprot, $_SERVER[HTTPS] ? 'https://' : 'http://');
    define(sitehost, $_SERVER[HTTP_HOST]);
    define(siteuri,  $_SERVER[REQUEST_URI]);

    if(!$_COOKIE[SITEINDEX]) {
        setcookie(SITEINDEX, max(explode('/', dirname($_SERVER[PHP_SELF]))));
        define(siteindex, '/' . max(explode('/', dirname($_SERVER[PHP_SELF]))) . '/');
    } else {
        define(siteindex, '/' . $_COOKIE[SITEINDEX] . '/'); }

    define(siteurl, siteprot . sitehost . siteindex);
    define(secret, '/' . max(explode('/', dirname($_SERVER[PHP_SELF]))) . '/');
?>

Notice: Use of Undefined Constant siteprot - assumed 'siteprot' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 2

Notice: Use of Undefined Constant HTTPS - assumed 'HTTPS' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 2

Notice: Use of Undefined Constant sitehost - assumed 'sitehost' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 3

Notice: Use of Undefined Constant HTTP_HOST - assumed 'HTTP_HOST' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 3

Notice: Use of Undefined Constant siteuri - assumed 'siteuri' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 4

Notice: Use of Undefined Constant REQUEST_URI - assumed 'REQUEST_URI' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php online 4

Notice: Use of Undefined Constant SITEINDEX - assumed 'SITEINDEX' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 6

Notice: Undefined index: SITEINDEX in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 6

Notice: Use of Undefined Constant SITEINDEX - assumed 'SITEINDEX' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 7

Notice: Use of Undefined Constant PHP_SELF - assumed 'PHP_SELF' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 7

Warning: Cannot Modify header information - headers already sent by (output Started at D: Roberto Monteiro Desktop Server 57 www qrcode index.php:2) in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 7

Notice: Use of Undefined Constant siteindex - assumed 'siteindex' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 8

Notice: Use of Undefined Constant PHP_SELF - assumed 'PHP_SELF' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 8

Notice: Use of Undefined Constant siteurl - assumed 'siteurl' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 12

Notice: Use of Undefined Constant secret - assumed 'secret' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 13

Notice: Use of Undefined Constant PHP_SELF - assumed 'PHP_SELF' in D: Roberto Monteiro Desktop Server 57 www qrcode index.php on line 13 /qrcode//qrcode/

2 answers

1

The problem is the lack of quotes to delimit strings.

define(siteprot, $_SERVER[HTTPS] ? 'https://' : 'http://');
define(sitehost, $_SERVER[HTTP_HOST]);
define(siteuri,  $_SERVER[REQUEST_U...

It should be like this

define('siteprot', $_SERVER['HTTPS'] ? 'https://' : 'http://');
define('sitehost', $_SERVER['HTTP_HOST']);
define('siteuri',  $_SERVER['REQUEST_U...

You should fix this in everything. Example

$_SERVER[PHP_SELF] // errado

$_SERVER['PHP_SELF'] // correto

You can still wonder, why it worked in another environment?

The answer is that the previous environment was misconfigured or was deactivated intentionally and does not necessarily mean that whoever did it did not know what they were doing.

PHP, when configured to omit these errors, automatically transforms the undefined constant into a string and thus, even with the wrong code semantics, everything works smoothly.

If you want to solve quickly, just disable Warning of this error level. But the recommended is to fix everything by delimiting with quotes.

ini_set('error_reporting', E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED);

This disables "notice" and break type errors and disables Strict and deprecated types.

Stressing, only use this as a solution if you are in urgency and do not have time to fix everything.

0


The mistake "siteindex" not defined happens because you are not checking whether this variable was previously declared. With this PHP does not recognize the contents of this variable.

What you can do is check if it exists, if there is no define it first:

if(!defined('SITEINDEX')){
   define("SITEINDEX","alguma coisa");
}

In your case, you need to create a cookie with a fixed name, otherwise PHP will not be able to detect which value SITEINDEX:

if( empty($_COOKIE['SITEINDEX']) ) {
    setcookie('SITEINDEX', max(explode('/', dirname($_SERVER[PHP_SELF]))));
    define('SITEINDEX', '/' . max(explode('/', dirname($_SERVER[PHP_SELF]))) . '/'); 
} else {
    define('SITEINDEX', '/' . $_COOKIE['SITEINDEX'] . '/'); }
}

With this PHP will recognize that when the COOKIE is not set at the position 'SITEINDEX' and the content is empty, it will fill in correctly and should not generate the variable definition problem.

Error Handling: Check this Post

  • yeah, I know I could trade everything for isset or Empty but it’s over 60,000 lines, I switched some and it works great, but then again, I didn’t want to have to use something I stopped using which is Empty and isset, but Valew

  • added an example of notices if connect display error

  • actually works in any version of php I am currently in 7 but was in 5, is that with error off it works but when linking is that of error, but I will try with these you put

  • really, with these (E_ERROR | E_WARNING | E_PARSE) it allowed the execution of the script without displaying the notices, has it possible not to display only notice or need to stipulate everyone I want to display except notice? because I want to see all the errors except the ones that aren’t exactly a mistake

  • I updated the answer by adding at the end the part of the error re-port

  • cool just put what we don’t want

  • I don’t know why, but the ones I used only worked like this E_ALL & ~E_NOTICE | E_STRICT

  • I think it’s best to review this issue of errors Why use error_reporting with display_errors and display_startup_errors? ;)

  • @Guilhermenascimento Inkei in response to the other Post

  • @Guilhermenascimento Corrected!

  • @Everson is not really a matter of correction is just a feature tip to simplify something, both ways are correct :)

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.