Jump to content

[SOLVED] Copy visitors IP to Clipboard in IE AND FireFox


MesaFloyd

Recommended Posts

I am a 'rookie' please be gentile.

I have a website in PHP with some HTM/L.  I display a visitors IP address by using this

<?php echo $_SERVER['REMOTE_ADDR'] ; ?>

 

All works fine, I simply want to add a <Copy to Clipboard> button <or link> that when pressed copies that IP to a the users clipboard (I do not want him to highlight then <ctl-C>...

 

My problem is, I see several solutions, but most do not function in both IE and Firefox...

I have also seen solutions that require a flash plugin, which is not desireable...

Can anyone suggest a clean and simple solution.

Thanks in advance.

 

Link to comment
Share on other sites

Step 1:

download this file keep name as "copy.swf" (right click link "Save"):

http://tzfiles.com/javascripts/copy.swf

 

Step 2:

Upload "copy.swf" to your server.

 

Step 3:

Save this code in a file called "copy.js" place it in the same folder as "copy.swf" on the server

 

function highlight(field) {
    field.focus();
    field.select();
    var urlSwf = "/javascripts/copy.swf";

    window.status = 'Copied text to clipboard';

    // Copy the text inside the text box to the user's clipboard
    var flashcopier = 'flashcopier';
    var divholder = document.createElement('div');
    divholder.id = flashcopier;
    document.body.appendChild(divholder);

    divholder.innerHTML = '';
    var divinfo = '<embed src="' + urlSwf + '" FlashVars="clipboard='+escape(field.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    divholder.innerHTML = divinfo;
    return false;
} 

 

Step 4:

change this var urlSwf = "/javascripts/copy.swf"; to the location of you swf file (Line 4)

 

step 5:

add this to the head of your document (replace src with the location of your "copy.js"):

<script type="text/javascript" src="/javascripts/copy.js"></script>

 

Step 6:

Add this in the body of your document:

<input id="addr"  value="<?= $_SERVER['REMOTE_ADDR']; ?>" type="text">
<input type="button" onclick="highlight(getElementById('addr'));" value="Copy" />

 

Step 7:

Give it a try in your browser!

Link to comment
Share on other sites

Little Guy

I followed your <excellent> instructions... and I think your code works great!

 

For those <dummies like me> just some details...

When I first saved the file, I saved it as .html and the php code appeard in the box... not the IP address... even so, when I push the copy button, it DID copy the code...

Here is what appears in the box and what is copied..

<?= $_SERVER['REMOTE_ADDR']; ?>

 

Then, I had an "ipifiny"(sp) ... duh! , I changed the file name to .php... and viola, it works.

I will now integreate it on the working web page(I tested an experiment page)

 

I'm promoting you to "THE BIG GUY"... ub da man!

Thanks so much

(BTW-new to this, I don't know how to tag this as SOLVED! so I put it in the headder, hope thats OK)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.