简体   繁体   中英

How can I prevent text from being copied from my webpage?

I am trying to write an online programming quiz app. So, many questions might be like, "What would be the output of the following code ?".

There is a possibility that users might just copy-paste the whole code and try running it in another window. Is there any good way I can prevent this? I know we can disable right click and Ctrl-C, but is there any more elegant way? Even better, is there a way the app can know that the user is trying to copy stuff, and inform the user that it is not allowed?

(Tagging javascript just because I don't know what to tag this as. Please retag to something more meaningful if you if you can think of something)

The best way to prevent this is to not have the code there at all. Any kind of js is not going to stop anybody (especially coders). The best option I've seen is to use images.

To really prevent any text tampering you could convert the code snippet into an image and post that for the user to read and interpret.

The image solution still leaves open the possibility of interpreting the image with an OCR app but using an OCR is significantly more complex than simply copying and pasting.

This is an ongoing problem in the online world. Preventing copying is actually rather difficult. You can use Javascript to disable right click and Ctrl-C, but there are many ways around that. One approach might be to put your questions in a graphic, rather than text. The user can still download the pic, but it's less useful to them.

i think this can't be done with javascript… also if you are able to block a right cick, you cant block a user using curl and show in the source…

the best solution would probably be to render images of the code serverside… or use flash therefore.

如果您不希望用户复制和粘贴代码,请使用GD等库将其渲染为图像并显示此图像。

Realistically, the best way to handle this is to disable text selection , but while that will deter 90% of users, a determined user can subvert you with something as simple as Firebug. Chances are, if they are looking at code, they are smart enough to handle this readily.

There is no 100% solution if you're using live text. On the other hand, you could use an image or a swf. In either case the user would have to type all of the code manually, which might be more than sufficient deterrent.

Personally, if it were my site, I would look into an image manipulation program so that I could generate and cache the files easily.

You can't prevent the user from do what he wants in your webpage if the code is presented in text-form on your source code page.

You can make things more difficult for the users to copy the code, but you can't prevent it.

Even if you render the code as an image, the user could still simply read and write back the code from the image.

You'll have real problems trying to prevent users copying+pasting text from your site. Many have tried this, with varying degrees of success, but in the end even the best solution has wide open holes that a half-way determined user can get through.

A better solution in your specific example might be to modify the code such that it looks correct but fails to compile. You could do this by replacing characters in the code with other characters that look the same but will choke a compiler.

For example, but non-breaking spaces and other white space characters instead of normal spaces, and most compilers will complain about it. If that doesn't do the trick, there are numerous greek letters which are visually identical to normal ASCII letters, but are actually different character codes; replace one instance of "M" with Μ but not the others, and your code will look fine but will break the compiler.

If your determined copy+paste cheater still manages to get the program working after that, then they probably deserve to get the points for the answer anyway. ;-)

See this site for a list of unusual characters with their HTML entity codes, many of which would be suitable for this kind of sneaky trickery.

You can't. The best way is to use image. Or you can use onSelectStart hook to provide your own function

<body onselectstart="return false">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM