Two methods of generating random strings in PHP

Source: Internet
Author: User
Tags rand

In order to prevent collection, really think a lot of ways, before the limit of IP, the current basic completion, the idea said: in the unit time, such as the number of pages browsed in a minute exceeds the limit, then wait, and finally when the total browsing page to reach the set limit of the day, the lock IP, still need further adjustment, Now consider adding random characters to the content page to disrupt the data collected by the collectors!

Attach two random strings of code, each has its own characteristics, see how you use to achieve reasonable results.

First paragraph:

function rand_string ($len =, $type = ", $addChars =") {

$str = ";

Switch ($type) {

Case 0:

$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '. $addChars;

Break

Case 1:

$chars = Str_repeat (' 0123456789′, 3);

Break

Case 2:

$chars = ' abcdefghijklmnopqrstuvwxyz '. $addChars;

Break

Case 3:

$chars = ' abcdefghijklmnopqrstuvwxyz '. $addChars;

Break

Default:

By default remove easily confusing characters Ooll and number 01, to add please use Addchars parameter

$chars = ' abcdefghijkmnpqrstuvwxyzabcdefghijkmnpqrstuvwxyz23456789′. $addChars;

Break

}

if ($len > 10) {//bit too long repeat string a certain number of times

$chars = $type = = 1? Str_repeat ($chars, $len): Str_repeat ($chars, 5);

}

if ($type!= 4) {

$chars = Str_shuffle ($chars);

$str = substr ($chars, 0, $len);

} else {

Random word

for ($i = 0; $i < $len; $i + +) {

$str. = Msubstr ($chars, Floor (mt_rand (0, Mb_strlen ($chars, ' utf-8′) –1), 1);

}

}

return $str;

}

Echo rand_string ();

Second paragraph code:

function Generateweirdstr ($var) {

$table = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789′;

$len =rand (30,30+ $var);

$text = ";

for ($i =0; $i < $len; $i + +) {

$ch = $table [rand (0,61)];

$text. = $i >0? ' | '. $ch: $ch;

}

return $text;

}

Echo Generateweirdstr (15);

With these two pieces of code, can achieve reasonable results!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.