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):
replace: copy("User's IP address would go here"); Code (markup): with copy("<?php echo $_SERVER['REMOTE_ADDR']; ?>"); PHP:
I think most browsers won't allow direct copying but that's indeed the code, except if you are using a proxy check this http://www.hawkee.com/snippet/1358/
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?
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: