How to prevent crawlers depending on XPath from getting pages contents [duplicate]


Tags: php,html,css,parsing,xpath

Problem :

This question already has an answer here:

There is a library of php that makes evreybody able to attacks me (something like cURL). Then i have a idea to prevent it, I want to use dynamic class name for my elements. look at this:

<div class="<?php $ClassName ?>">anything</div> // $className is taken from the database

Note: $ClassName will vary evry time.

In this case, anyone don't know what is my class name to select my element and then copy my data. Now i have two problem:

  1. How can I communicate between $ClassName and .$ClassName (in css file)? in other words, how can i use php variable for css class names ? (dynamic css classes)
  2. Is it optimized to take all class names from database ?!


Solution :

Using the database to get the class name is not optimal until it can be done locally. You should define a array of all class names, and then pick one up them by array_rand, some thing like this:

// php code
   <?php
     $classes = array('class1','class2','class3','class4'); 
     $class_name = $classes[array_rand($classes)];
   ?>


// html code
     <div class="<? php echo $class_name; ?>">anything</div>


// css code
   <style>
     .<? php echo $class_name; ?> {
      // your css codes
     }
   </style>

Note: you must know that you can't use php codes at .css file, then you should write all css codes that you want to be dynamic in your .php file and use <style> stuff </style>.

Meanwhilem, as @sємsєм said, you can creat dynamic html tags.

Some thing like this: (full code)

// php code
   <?php
     // dynamic class
     $classes = array('class1','class2','class3','class4'); 
     $class_name = $classes[array_rand($classes)];

     // dynamic tags
     $tags_statr = array('','<div>','<div><div>','<div><p>','<span><div>');
     $tags_end = array('','</div>','</div></div>','</div></p>','</span></div>');
     $numb = array_rand($tags_statr);
   ?>


// html code
     <?php echo $tags_statr[$numb]; ?>
     <div class="<? php echo $class_name; ?>">anything</div>
     <?php echo $tags_end[$numb]; ?>


// css code
   <style>
     .<? php echo $class_name; ?> {
      // your css codes
     }
   </style>

And for higher security, You can put your content (Here 'anything') (in addition to the external dynamic tags). for example:

<span1>anything</span1> // <span1> changed to <span2,3,4....>

In this case, the adjacent tag with data is also dynamic, And this makes it harder for crawlers.

Finally, I must say that you can't prevent crawlers utterly, you just make it difficult. If you really want to protect your data, you can do things like them:

  • Increased restrictions for users. (e.g Only registered users can see important information)
  • Monitor IP that uses of your website (and if suspicious, block it)
  • Use relevant software. (e.g To limit the search for an IP on a daily basis)

    CSS Howto..

    How can I use a `comment` fa-icon and a text div to display the number of messages a user has?

    CSS show horizontal scroll bars?

    How can I use PHP code in a CSS file

    How to adjust button css styles

    How do I correct overlapping spacing error in CSS when it wraps text?

    How to make margins collapse evenly?

    How to animate a fixed div with jQuery without page scrollbar resetting to top

    How to shift the alignment of text in a select dropdown toward the top and left with CSS?

    How to push floated element to below another floated element?

    How can I format all td elements contained in a table with class myclass in CSS?

    HTML/CSS: How to move my navigation bar to the middle?

    how to change css of the image link when clicked?

    How to use 3 background images for a resizable layout in CSS?

    Intellij IDEA 11: how can I compile .css from .less?

    How would I make this into an dynamic :nth-child css selector [duplicate]

    how to make a text ele to the right of a left floated ele automatically occupy the rest width?

    Fade in/out text slideshow

    How to make read more option with html/CSS?

    HTTPS and how to reference files and images

    How to wrap or label all divs like files in opertaing systems (jQuery) [closed]

    How can I align text to the top and the bottom of the same div css?

    How to center the boxes as shown?

    How to display a div's name with jquery and css

    css how to remove the empty space at the left

    HTML/CSS: How to create a box for the footer?

    CSS Sprites - How to leave Hover button in depressed state

    How to add a line break inside with CSS?

    How to Make a materialize CSS button's width same as col s12?

    Images not showing in PhoneGap Build application

    Validatation error “Value Error : background-position Too many values or values are not” How to solve?