PHP in JavaScript

Discussion in 'PHP' started by Hyphen, Oct 20, 2008.

  1. #1
    I am trying to use a common onload clipboard effect to automatically clipboard the user's IP, but I am unsure how to use PHP in unison with JavaScript. Can anyone help me?

    <script type="text/javascript" language="javascript">
    function copy(text2copy) {
      if (window.clipboardData) {
        window.clipboardData.setData("Text",text2copy);
      } else {
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) {
          var divholder = document.createElement('div');
          divholder.id = flashcopier;
          document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
      }
    }
    
    window.onload = snvc;
    
    function snvc() {
    copy("User's IP address would go here");
    }
    </script>
    Code (markup):

     
    Hyphen, Oct 20, 2008 IP
  2. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    replace:

    
    copy("User's IP address would go here");
    
    Code (markup):
    with

    
    copy("<?php echo $_SERVER['REMOTE_ADDR']; ?>");
    
    PHP:
     
    Bind, Oct 20, 2008 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    EricBruggema, Oct 20, 2008 IP
  4. Hyphen

    Hyphen Well-Known Member

    Messages:
    464
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #4
    I did this before and it copies that actual code to the clipboard, not the IP.

    Oh, okay... nevermind. Probably because I had the file as .htm and not .php...
    Yeah, thanks for helping me realize that.


    Another question, though. If I wanted to make it so that the IP copied when an input button was clicked, how would I do that?
     
    Hyphen, Oct 20, 2008 IP
  5. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    that's a misleading script snippet.

    it will indeed present you with the users real ip if they are connecting directly or through a non-elite proxy, but if they are connecting through an elite proxy, a phproxy, or CGI proxy, then you wont get their real ip because the elite proxy server filters out and does NOT pass on the 'HTTP_X_FORWARDED_FOR' value, so all you will get is the proxy servers ip.


    something like this but it will only work in IE i believe :
    
    <SCRIPT LANGUAGE="JavaScript">
    function ClipBoard()
        {
            holdtext.innerText = copytext.innerText;
            Copied = holdtext.createTextRange();
            Copied.execCommand("Copy");
        }
    </SCRIPT>
    
    <SPAN ID="copytext">
    <?php echo $_SERVER['REMOTE_ADDR'] ?>
    </SPAN>
    <TEXTAREA ID="holdtext" STYLE="display:none;">
    </TEXTAREA>
    <BUTTON onClick="ClipBoard();">Copy IP to Clipboard</BUTTON>
    
    PHP:
     
    Bind, Oct 20, 2008 IP