gpt4 book ai didi

php - 保护 Joomla SQL 连接

转载 作者:行者123 更新时间:2023-11-30 22:33:55 25 4
gpt4 key购买 nike

我在我的论坛上使用这段代码来“检查重复的 ip”:

<?php 

$db = JFactory::getDBO();

$pid = $forum['Post']['topic_id'];

$ipaddress = $forum['User']['ipaddress'];

$query = 'SELECT count(ipaddress) FROM #__forum_comments WHERE ipaddress = "' . $ipaddress . '" AND pid = ' . (int) $pid;

$count_ip = $db->setQuery($query)->loadResult();

if($count_ip >= 2){
echo 'Your ip repeated';
}

?>

我问过编写这段代码的开发人员有关 SQL 注入(inject)的保护,他们是这样说的:

The native Joomla method is JFactory::getDBO() which is the right way to do it. You can try using the escape method to see if that works: $ipaddress = $db->escape($forum['User']['ipaddress']); There's no need to escape the $pid because casting it to an integer is enough. In fact, since both values come straight from the database it's pretty safe to assume that there cannot be any sort of mysql injection here even without escaping.

我需要做这样的事情吗?

//escaping to prevent sql injection
$pid = mysqli_real_escape_string($mysqli, $review['Review']['listing_id']);
$ipaddress = mysqli_real_escape_string($mysqli, $review['User']['ipaddress']);

最佳答案

你应该这样做:

$jinput = JFactory::getApplication()->input;
$pid = $jinput->get('pid', '', 'integer');
$ip = $jinput->get('ip', '', 'string');

然后您可以运行您的查询。

关于php - 保护 Joomla SQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com