将php$_GET传递到flash播放器xml文件


Pass php $_GET to flash player xml file

我在x.php页面上嵌入了一个flash播放器?user=john使用swfobject。播放器调用xml文件content.php来获取结果。在content.php中,我有$_GET['user']。我正试图从url id中获取用户名,并以此为基础获取结果。然而,我得到了一个500的错误。我认为content.php无法访问user变量。如果我只输入用户名"john"而不是$_GET['user'],那么它就可以工作了。如何使用$_get['user']

XML文件内容.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> 

我的播放器Actionscript当然指向content.php,这不是的问题

xmlData.load("contentp.php");

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&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&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'

即使bob不存在或密码不是sampleOR 'hello'='hello'也会在所有情况下使条件为真。

您的部分查询甚至可能被注释掉。尝试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_eescape_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...';

mysql_real_eescape_string的php.net页面中的示例1有一个使用sprintf的好示例:

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

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

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

你可以做两件事:

  1. 将$_GET['user']的内容作为<param>然后让你的flash对象将其添加到它的请求中
  2. 让flash使用其他文章中的技术从查询字符串中获取信息

不过,我觉得这应该得到警告,请记住在使用查询字符串之前对其内容进行消毒,这样您就不会出现CSS或SQL注入漏洞。

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

从本质上讲,您应该使用flash vars参数将GET变量传递给flash(您可以在这里阅读有关它的优秀教程)。一旦flash有了变量,它就应该用?yourNewGetVariable=ValuePassedOnToFlash附加content.php的查询,而Joshua就在你传递这个值之前,你肯定应该对它进行净化,并为URL编码。那么您的content.php文件访问yourNewGetVariable 应该没有问题