有没有办法将cookies插入数据库?

发布于 2024-10-10 08:39:57 字数 484 浏览 0 评论 0原文

我需要将用户名与数据库中的值进行匹配,因此我想插入保存在 cookie 中的用户名。该功能非常简单。 cookie 存储正确,我可以回显它。我的插入脚本也可以工作,因为我可以插入其他东西。但由于某种原因我无法插入 cookie 值。

这几乎就是我想做的事情:

$username = $_COOKIE['username'];

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('error');
$query1 = "INSERT INTO Gallery (username) VALUES('$username')";
$data1 = mysqli_query ($dbc, $query1) or die('error1');
mysqli_close($dbc);

我缺少什么吗?我尝试使用会话,但没有运气。

我还使 cookie 在整个域中都可访问。

I need to match up a users name with a value inside a database, so I want to insert the users name that is saved in a cookie. The function is pretty simple. The cookie is stored correctly and I can echo it. My insert script also works cause I can insert other things. But for some reason I cannot insert a cookies value.

This is pretty much what I'm trying to do:

$username = $_COOKIE['username'];

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('error');
$query1 = "INSERT INTO Gallery (username) VALUES('$username')";
$data1 = mysqli_query ($dbc, $query1) or die('error1');
mysqli_close($dbc);

Is there something I'm missing? I tried using sessions, but no luck.

I also made the cookie accessible throughout the whole domain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-10-17 08:39:57

这里(至少)存在两个问题。

  1. 您将 cookie 的值复制到名为 $user 的变量,但使用名为 $username 的变量来尝试将数据插入数据库
  2. 您不执行任何类型的操作在 SQL 查询中使用之前,对 cookie 数据(浏览器提供的数据,因此受到污染)进行完整性检查。这是小鲍比桌的邀请。

There are (at least) two problems here.

  1. You copy the value of the cookie to a variable called $user but use a variable called $username to try to insert data into the database
  2. You don't perform any kind of sanity check on the cookie data (which is data provided by the browser and thus tainted) before using in an SQL query. This is an invitation to Little Bobby Tables.
染柒℉ 2024-10-17 08:39:57

可能尝试将您的查询更改为此...

$query1 = "INSERT INTO Gallery (username) VALUES(" . mysql_escape_string($_COOKIE['username']) . ")";

Possibly try changing your query to this...

$query1 = "INSERT INTO Gallery (username) VALUES(" . mysql_escape_string($_COOKIE['username']) . ")";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文