Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 Cookie安全问题_Php_Sql_Database_Security_Cookies - Fatal编程技术网

PHP Cookie安全问题

PHP Cookie安全问题,php,sql,database,security,cookies,Php,Sql,Database,Security,Cookies,我有下面的代码,它展示了一个“每日词汇”, 由于我对php编码比较陌生,所以我想确保没有 如何从cookie值中从数据库中进行选择的安全问题。谢谢 if ($word_of_the_day) { $wotd = $wpdb->get_results("SELECT term,definition FROM glossary WHERE term = '{$word_of_the_day}'"); foreach ($wotd as $term) { } } elseif ($_

我有下面的代码,它展示了一个“每日词汇”, 由于我对php编码比较陌生,所以我想确保没有 如何从cookie值中从数据库中进行选择的安全问题。谢谢

if ($word_of_the_day) {
   $wotd = $wpdb->get_results("SELECT term,definition FROM glossary WHERE term = '{$word_of_the_day}'");
   foreach ($wotd as $term) { }
}
elseif ($_COOKIE['WOTD']) {
   $word_of_the_day = htmlspecialchars(addslashes($_COOKIE['WOTD']));
   $wotd = $wpdb->get_results("SELECT term,definition FROM glossary WHERE term = '{$word_of_the_day}'");
   foreach ($wotd as $term) { }
}
else {
   $wotd = $wpdb->get_results("SELECT term,definition FROM glossary ORDER BY RAND() LIMIT 1");
   foreach ($wotd as $term) {
      setcookie("WOTD", $term->term, time()+86400);
   }
}

您应该检查:“转义字符串中的特殊字符,以便在SQL语句中使用”。您不必手动执行使用
htmlspecialchars
addslashes
所执行的操作。你熟悉安全风险吗?如果您在
SELECT
语句中包含的变量,
$word\u of_the_day
来自用户,则可能存在SQL注入问题。

您应该检查:“转义字符串中用于SQL语句的特殊字符”。您不必手动执行使用
htmlspecialchars
addslashes
所执行的操作。你熟悉安全风险吗?如果您在
SELECT
语句中包含的变量
$word\u of_the_day
来自用户,那么您就有一个潜在的SQL注入问题。

当天的$word\u从何而来?如果它来自用户输入,那么您可以接受SQL注入。

每日$word\u从何而来?如果它来自用户输入,那么您可以接受SQL注入。

如果当天的$word\u来自用户输入,那么这就是您的第一个问题。使用前请执行以下操作:

$word_for_the_day = mysql_real_escape_string($word_for_the_day);

你的饼干看起来还不错。htmlspecialchars()和addslashes()调用,在您使用它们的上下文中,似乎不易受到SQL注入或XSS攻击。

如果当天的$word来自用户输入,那么这就是您的第一个问题。使用前请执行以下操作:

$word_for_the_day = mysql_real_escape_string($word_for_the_day);

你的饼干看起来还不错。htmlspecialchars()和addslashes()调用在您使用它们的上下文中似乎不易受到SQL注入或XSS攻击。

addslashes非常弱。首先,通过mysql\u escape\u字符串从数据库中运行所有查询,以防止sql注入。这只是最基本的

if($word_of_the_day){

$word_of_the_day = mysql_escape_string($word_of_the_day); 
 $wotd = $wpdb->get_results ("SELECT term,definition FROM glossary WHERE term = '{$word_of_the_day}'");
而且,不管您编写的代码有多安全,cookie通常都不是很安全。为了获得更安全的解决方案,我建议您使用PHP会话($\u会话)。您可以将变量存储在此超全局变量中,它将在页面加载之间保持不变


在那之后,如果你真的想要阻止会话劫持或中毒的话,你可能会想保护它。

addslashes非常弱。首先,通过mysql\u escape\u字符串从数据库中运行所有查询,以防止sql注入。这只是最基本的

if($word_of_the_day){

$word_of_the_day = mysql_escape_string($word_of_the_day); 
 $wotd = $wpdb->get_results ("SELECT term,definition FROM glossary WHERE term = '{$word_of_the_day}'");
而且,不管您编写的代码有多安全,cookie通常都不是很安全。为了获得更安全的解决方案,我建议您使用PHP会话($\u会话)。您可以将变量存储在此超全局变量中,它将在页面加载之间保持不变


在此之后,如果你真的想要它,你可能想保护会话劫持或中毒。

你可以考虑的另一个选项是存储单词的ID,而不是cookie中的单词本身。这样,它只能是一个整数。当然,使用这个词也很好,只要你先,我只想提供另一个选项。

你可以考虑的另一个选项是存储单词的ID,而不是cookie中的单词本身。这样,它只能是一个整数。当然,使用这个词也可以,只要您先使用它,我只想提供另一个选项。

最安全的方法之一是使用函数,函数实现参数:

$db = new PDO('mysql:host=hostname;dbname=defaultDbName', 'username', 'password');
$stmt = $db->prepare('SELECT term,definition FROM glossary WHERE term = :wotd');
if ($stmt) {
    if ($stmt->execute(array(':wotd' => $word_of_the_day))) { //This is safe for any input method
        $info = $stmt->fetchAll();
        foreach($info as $row) {
            //Whatever
        }
    }
}

PDO驱动程序根据表中的数据类型进行正确的转义/引用。

最安全的方法之一是使用函数,函数实现参数:

$db = new PDO('mysql:host=hostname;dbname=defaultDbName', 'username', 'password');
$stmt = $db->prepare('SELECT term,definition FROM glossary WHERE term = :wotd');
if ($stmt) {
    if ($stmt->execute(array(':wotd' => $word_of_the_day))) { //This is safe for any input method
        $info = $stmt->fetchAll();
        foreach($info as $row) {
            //Whatever
        }
    }
}
PDO驱动程序根据表中的数据类型进行正确的转义/引用