Avoid using isset in PHP when accessing $ _POST, $ _GET and other variables?

how can I prevent PHP from returning an Undefined error every time a variable is checked, if it contains content and that some variable is not used yet? In my previous setup, I can check $ _POST ['email'] even if I haven’t added anything yet. It simply returns an empty or empty result. That I want my PHP setup to work, but for the life of me, I cannot find how to configure it. :(

Example:

<?php
if ($_POST['info'] != '') {
  // do some stuff
}
?>
<form method="post">
<input type="text" name="info" />
<input type="submit" />
</form>

When you use the above script on one PHP page and run it in my current setup, it returns an Undefined error . In my previous setup, this script worked like a charm. Can anyone highlight this issue. If you need more information, just say it and I will try to add more information to this post.

I know about isset () validation , but I don't want to use it in all my scripts. I want it not to be restrictive on variables.

+3
source share
6 answers

, , . PHP, /, . :

ini_set('display_errors', 0);

, - , , , , .

, ,

<?php
if (isset($_POST['info'])) {
  // do some stuff
}
?>
<form method="post">
<input type="text" name="info" />
<input type="submit" />
</form>
+3

empty, true, ​​ (.. , 0, NULL, FALSE ..):

if(!empty($_POST['variable'])){
    /* ... */
}

, , , , false ( ) , .

+3

:

if( @$_POST['variable'] == 'value' )
{
     ...
}

PHP , , .

0

? , error_reporting PHP.INI.

"error_reporting = E_ALL;" (, E_ALL) , , .

- , , PHP CP -.

, ; /

ps- , Apache. , Ubunto ( , ), CentOS :

/sbin/service httpd restart
0

SET, form.

empty.

if(!empty($_POST['var']))

0

$_POST vars, :

if (isset($_POST['info']))
  $info = $_POST['info'];
... etc....

?

function setPostVar( &$v ) {
    $trace = debug_backtrace();
    $vLine = file( __FILE__ );
    $fLine = $vLine[ $trace[0]['line'] - 1 ];
    preg_match( "#\\$(\w+)#", $fLine, $match ); 
    eval("\$v = isset(\$_POST[\"$match[1]\"])?\$_POST[\"$match[1]\"]:'';");
}

, , POST

ie. $Foo = $_POST['Foo'];

:

setPostVar($Foo); // equivalent to if(isset($_POST['Foo']) $Foo = $_POST['Foo'];
setPostVar($Bar); // ditto
setPostVar($Baz); // ditto
0

Source: https://habr.com/ru/post/1768876/


All Articles