Warning: trim() expects parameter 1 to be string,

2020-03-31 08:04发布

问题:

i have php script, when i executed this code, i got warning

Warning: trim() expects parameter 1 to be string, array given in home/public_html

i've added antixss function for security vulnerability , like this code below. how do i resolve this warning?

<?php
$email_to = ""; 
$email_subject = ""; 
$success = ""; 

function antixss($value)
{
    $xss = htmlspecialchars(trim($value));
    return $xss;
}


if(!isset($_POST['First_Name']) ||
   !isset($_POST['Last_Name']) 
    {
    died('Sorry, there appears to be a problem.');      
}
$first_name =antixss($_POST['First_Name']); 
$last_name = antixss($_POST['Last_Name']);    

how do i resolve this warning? thanks

回答1:

It appears the error is in your html and not in the php.

If $_POST['First_Name'] is an array while it should not be, it is likely your html looks something like:

<input name="First_Name[]" ...>
                       ^^ here, remove this


回答2:

First of all, your IF statement is all messed up

if(!isset($_POST['First_Name']) || !isset($_POST['Last_Name']) || 
    {
    died('Sorry, there appears to be a problem.');      
}

You start it with a bracket, you end it with an OR sign (||). First repair that, to something like this:

if(!isset($_POST['First_Name']) || !isset($_POST['Last_Name'])){
    died('Sorry, there appears to be a problem.');
}

Then, if the problem persists type in var_dump($value) and/or var_dump($_POST) inside the antixss() function so it's something like this

function antixss($value){
    var_dump($value); var_dump($_POST); exit();
    $xss = htmlspecialchars(trim($value));
    return $xss;
}

And tell us what the output is.



标签: php warnings xss