【问题标题】:Pass php $_GET to flash player xml file将 php $_GET 传递给 flash player xml 文件
【发布时间】:2011-09-07 12:15:15
【问题描述】:

我在页面 x.php?user=john 上使用 swfobject 嵌入了一个 Flash 播放器。播放器调用xml文件content.php获取结果。在 content.php 我有 $_GET['user']。我正在尝试从 url id 获取用户名并基于此获取结果。但是,我收到 500 错误。我不认为 content.php 能够访问 user 变量。如果我只是输入用户名“john”而不是 $_GET['user'] 那么它可以工作。我怎样才能让它与 $_GET['user']

一起使用

XML 文件 content.php 看起来像这样

$sql = 'SELECT a.videote as videote, b.user_name as user_name'. 
' FROM '.$video.' as a,'.$users.' as b'.
' where b.user_name=$_GET['user'] and... //if i replace $_GET['user'] with john then it works

在x.php中flash是这样嵌入的

<script type="text/javascript">
    var flashvars = {};
    var so = new SWFObject("play2.swf", "sotester", "1000", "400", "8", "#000000");
    so.addParam("allowFullScreen", "true");
    so.addParam("scale", "noscale");
    so.addParam("menu", "false");
    so.write("flashcontent");
</script> 

我的 Player Actionscript 当然指向 content.php,这不是这里的问题

xmlData.load("contentp.php");

【问题讨论】:

  • 你能贴出相关的代码吗?还有,加载x.php或content.php时是否返回服务器错误?
  • @GargantuChe 检查更新后的帖子
  • @GargantuChe 也 500 错误是由 content.php 而不是 x.php 引起的

标签: php javascript flash actionscript get


【解决方案1】:

Pinkie,感谢您发布代码。

$sql = 'SELECT a.videote as videote, b.user_name as user_name'. 
    ' FROM '.$video.' as a,'.$users.' as b'.
    ' where b.user_name=$_GET['user'] and... //if i replace $_GET['user'] with john then it works

这里有一些问题,但我们可以解决它们。

更改字符串时会出现语法错误。您对$video$users 的方式有正确的想法。但是当添加$_GET['user'] 时,PHP 认为第一个撇号正在结束当前字符串。

考虑一下:

' where b.user_name=$_GET['user'] and...'

看起来像两个字符串,由单词“user”分隔:

' where b.user_name=$_GET[' user '] and...'

这不是正确的语法,因此返回 500 错误。我猜如果您尝试一下,您的错误就会消失:

' where b.user_name=' . $_GET['user'] . ' and...'

下一个问题是,如果用户要为“user”参数发送精心设计的值,他们可能会导致查询以您不希望的方式运行。

试试这个:创建一个名为 login.php 的文件,其内容如下:

<?php
    // Just display the output; no HTML formatting needed
    header("Content-Type: text/plain");

    // This must succeed, or mysql_real_escape_string() won't have any effect
    mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
        OR die(mysql_error());

    $safeQuery = 'SELECT count(*) FROM users WHERE user=\'' . mysql_real_escape_string($_GET['username']) . '\' AND pass=\'' . mysql_real_escape_string($_GET['password']) . '\';';
    echo "  safeQuery is: $safeQuery\n";

    $unsafeQuery = 'SELECT count(*) FROM users WHERE user=\'' . $_GET['username'] . '\' AND pass=\'' . $_GET['password'] . '\';';
    echo "unsafeQuery is: $unsafeQuery\n";
?>

加载login.php?username=bob&amp;password=sample。输出看起来很合理:

  safeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample';
unsafeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample';

现在尝试加载login.php?username=bob&amp;password=sample' OR 'hello'='hello"

  safeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample\' OR \'hello\'=\'hello';
unsafeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample' OR 'hello'='hello';

安全查询将返回零,除非您有一个名为 bob 的用户,其密码确实是 sample' OR 'hello'='hello"

但是,不安全的版本将返回数据库中的用户总数。 WHERE 子句现在是:

WHERE user='bob' AND pass='sample' OR 'hello'='hello'

OR 'hello'='hello' 将使条件在所有情况下都为真,即使bob 不存在或密码不是sample

您的部分查询甚至可以被注释掉。试试login.php?username=bob' --

  safeQuery is: SELECT count(*) FROM users WHERE user='bob\' --' AND pass='';
unsafeQuery is: SELECT count(*) FROM users WHERE user='bob' --' AND pass='';

密码参数现在被忽略,因为它嵌入在 SQL 注释中。

因此,即使您只是执行一个 SELECT 语句,如果他们的输入没有被转义,聪明的用户也可以操纵结果。

您可以使用 mysql_real_escape_string 来防止此类错误值。此函数将在必要时添加反斜杠,以防止输入数据作为 SQL 执行。

$sql = 'SELECT a.videote as videote, b.user_name as user_name'. 
    ' FROM '.$video.' as a,'.$users.' as b'.
    ' where b.user_name=\'' . mysql_real_escape_string($_GET['user']) . '\' and...';

php.net page for mysql_real_escape_string 中的示例 1 有一个使用 sprintf 的绝佳示例:

$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
    mysql_real_escape_string($user),
    mysql_real_escape_string($password));

每个%s 都被参数替换(按照它们指定的顺序)。这样可以更轻松地保持查询的可读性,同时防止错误的输入数据。

【讨论】:

  • 太棒了。你说对了。串联绝对是一个问题。连同我所做的其他事情,它现在可以工作了。
  • 很高兴为您提供帮助!感谢您的反馈。
  • 我只是想知道为什么我需要使用 mysql_real_string_escape 知道我只是选择而不是插入。你能澄清一下吗?
  • @Pinkie,我更新了帖子以包含示例。简短的版本是用户可以禁用或更改您的 WHERE 子句,并导致查询返回他们想要的任何内容。让我知道这是否有意义。谢谢!
  • 感谢您的详细解释和示例。现在很清楚了。
【解决方案2】:

所以,如果我没看错的话,当用户访问 x.php?user=john 的页面时,您的服务器将返回一个包含 flash 对象的页面。该 flash 对象依次尝试在 content.php 中加载 xml 页面。你是正确的 content.php 不能从 x.php 访问任何 $_GET 变量,因为它们是两个完全不同的请求。

你可以做两件事:

  1. 将 $_GET['user'] 的内容作为 放入页面,然后让您的 flash 对象将其添加到其请求中。
  2. 让 flash 使用技术from this other article 从查询字符串中获取信息。

我觉得这值得一个警告,请记住在使用前清理查询字符串的内容,这样你就不会遇到 CSS 或 SQL 注入漏洞。

【讨论】:

  • @JoshuaRogers 你能澄清你的答案吗?我如何将我的 $_GET 作为 以及如何让 flash 对象将其添加到它的请求中。我不会问是否知道答案。
  • 在x.php中添加so.addParam("user", "");在您的 .as 中,您将更改要加载的代码,如下所示: var user:String = swfobject.getQueryParamValue("user"); xmlData.load("content.php?user=" + user);但是,您应该知道,上面的代码容易受到 SQL 注入的影响。用户是否应该添加 ?user="Jim"; DELETE FROM users,您可能会发现 users 表已被清除干净。 [php.net/manual/en/function.addslashes.php/] 我推荐 PHP 的 addlashes。
  • 另外,content.php 似乎失败了,因为您的 SQL 中的 $_GET['user'] 周围应该有引号。
  • @JoshuaRogers 我添加了 addParam 并根据您的说明修改了 .as 文件。尽管 500 错误现在消失了,但我没有得到结果。您没有提及有关 sql 查询的任何内容。我是否将其保留为b.user_name=\'$_GET["user"]\'
  • @JoshuaRogers 查看请求日志,我看到请求 URL:http://domain.com/contentp.php?user=undefined。我越来越不确定了。我不太确定 user 是否被正确传递。有任何想法吗。顺便说一句,我的.as 文件中有var user:String = swfobject.getQueryParamValue("user"); 。它是否正确。我假设这就是它应该去的地方
【解决方案3】:

所以 Joshua 肯定有正确的想法,但让我看看我是否可以澄清和扩展一下。

本质上,您应该使用 flash vars 参数将 GET 变量传递给 flash(您可以阅读关于它的优秀教程 here)。一旦 flash 有了变量,它就应该使用?yourNewGetVariable=ValuePassedOnToFlash 附加对 content.php 的查询,而 Joshua 就在您传递该值之前,您绝对应该使用sanitize 它和encode 它作为 URL 的查询。那么你的 content.php 文件访问yourNewGetVariable应该没有问题

【讨论】:

  • 你提到我的链接没有使用 swfobject。根据@JoshuaRogers 的帮助,我将added so.addParam("user", "&lt;?php print urlencode($_GET['user']) ?&gt;"); 作为一个swfobject 参数并在我的.as 文件中添加了var user:String = swfobject.getQueryParamValue("user"); 并修改了xmlData.load("content.php?user=" + user); 在我的sql 查询中,当我运行播放器查看请求日志时,我现在有b.user_name=\'$_GET["user"]\'我看到http://domain.com/contentp.php?user=undefined。得不到结果。知道为什么我变得不确定。
  • @Pinkie,抱歉链接不好,就您的新问题而言,我相信(但我不是专家)应该使用关于 here 和 @ 的 flashvars 传递变量987654325@
猜你喜欢
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 2011-10-17
相关资源
最近更新 更多