-2
    echo "<b>Your ID</b> = ". $row['customer_id']."";

I couldn't find any way to copy the echo to the clipboard. Can you guys help me ? Thanks much.

azbcexds
  • 1
  • 1
  • 2
    [You will likely have to use JavaScript](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – evolutionxbox Dec 12 '22 at 16:25
  • ik about it i can make a copy button to copy an input box but i don't know how to do it in php – azbcexds Dec 12 '22 at 16:29
  • https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API – epascarello Dec 12 '22 at 16:29
  • @azbcexds PHP runs on the server, it can not copy to a user's clipboard in a browser. You would have to use JavaScript to do it. – epascarello Dec 12 '22 at 16:30
  • I think i couldn't clarify myself so well sorry about it. I will copy the echo output with JS for sure but how do i use the JS code with PHP ? – azbcexds Dec 12 '22 at 16:42
  • I suspect your mixing up Server and Client side. PHP is on the server. It can send data to the Client (your browser) but it can't do anything INSIDE your browser. As soon as PHP has send you the data, it forgets about you. If you want the browser to interact with the clipboard of your computer, you have to use javascript (it is inside your browser). [This answer](https://stackoverflow.com/q/400212/1685196) provides examples. – Michel Dec 12 '22 at 18:13

1 Answers1

-2

The shortest answer; you'll have to tell the OS to do it for you by using shell_exec("echo [thing_to_copy] | [OS Copy Command]

e.g. on MacOS, the copy to clipboard command is pbcopy.

So in PHP we can do

shell_exec("echo 'copy-me' | pbcopy");

which will put copy-me into the clipboard OF THE SERVER.


IMPORTANT

PHP is a server-side language. Meaning, it runs on the server. If you run this, it'll copy the content (copy-me in the previous example) to the server's clipboard which might NOT be what you want.

To copy to the client's clipboard, you'll have to use Javascript because it runs on the client-side.

Edit: added a link to copy using javascript from comments


Security Concern

Never run or use shell_exec or exec without sanitization. In fact, don't run them at all using user-supplied data.

It is a huge security risk and might cause an RCE vulnerability.

Michael Yousrie
  • 1,132
  • 2
  • 10
  • 20