How to Prevent Copy & Paste Text Content in PHP

Learn how to create a Prevent Copy & Paste Text Content using PHP. A simple tool for preventing foreign invaders from copying your blog content. This stops the viewer to be able to do highlighting of text and copying.

In this tutorial we will create a Prevent Copy & Paste Text Content using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by newly coders for its user-friendly environment.

So Let's do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your local database web server then create a database name in it db_copy. After that, click Import then locate the database file inside the folder of the application then click ok.

tut1

To create the sample table and data in the database copy the code below and paste it in the database SQL tab in PHPMyAdmin.

  1. CREATE TABLE `blog` (
  2. `title` varchar(50) NOT NULL,
  3. `content` text NOT NULL
  4.  
  5. INSERT INTO `blog` (`blog_id`, `title`, `content`) VALUES
  6. (1, 'Far Away River', 'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane.'),
  7. (2, 'Wether', 'A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine. I am so happy, my dear friend, so absorbed in the exquisite sense of mere tranquil existence, that I neglect my talents. I should be incapable of drawing a single stroke at the present moment; and yet I feel that I never was a greater artist than now. When, while the lovely valley teems with vapour around me, and the meridian sun strikes the upper surface of the impenetrable foliage of my trees, and but a few stray gleams steal into the inner sanctuary, I throw myself down among the tall grass by the trickling stream; and, as I lie close to the earth, a thousand unknown plants are noticed by me: when I hear the buzz of the little world among the stalks, and grow familiar with the countless indescribable forms of the insects and flies, then I feel the presence of the Almighty, who formed us in his own image, and the breath');

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_copy');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Prevent Copy & Paste Text Content</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Create Content</button>
  18. <br /><br />
  19. <?php
  20. require 'conn.php';
  21.  
  22. $query = $conn->query("SELECT * FROM `blog` ORDER BY `blog_id` DESC");
  23. while($fetch = $query->fetch_array()){
  24. ?>
  25. <div class = "col-md-12 content" style = "word-wrap:break-word; background-color:#eee; padding:20px;">
  26. <h4 class="text-primary"><?php echo $fetch['title']?></h4>
  27. <hr style = "border-top:1px solid #000;"/>
  28. <?php
  29. $content = substr($fetch['content'], 0, 300);
  30. echo $content." <a href='content.php?blog_id=".$fetch['blog_id']."'>Read more</a>";
  31. ?>
  32. </div>
  33. <br style = "clear:both;"/>
  34. <br /><br />
  35. <?php
  36. }
  37.  
  38. ?>
  39. </div>
  40.  
  41. <div class="modal fade" id="form_modal" aria-hidden="true">
  42. <div class="modal-dialog" role="document">
  43. <form method="POST" action="save_query.php">
  44. <div class="modal-content">
  45. <div class="modal-body">
  46. <div class="col-md-2"></div>
  47. <div class="col-md-8">
  48. <div class="form-group">
  49. <label>Title</label>
  50. <input class="form-control" type="text" name="title"/>
  51. </div>
  52. <div class="form-group">
  53. <label>Content</label>
  54. <textarea class="form-control" style="resize:none; height:300px;" name="content"></textarea>
  55. </div>
  56. </div>
  57. </div>
  58. <div style="clear:both;"></div>
  59. <div class="modal-footer">
  60. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  61. <button name="submit" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Submit</button>
  62. </div>
  63. </div>
  64. </form>
  65. </div>
  66. </div>
  67. </body>
  68. <script src="js/jquery-3.2.1.min.js"></script>
  69. <script src="js/bootstrap.js"></script>
  70. </html>

Creating Save Query

This code contains the storing of data input to the database. This code will prompt the user to complete first the required input, to be able to submit the data to the database server. To create this just write these block of code inside the text editor, then name this file as save_query.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['submit'])){
  5. $title = $_POST['title'];
  6. $content = $_POST['content'];
  7.  
  8. if($title == "" && $content == ""){
  9. echo "<script>alert('Please complete the required field!')</script>";
  10. echo "<script>window.location = 'index.php'</script>";
  11. }else{
  12. $conn->query("INSERT INTO `blog` VALUES('', '$title', '$content')");
  13. echo "<script>alert('Successfully Saved!')</script>";
  14. echo "<script>window.location = 'index.php'</script>";
  15. }
  16. }
  17. ?>

Creating Main Function

This code contains the specific function for the application. This code will prevent any attempt of copy and pasting the text content of the author by adding some css prevet function. This method will stop user from highlighting the text content and make stop right clicking from functioning, To have better understanding follow the whole code then save it as content.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. <style>
  7. #content{
  8. -webkit-touch-callout: none;
  9. -webkit-user-select: none;
  10. -khtml-user-select: none;
  11. -moz-user-select: none;
  12. -ms-user-select: none;
  13. user-select: none;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <nav class="navbar navbar-default">
  19. <div class="container-fluid">
  20. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  21. </div>
  22. </nav>
  23. <div class="col-md-3"></div>
  24. <div class="col-md-6 well">
  25. <h3 class="text-primary">Prevent Copy & Paste Text Content</h3>
  26. <hr style="border-top:1px dotted #ccc;"/>
  27. <a class="btn btn-success" href="index.php"><span class="glyphicon glyphicon-hand-right"></span> Back</a>
  28. <br /><br />
  29. <?php
  30. require 'conn.php';
  31. if(ISSET($_REQUEST['blog_id'])){
  32. $query = $conn->query("SELECT * FROM `blog` WHERE `blog_id` = '$_REQUEST[blog_id]'");
  33. $fetch = $query->fetch_array();
  34. ?>
  35. <h2><?php echo $fetch['title']?></h2>
  36. <hr style="1px groovy #ccc"/>
  37. <div id="content" style="word-wrap:break-word;"><p><?php echo $fetch['content']?></p></div>
  38. <?php
  39. }
  40. ?>
  41. </div>
  42. </body>
  43. </html>
DEMO

There you have it we successfully created a Prevent Copy & Paste Text Content. I hope that this simple tutorial helps you with what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

Add new comment