Get random string

Source: Internet
Author: User
Tags ereg ord
Get random string
  1. /*
  2. Function: Get random string
  3. Parameters:
  4. 1, (int) $length = #随机字符长度, default is 32
  5. 2, (int) $mode = 0 #随机字符类型, 0 for the case of English and numerals, 1 for numbers, 2 for lowercase, 3 for uppercase, 4 for uppercase and lowercase, 5 for lowercase letters and numbers, 6 for small letters and numbers
  6. Return: The obtained string
  7. Use:
  8. $code = new Activecodeobj;
  9. $str = $code->getcode ($length, $mode);
  10. */
  11. Class Activecodeobj
  12. {
  13. function GetCode ($length = +, $mode = 0)
  14. {
  15. Switch ($mode) {
  16. Case ' 1 ':
  17. $str = ' 1234567890 ';
  18. Break
  19. Case ' 2 ':
  20. $str = ' abcdefghijklmnopqrstuvwxyz ';
  21. Break
  22. Case ' 3 ':
  23. $str = ' abcdefghijklmnopqrstuvwxyz ';
  24. Break
  25. Case ' 4 ':
  26. $str = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';
  27. Break
  28. Case ' 5 ':
  29. $str = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ';
  30. Break
  31. Case ' 6 ':
  32. $str = ' abcdefghijklmnopqrstuvwxyz1234567890 ';
  33. Break
  34. Default
  35. $str = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 ';
  36. Break
  37. }
  38. $result = ";
  39. $l = strlen ($STR);
  40. for ($i = 0; $i < $length; $i + +) {
  41. $num = rand (0, $l);
  42. $result. = $str [$num];
  43. }
  44. return $result;
  45. }
  46. }
  47. Get client Information
  48. /*
  49. Role: Obtain client information
  50. Parameters:
  51. Returns: the specified data
  52. Use:
  53. $code = new Clientgetobj;
  54. 1, browser: $str = $code->getbrowse ();
  55. 2, IP address: $str = $code->getip ();
  56. 4, operating system: $STR = $code->getos ();
  57. */
  58. Class Clientgetobj
  59. {
  60. function Getbrowse ()
  61. {
  62. Global $_server;
  63. $Agent = $_server[' http_user_agent ');
  64. $browser = ";
  65. $browserver = ";
  66. $Browser = Array (' Lynx ', ' MOSAIC ', ' AOL ', ' Opera ', ' JAVA ', ' macweb ', ' webexplorer ', ' omniweb ');
  67. for ($i = 0; $i <= 7; $i + +) {
  68. if (Strpos ($Agent, $Browsers [$i])) {
  69. $browser = $Browsers [$i];
  70. $browserver = ";
  71. }
  72. }
  73. if (Ereg (' Mozilla ', $Agent) &&!ereg (' MSIE ', $Agent)) {
  74. $temp = Explode (' (', $Agent);
  75. $Part = $temp [0];
  76. $temp = explode ('/', $Part);
  77. $browserver = $temp [1];
  78. $temp = Explode (' ', $browserver);
  79. $browserver = $temp [0];
  80. $browserver = Preg_replace ('/([D.] +)/', ' \1 ', $browserver);
  81. $browserver = $browserver;
  82. $browser = ' Netscape Navigator ';
  83. }
  84. if (Ereg (' Mozilla ', $Agent) && ereg (' Opera ', $Agent)) {
  85. $temp = Explode (' (', $Agent);
  86. $Part = $temp [1];
  87. $temp = Explode (') ', $Part);
  88. $browserver = $temp [1];
  89. $temp = Explode (' ', $browserver);
  90. $browserver = $temp [2];
  91. $browserver = Preg_replace ('/([D.] +)/', ' \1 ', $browserver);
  92. $browserver = $browserver;
  93. $browser = ' Opera ';
  94. }
  95. if (Ereg (' Mozilla ', $Agent) && ereg (' MSIE ', $Agent)) {
  96. $temp = Explode (' (', $Agent);
  97. $Part = $temp [1];
  98. $temp = explode ('; ', $Part);
  99. $Part = $temp [1];
  100. $temp = Explode (' ', $Part);
  101. $browserver = $temp [2];
  102. $browserver = Preg_replace ('/([D.] +)/', ' \1 ', $browserver);
  103. $browserver = $browserver;
  104. $browser = ' Internet Explorer ';
  105. }
  106. if ($browser! = ") {
  107. $browseinfo = $browser. ' '. $browserver;
  108. } else {
  109. $browseinfo = false;
  110. }
  111. return $browseinfo;
  112. }
  113. function GetIP ()
  114. {
  115. Global $_server;
  116. if (getenv (' http_client_ip ')) {
  117. $ip = getenv (' http_client_ip ');
  118. } else if (getenv (' http_x_forwarded_for ')) {
  119. $ip = getenv (' http_x_forwarded_for ');
  120. } else if (getenv (' remote_addr ')) {
  121. $ip = getenv (' remote_addr ');
  122. } else {
  123. $ip = $_server[' remote_addr ');
  124. }
  125. return $IP;
  126. }
  127. function Getos ()
  128. {
  129. Global $_server;
  130. $agent = $_server[' http_user_agent ');
  131. $os = false;
  132. if (eregi (' win ', $agent) && strpos ($agent, ' 95 ')) {
  133. $os = ' Windows 95 ';
  134. }
  135. else if (eregi (' Win 9x ', $agent) && strpos ($agent, ' 4.90 ')) {
  136. $os = ' Windows ME ';
  137. }
  138. else if (eregi (' win ', $agent) && ereg (' 98 ', $agent)) {
  139. $os = ' Windows 98 ';
  140. }
  141. else if (eregi (' win ', $agent) && eregi (' NT 5.1 ', $agent)) {
  142. $os = ' Windows XP ';
  143. }
  144. else if (eregi (' win ', $agent) && eregi (' NT 5 ', $agent)) {
  145. $os = ' Windows 2000 ';
  146. }
  147. else if (eregi (' win ', $agent) && eregi (' NT ', $agent)) {
  148. $os = ' Windows NT ';
  149. }
  150. else if (eregi (' win ', $agent) && ereg (' + ', $agent)) {
  151. $os = ' Windows 32 ';
  152. }
  153. else if (eregi (' Linux ', $agent)) {
  154. $os = ' Linux ';
  155. }
  156. else if (eregi (' Unix ', $agent)) {
  157. $os = ' Unix ';
  158. }
  159. else if (eregi (' Sun ', $agent) && eregi (' OS ', $agent)) {
  160. $os = ' SunOS ';
  161. }
  162. else if (eregi (' IBM ', $agent) && eregi (' OS ', $agent)) {
  163. $os = ' IBM os/2 ';
  164. }
  165. else if (eregi (' Mac ', $agent) && eregi (' PC ', $agent)) {
  166. $os = ' Macintosh ';
  167. }
  168. else if (eregi (' PowerPC ', $agent)) {
  169. $os = ' PowerPC ';
  170. }
  171. else if (eregi (' AIX ', $agent)) {
  172. $os = ' AIX ';
  173. }
  174. else if (eregi (' HPUX ', $agent)) {
  175. $os = ' HPUX ';
  176. }
  177. else if (eregi (' NetBSD ', $agent)) {
  178. $os = ' NetBSD ';
  179. }
  180. else if (eregi (' BSD ', $agent)) {
  181. $os = ' BSD ';
  182. }
  183. else if (ereg (' OSF1 ', $agent)) {
  184. $os = ' OSF1 ';
  185. }
  186. else if (ereg (' IRIX ', $agent)) {
  187. $os = ' IRIX ';
  188. }
  189. else if (eregi (' FreeBSD ', $agent)) {
  190. $os = ' FreeBSD ';
  191. }
  192. else if (eregi (' teleport ', $agent)) {
  193. $os = ' teleport ';
  194. }
  195. else if (eregi (' FlashGet ', $agent)) {
  196. $os = ' FlashGet ';
  197. }
  198. else if (eregi (' Webzip ', $agent)) {
  199. $os = ' Webzip ';
  200. }
  201. else if (eregi (' Offline ', $agent)) {
  202. $os = ' offline ';
  203. }
  204. else {
  205. $os = ' Unknown ';
  206. }
  207. return $os;
  208. }
  209. }
  210. Modified from Q3boy
  211. Class Cnstrobj
  212. {
  213. function Substrgb ($str = ', $start = ', $len = ') {
  214. if ($start = = 0 | | $start = = ') {
  215. $start = 1;
  216. }
  217. if ($str = = "| | $len = =") {
  218. return false;
  219. }
  220. for ($i = 0; $i < $start + $len; $i + +) {
  221. $tmpstr = (ord ($str [$i]) >= 161 && Ord ($str [$i]) <= 247&& ord ($str [$i +1]) >= 161 && Ord ($ str[$i +1]) <= 254) $str [$i]. $str [+ + $i]: $tmpstr = $str [$i];
  222. if ($i >= $start && $i < ($start + $len))
  223. {
  224. $tmp. = $tmpstr;
  225. }
  226. }
  227. return $tmp;
  228. }
  229. function Isgb ($STR)
  230. {
  231. $strLen = StrLen ($STR);
  232. $length = 1;
  233. for ($i = 0; $i < $strLen; $i + +) {
  234. $tmpstr = Ord (substr ($str, $i, 1));
  235. $tmpstr 2 = Ord (substr ($str, $i +1, 1));
  236. if (($tmpstr <= 161 | | $tmpstr >= 247) && ($tmpstr 2 <= 161 | | $tmpstr 2 >= 247)) {
  237. $legalflag = false;
  238. } else {
  239. $legalflag = true;
  240. }
  241. }
  242. return $legalflag;
  243. }
  244. }
  245. Download from an e-text website
  246. /***************************************
  247. * * Filename ...: class.smtp.inc
  248. * * Project ...: SMTP Class
  249. * * Version ...: 1.00b
  250. * * Last Modified: September 2001
  251. ***************************************/
  252. Define (' smtp_status_not_connected ', 1, TRUE);
  253. Define (' smtp_status_connected ', 2, TRUE);
  254. Class smtp{
  255. var $connection;
  256. var $recipients;
  257. var $headers;
  258. var $timeout;
  259. var $errors;
  260. var $status;
  261. var $body;
  262. var $from;
  263. var $host;
  264. var $port;
  265. var $helo;
  266. var $auth;
  267. var $user;
  268. var $pass;
  269. /***************************************
  270. * * Constructor function. Arguments:
  271. * * $params-an Assoc Array of parameters:
  272. **
  273. * * host-the hostname of the SMTP server Default:localhost
  274. * * Port-the port The SMTP server runs on default:25
  275. * * Helo-what to Send as the HELO command default:localhost
  276. * * (typically the hostname of the
  277. * * Machine This script runs on)
  278. * * Auth-whether to use Basic authentication Default:false
  279. * * User-username for authentication Default:
  280. * * Pass-password for authentication Default:
  281. * * timeout-the timeout in seconds for the call Default:5
  282. * * to Fsockopen ()
  283. ***************************************/
  284. function smtp ($params = Array ()) {
  285. if (!defined (' CRLF '))
  286. Define (' CRLF ', "\ r \ n", TRUE);
  287. $this->timeout = 5;
  288. $this->status = smtp_status_not_connected;
  289. $this->host = ' localhost ';
  290. $this->port = 25;
  291. $this->helo = ' localhost ';
  292. $this->auth = FALSE;
  293. $this->user = ";
  294. $this->pass = ";
  295. $this->errors = Array ();
  296. foreach ($params as $key = = $value) {
  297. $this $key = $value;
  298. }
  299. }
  300. /***************************************
  301. * * Connect function. This would, when called
  302. * * Statically, create a new SMTP object,
  303. * * Call the Connect function (ie this function)
  304. * * and return it. When not called statically,
  305. * * It would connect to the server and send
  306. * * The HELO command.
  307. ***************************************/
  308. Function Connect ($params = Array ()) {
  309. if (!isset ($this->status)) {
  310. $obj = new SMTP ($PARAMS);
  311. if ($obj->connect ()) {
  312. $obj->status = smtp_status_connected;
  313. }
  314. return $obj;
  315. }else{
  316. $this->connection = Fsockopen ($this->host, $this->port, $errno, $errstr, $this->timeout);
  317. Socket_set_timeout ($this->connection, 0, 250000);
  318. $greeting = $this->get_data ();
  319. if (Is_resource ($this->connection)) {
  320. Return $this->auth? $this->ehlo (): $this->helo ();
  321. }else{
  322. $this->errors[] = ' Failed to connect to server: '. $errstr;
  323. return FALSE;
  324. }
  325. }
  326. }
  327. /***************************************
  328. * * Function which handles sending the mail.
  329. * * Arguments:
  330. * * $params-optional assoc Array of parameters.
  331. * * Can contain:
  332. * * Recipients-indexed array of recipients
  333. * * From-the from address. (used in MAIL from,
  334. * * This'll be the return path
  335. * * headers-indexed array of headers, one header per array entry
  336. * * Body-the body of the email
  337. * * It can also contain any of the parameters from the Connect ()
  338. * * function
  339. ***************************************/
  340. function Send ($params = Array ()) {
  341. foreach ($params as $key = = $value) {
  342. $this->set ($key, $value);
  343. }
  344. if ($this->is_connected ()) {
  345. Do we auth or not? Note the distinction between the auth variable and auth () function
  346. if ($this->auth) {
  347. if (! $this->auth ())
  348. return FALSE;
  349. }
  350. $this->mail ($this->from);
  351. if (Is_array ($this->recipients))
  352. foreach ($this->recipients as $value)
  353. $this->rcpt ($value);
  354. Else
  355. $this->rcpt ($this->recipients);
  356. if (! $this->data ())
  357. return FALSE;
  358. Transparency
  359. $headers = Str_replace (CRLF. '. ', CRLF. '.. ', Trim (implode (CRLF, $this->headers));
  360. $body = Str_replace (CRLF. '. ', CRLF. '. ', $this->body);
  361. $body = $body [0] = = '. '? '.'. $body: $body;
  362. $this->send_data ($headers);
  363. $this->send_data (");
  364. $this->send_data ($body);
  365. $this->send_data ('. ');
  366. Return (substr (Trim ($this->get_data ()), 0, 3) = = = ' 250 ');
  367. }else{
  368. $this->errors[] = ' not connected! ';
  369. return FALSE;
  370. }
  371. }
  372. /***************************************
  373. * * Function to implement HELO cmd
  374. ***************************************/
  375. function Helo () {
  376. if (Is_resource ($this->connection)
  377. and $this->send_data (' HELO '. $this->helo)
  378. and substr (Trim ($error = $this->get_data ()), 0, 3) = = = ' 250 ') {
  379. return TRUE;
  380. }else{
  381. $this->errors[] = ' HELO command failed, output: '. Trim (substr (Trim ($error), 3));
  382. return FALSE;
  383. }
  384. }
  385. /***************************************
  386. * * Function to implement EHLO cmd
  387. ***************************************/
  388. function Ehlo () {
  389. if (Is_resource ($this->connection)
  390. and $this->send_data (' EHLO '. $this->helo)
  391. and substr (Trim ($error = $this->get_data ()), 0, 3) = = = ' 250 ') {
  392. return TRUE;
  393. }else{
  394. $this->errors[] = ' EHLO command failed, output: '. Trim (substr (Trim ($error), 3));
  395. return FALSE;
  396. }
  397. }
  398. /***************************************
  399. * * Function to implement AUTH cmd
  400. ***************************************/
  401. function auth () {
  402. if (Is_resource ($this->connection)
  403. and $this->send_data (' AUTH LOGIN ')
  404. and substr (Trim ($error = $this->get_data ()), 0, 3) = = = ' 334 '
  405. and $this->send_data (Base64_encode ($this->user))//Send username
  406. and substr (Trim ($error = $this->get_data ()), 0, 3) = = = ' 334 '
  407. and $this->send_data (Base64_encode ($this->pass))//Send password
  408. and substr (Trim ($error = $this->get_data ()), 0, 3) = = = ' 235 ') {
  409. return TRUE;
  410. }else{
  411. $this->errors[] = ' AUTH command failed: '. Trim (substr (Trim ($error), 3));
  412. return FALSE;
  413. }
  414. }
  415. /***************************************
  416. * * Function that handles the MAIL From:cmd
  417. ***************************************/
  418. function mail ($from) {
  419. if ($this->is_connected ()
  420. and $this->send_data (' MAIL from:< '. $from. ' > ')
  421. and substr (Trim ($this->get_data ()), 0, 2) = = = ' 250 ') {
  422. return TRUE;
  423. }else
  424. return FALSE;
  425. }
  426. /***************************************
  427. * * Function that handles the RCPT To:cmd
  428. ***************************************/
  429. Function Rcpt ($to) {
  430. if ($this->is_connected ()
  431. and $this->send_data (' RCPT to:< '. $to. ' > ')
  432. and substr (Trim ($error = $this->get_data ()), 0, 2) = = = ' 25 ') {
  433. return TRUE;
  434. }else{
  435. $this->errors[] = Trim (substr (Trim ($error), 3));
  436. return FALSE;
  437. }
  438. }
  439. /***************************************
  440. * * Function that sends the DATA CMD
  441. ***************************************/
  442. function data () {
  443. if ($this->is_connected ()
  444. and $this->send_data (' data ')
  445. and substr (Trim ($error = $this->get_data ()), 0, 3) = = = ' 354 ') {
  446. return TRUE;
  447. }else{
  448. $this->errors[] = Trim (substr (Trim ($error), 3));
  449. return FALSE;
  450. }
  451. }
  452. /***************************************
  453. * * Function to determine if the This object
  454. * * is connected to the server or not.
  455. ***************************************/
  456. function is_connected () {
  457. Return (Is_resource ($this->connection) and ($this->status = = = smtp_status_connected));
  458. }
  459. /***************************************
  460. * * Function to send a bit of data
  461. ***************************************/
  462. function Send_data ($data) {
  463. if (Is_resource ($this->connection)) {
  464. Return fwrite ($this->connection, $data. CRLF, strlen ($data) +2);
  465. }else
  466. return FALSE;
  467. }
  468. /***************************************
  469. * * Function to get data.
  470. ***************************************/
  471. function &get_data () {
  472. $return = ";
  473. $line = ";
  474. if (Is_resource ($this->connection)) {
  475. while (Strpos ($return, CRLF) = = = FALSE OR substr ($line, 3, 1)!== ") {
  476. $line = fgets ($this->connection, 512);
  477. $return. = $line;
  478. }
  479. return $return;
  480. }else
  481. return FALSE;
  482. }
  483. /***************************************
  484. * * Sets a variable
  485. ***************************************/
  486. function set ($var, $value) {
  487. $this $var = $value;
  488. return TRUE;
  489. }
  490. }//End of Class
  491. ?>
Copy Code
  • Related Article

    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.