Avoid notice in PHP display_error

1

I have this code snippet, 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 the cookie to load with the first URL URI so it does not change every time you press F5 or you have to redo every time you press F5, and in this In case, I can not assign any value to this cookie siteindex before the if happens or it would not have 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 snippet, since I have if of this style in several places, has the variable declared for it to know if it exists and, if so, to do something, if not, only show, in online servers that do not have display_error they work but in localhost that has display_error they do not work. I did not want to turn off dislay_error , but I would heal this without declaring the variable with some value that would prevent it from working.

How would you do that?

An example of 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 on line   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: Can not 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 /

    
asked by anonymous 05.06.2017 / 19:18

2 answers

0

The error " siteindex" not defined happens because you are not checking if this variable has been previously declared, so PHP does not recognize the contents of this variable."

What you can do is check if it exists, if it does not exist, set 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 the value of 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 COOKIE is not set to 'SITEINDEX' and the content is empty, it will fill correctly and should not generate the variable definition problem.

Error Handling: Check this Post

    
05.06.2017 / 20:01
1

The problem is the lack of quotation marks 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 at all. Example

$_SERVER[PHP_SELF] // errado

$_SERVER['PHP_SELF'] // correto

You can still ask, why did it work in another environment?

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

When PHP is set to skip these errors, it automatically transforms the undefined constant into a string, so even with the wrong code semantics, everything works without problems.

If you want to resolve fast, simply disable the warning of this level of error. But the recommended one is to fix everything by delimiting with quotes.

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

This disables the "notice" and break errors in the strict and deprecated types.

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

    
03.07.2017 / 19:17