Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在web应用程序中存储和转义数据?_Php_Mysql_Xhtml_Escaping - Fatal编程技术网

Php 如何在web应用程序中存储和转义数据?

Php 如何在web应用程序中存储和转义数据?,php,mysql,xhtml,escaping,Php,Mysql,Xhtml,Escaping,我正在寻找一种更正式、正确和独立的解决方案,将数据存储到数据库和web应用程序中。几个月来我听到了很多意见,但我不确定巫婆是否正确。计划让我的webapp更具可伸缩性我认为在数据库中存储真实和未经扫描的数据更方便,你同意吗 现在我正在使用这个解决方案,一个带有PHP5、XHTML和MySQLi伪代码的示例。 我使用的是无处不在的UTF-8和无魔法引号 数据处理: <?php // catching data from a simple input form $id_profile

我正在寻找一种更正式、正确和独立的解决方案,将数据存储到数据库和web应用程序中。几个月来我听到了很多意见,但我不确定巫婆是否正确。计划让我的webapp更具可伸缩性我认为在数据库中存储真实和未经扫描的数据更方便,你同意吗

现在我正在使用这个解决方案,一个带有PHP5、XHTML和MySQLi伪代码的示例。 我使用的是无处不在的UTF-8和无魔法引号

数据处理:

<?php
// catching data from a simple input form
$id_profile         = $_POST['id_profile'];
$details['name']        = $_POST['name'];
$details['text']            = $_POST['text'];

// filter $details
foreach($details as &$item) {   $item = trim($item); /* some stuff and other filters but no addslashes() */ }

// Preparing and doing the query
$stmt = $mysqli->prepare("
    UPDATE      profiles
    SET         name = ?, text = ?
    WHERE       id_profile = ?  ");
$stmt->bind_param('ssi', $details['name'], $details['text'], $id_profile);
$stmt->execute();
// [...]

我在PHP中使用扩展名。这允许我在PDO的
quote()
方法中包装输入参数:

$first_name = $this->pdo->quote($_POST['first_name']);
$sql = "INSERT INTO contacts (first_name) VALUES ('$first_name');
$res = $this->pdo->exec($sql);
...
或者对准备好的语句使用参数替换

$sql = "INSERT INTO contacts (first_name, last_name, email) VALUES (?,?,?)";
$smt = $this->pdo->prepare(array(
    $_POST['first_name'],
    $_POST['last_name'],
    $_POST['email']
));
在获取数据库记录时,我确保字符串包装在
htmlspecialchars()
中,整数包装在
intval()
中,尽管这可能是不可伸缩的。您可能希望创建一个函数或方法,用于从数据库中选择行并自动应用任何转换。一个快速而肮脏的例子:

/**
 * @param string $table name
 * @param integer $id of record
 * @return array of returned objects
 * @abstract
 */
function select_from_db() {
    global $pdo;
    $id = intval($id);
    $row = array();
    $sql = "SELECT * FROM $table WHERE id = '$id'";
    $res = $pdo->query($sql);
    while ($row = $res->fetchObject()) {
        foreach ($row as $field => $value) {
            $row[$field] = htmlspecialchars($value);
        }
    }
    return $row;
}

// example usage:
$res = select_from_db('contacts', 1);

你认为我应该使用htmlspecialchars()而不是htmlentities()?谢谢取决于是否要将所有字符转换为等效实体。如果像上面那样在包装器函数中使用,您可以随意替换它们,并使这些更改应用于整个应用程序。
/**
 * @param string $table name
 * @param integer $id of record
 * @return array of returned objects
 * @abstract
 */
function select_from_db() {
    global $pdo;
    $id = intval($id);
    $row = array();
    $sql = "SELECT * FROM $table WHERE id = '$id'";
    $res = $pdo->query($sql);
    while ($row = $res->fetchObject()) {
        foreach ($row as $field => $value) {
            $row[$field] = htmlspecialchars($value);
        }
    }
    return $row;
}

// example usage:
$res = select_from_db('contacts', 1);