从php隐藏url参数

斯旺维尔
<a href="rate.php?winner=<?=$images[0]->image_id?>&loser=<?=$images[1]->image_id?>"></a>

这是主页 index.php

这是我的主页。在上传到 php 文件之前,可以使用检查元素更改参数,这是一个问题。

这是 rate.php

<?php


include('mysql.php');
include('functions.php');


// If rating - update the database
if ($_GET['winner'] && $_GET['loser']) {


// Get the winner
$result = $conn->query("SELECT * FROM images WHERE image_id = ".$_GET['winner']." ");
$winner = $result->fetch_object();


// Get the loser
$result = $conn->query("SELECT * FROM images WHERE image_id = ".$_GET['loser']." ");
$loser = $result->fetch_object();


// Update the winner score
$winner_expected = expected($loser->score, $winner->score);
$winner_new_score = win($winner->score, $winner_expected);
    //test print "Winner: ".$winner->score." - ".$winner_new_score." - ".$winner_expected."<br>";
$conn->query("UPDATE images SET score = ".$winner_new_score.", wins = wins+1 WHERE image_id = ".$_GET['winner']);


// Update the loser score
$loser_expected = expected($winner->score, $loser->score);
$loser_new_score = loss($loser->score, $loser_expected);
    //test print "Loser: ".$loser->score." - ".$loser_new_score." - ".$loser_expected."<br>";
$conn->query("UPDATE images SET score = ".$loser_new_score.", losses = losses+1  WHERE image_id = ".$_GET['loser']);


// Insert battle
$conn->query("INSERT INTO battles SET winner = ".$_GET['winner'].", loser = ".$_GET['loser']." ");


// Back to the frontpage
header('location: /');

}


?>

我只是希望在将数据发送到 php 文件时可以修改参数

马格努斯·埃里克森

您需要在代码中添加一些额外的验证/验证。这与您使用 GET 还是 POST 传递数据无关。

您可以为每个呼叫设置一个会话,该会话定义允许用户传递的 ID。它的工作原理类似于基本的 CSRF 保护:

它可能类似于以下内容:

在投票页面:

<?php 
// Start sessions (should always be in the top
session_start();

// Get the image id's some how. Let's use these as an example
// This could just as well be strings or what ever it is you're posting
$image1 = 1;
$image2 = 2;

// Generate a pseudo random token
$token = bin2hex(random_bytes(16));

// Store the image references in a session with the token as name
$_SESSION[$token] = [$image1, $image2];
?>

// HTML that sends the image references and the token (important)

在接收数据的页面上:

<?php
// Again, start sessions;
session_start();

// Check that all parameters are there
if (!isset($_POST['winner'], $_POST['loser'], $_POST['token'])) {
    die('Invalid request');
}

$winner = $_POST['winner'];
$looser = $_POST['loser'];
$token  = $_POST['token'];

// Check if the session is set. If not, then the call didn't come from your page
if (!$token || empty($_SESSION[$token])) {
    die('We have a CSRF attack');
}

// Check if both image references exists in session. If not, then someone have change the values
if (!in_array($winner, $_SESSION[$token]) || !in_array($loser, $_SESSION[$token])) {
    die('Invalid image references! We have a cheater!');
}

// Remove the token from the session so the user can't repeat the call
unset($_SESSION[$token]);

// Do your DB stuff using Prepared Statements.

这是一个未经测试的示例,因此它可能无法直接使用,但它向您展示了一种可以使用的技术。

重要的

您目前对SQL 注入持开放态度,应该真正使用准备好的语句而不是连接您的查询。特别是因为您根本没有逃避用户输入!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

隐藏 URL 参数并读取它们

来自分类Dev

隐藏URL中的参数供用户查看

来自分类Dev

我可以从URL隐藏查询参数吗?

来自分类Dev

如何根据url参数隐藏/显示元素

来自分类Dev

发出请求时从URL隐藏功能参数

来自分类Dev

GOOGLE MAPS URL参数隐藏标签

来自分类Dev

.htaccess 在存在时隐藏 url 参数?

来自分类Dev

jQuery URL 参数隐藏列表项

来自分类Dev

变量的 PHP URL 参数

来自分类Dev

PHP-隐藏网址(GET)参数

来自分类Dev

PHP-隐藏网址(GET)参数

来自分类Dev

在php中抓取url参数

来自分类Dev

用PHP获取URL参数

来自分类Dev

获取php中的url参数

来自分类Dev

PHP无法从URL获取参数

来自分类Dev

在php中抓取url参数

来自分类Dev

htaccess重写URL php参数

来自分类Dev

PHP URL参数作为命令

来自分类Dev

带参数的 PHP 路由 URL

来自分类Dev

隐藏侧面板是否有Google Map URL参数?

来自分类Dev

CFWheels:使用隐藏的参数重定向到URL

来自分类Dev

在URL中隐藏spring session bean参数值

来自分类Dev

如何使用Spring MVC在URL中隐藏请求参数值

来自分类Dev

更改Struts 2中的显示URL以隐藏请求参数

来自分类Dev

Phalcon-如何使用.htaccess隐藏URL中的获取参数

来自分类Dev

如何在Symfony2中的URL中隐藏参数

来自分类Dev

骨干路由器如何从url隐藏参数

来自分类Dev

使用URL重写在URL中隐藏PHP变量

来自分类Dev

.htaccess从网址中删除index.php并隐藏参数键