Calendar

August 2008
S M T W T F S
« Jul   Sep »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Tag Cloud

Categories

Archives

Recent Posts

Recent Comments


« Congrats to Open Flex! | Main | Win free AIR Books »

Get IPAddress into Flash

By Rich Tretola | August 7, 2008
7,113 views

I have had a few people ask how to read the users IPAddress from within Flash. The way I do this is to use my application server to write the IPAddress into a flash var which I then read from flash. This is obviously open to spoofing if the user wants to play games as the IPAddress is displayed within the page source however it fulfills my needs.

For example my Index.jsp looks something like this:

1
2
3
4
5
<head>
<%
String flashIPAddress=request.getRemoteAddr();
%>
</head>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<%
String flashIPAddress=request.getRemoteAddr();
%>
<script src="AC_OETags.js" language="javascript"></script>
</head>

<body scroll='no'>
<script language="JavaScript" type="text/javascript">
<!--
        AC_FL_RunContent(
                    "src", "temp",
                    "width", "100%",
                    "height", "100%",
                    "align", "middle",
                    "id", "temp",
                    "quality", "high",
                    "bgcolor", "#869ca7",
                    "name", "test",
                                       "flashvars","flashIPAddress=<%= flashIPAddress %>",
                    "allowScriptAccess","sameDomain",
                    "type", "application/x-shockwave-flash",
                    "pluginspage", "http://www.adobe.com/go/getflashplayer"
    );
// -->
</script>
<noscript>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
            id="temp" width="100%" height="100%"
            codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
            <param name="movie" value="test.swf" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#869ca7" />
            <param name="allowScriptAccess" value="sameDomain" />
                        <param name="flashvars"value="flashIPAddress=<%= flashIPAddress %>"/>
            <embed src="test.swf" quality="high" bgcolor="#869ca7"
                width="100%" height="100%" name="temp" align="middle"
                play="true"
                loop="false"
                quality="high"
                allowScriptAccess="sameDomain"
                               flashVars="flashIPAddress=<%= flashIPAddress %>"
                type="application/x-shockwave-flash"
                pluginspage="http://www.adobe.com/go/getflashplayer">
            </embed>
    </object>
</noscript>
</body>
</html>

Notice that I grab the IPAddress in the head and then pass it in 3 different places within the flash embed and script code:

Now within my application I simply refer to this var like this:

1
var ipAddress:String = Application.application.parameters.flashIPAddress;

You could certainly take the same approach within a ColdFusion, ASP, or PHP environment as well.

Topics: Flex | 6 Comments »

6 Responses to “Get IPAddress into Flash”

  1. mnieves Says:
    August 10th, 2008 at 9:39 pm

    hello here is a php code and a flex example.

    MXML CODE:

    1
    2
    3
    4
    5
    6
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:HTTPService id="dir" url="http://yourdomain.com/ip.php" resultFormat="e4x" />
        <mx:Button  label="Get IP" click="dir.send()" x="258" y="238"/>
        <mx:TextInput  text="Ip:{dir.lastResult.ip}" x="210" y="208"/>
    </mx:Application>

    PHP CODE:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
     $REMOTE_ADDR;
    if ($REMOTE_ADDR)
    {
        $message =  "<result><ip>$REMOTE_ADDR</ip></result>";
    }
    else
    {
        $message = "<result><ip>No Ip or any message.</ip></result>";
    }
    echo $message;
    ?>

    Reply to this comment

  2. wodesign Says:
    August 14th, 2008 at 9:24 pm

    so,first way is good if off line;and if the client on line,the seconed is better.

    Reply to this comment

  3. Tom Says:
    December 18th, 2008 at 5:36 pm

    Is it a joke?

    Reply to this comment

  4. Websites tagged "read" on Postsaver Says:
    June 17th, 2009 at 6:32 am

    [...] – Get IPAddress into Flash saved by potato7942009-06-07 – Return to Death Race saved by thecdude2009-06-03 – US Jr dragster [...]

  5. Jerry Says:
    December 15th, 2009 at 10:38 am

    Here’s the JSP server-side code with some enhancements. If your app sits behind a load balancer, sometimes you’ll get the IP address of the load balancer when you do getRemoteAddr(). If you first check the X-Forwarded-For header, it will have the original IP address of the caller in it.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <%
    String ip = request.getHeader("X-Forwarded-For");
    if (ip == null || ip.length() == 0)
        ip = request.getRemoteAddr();
    %>
     
     
     <result>
    <ip>
    <%= ip %>
    </ip>
    </result>

    Reply to this comment

  6. GwenLg Says:
    January 22nd, 2010 at 3:47 pm

    Thanks a lot for such hot knowledge close to this topic ! You should make your custom dissertation, I opine. Just because some thesis service make such things and you can make good thesis summary as well.

    Reply to this comment

Comments