Get random string

WBOY
Release: 2016-07-25 09:11:13
Original
795 people have browsed it
Get random string
  1. /*
  2. Function: Get a random string
  3. Parameters:
  4. 1, (int)$length = 32 #Random character length, the default is 32
  5. 2, (int)$mode = 0 # Random character type, 0 is uppercase and lowercase letters and numbers, 1 is numbers, 2 is lowercase letters, 3 is uppercase letters, 4 is uppercase and lowercase letters, 5 is uppercase letters and numbers, 6 is lowercase letters and numbers
  6. Return: Get String
  7. Use:
  8. $code = new activeCodeObj;
  9. $str = $code->getCode($length, $mode);
  10. */
  11. class activeCodeObj
  12. {
  13. function getCode ($length = 32, $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 = 'ABCDEFGHIJKLMNOPQRSTUVWX YZ1234567890';
  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. Function: Get client information
  50. Parameters:
  51. Return: Specified information
  52. Usage:
  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('32', $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. //修改自q3boy
  211. class cnStrObj
  212. {
  213. ''$function = $tstr = star , $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. $tmpstr2 = ord( substr($str, $i+1, 1));
  236. if(($tmpstr <= 161 || $tmpstr >= 247) && ($tmpstr2 <= 161 || $tmpstr2 >= 247) ){
  237. $legalflag = false;
  238. } else {
  239. $legalflag = true;
  240. }
  241. }
  242. return $legalflag;
  243. }
  244. }
  245. //
  246. define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
  247. define('SMTP_STATUS_CONNECTED', 2, TRUE);
  248. class smtp{
  249. var $connection;
  250. var $connection; var $timeout ;
  251. var $errors;
  252. var $status;
  253. var $body;
  254. var $from;
  255. var $host;
  256. var $port;
  257. var $helo;
  258. var $auth;
  259. var $user;
  260. var $pass ;
  261. /***************************************
  262. ** Filename.......: class.smtp.inc
  263. ** Project........: SMTP Class
  264. ** Version........: 1.00b
  265. ** Last Modified..: 30 September 2001
  266. ***************************************/
  267. function smtp($params = array()){
  268. if(!defined('CRLF'))
  269. define('CRLF', "rn", TRUE);
  270. $this->timeout = 5;
  271. $this->status = SMTP_STATUS_NOT_CONNECTED;
  272. $this->host = 'localhost';
  273. $this->port = 25;
  274. $this->thelo> = 'localhost';
  275. $this->auth = FALSE;
  276. $this->user = '';
  277. $this->pass = '';
  278. $this->errors = array();
  279. foreach($params as $key => $value){
  280. $this->$key = $value;
  281. }
  282. }
  283. /***************************************
  284. ** Constructor function. Arguments:
  285. ** $params - An assoc array of parameters:
  286. **
  287. ** host - The hostname of the smtp server Default: localhost
  288. ** port - The port the smtp server runs on Default: 25
  289. ** helo - What to send as the HELO command Default: localhost
  290. ** (typically the hostname of the
  291. ** machine this script runs on)
  292. ** auth - Whether to use basic authentication Default: FALSE
  293. ** user - Username for authentication Default:
  294. ** pass - Password for authentication Default:
  295. ** timeout - The timeout in seconds for the call Default: 5
  296. ** to fsockopen()
  297. ***************************************/
  298. function connect($params = array()){
  299. if(!isset( $this->status)){
  300. $obj = new smtp($params);
  301. if($obj->connect()){
  302. $obj->status = SMTP_STATUS_CONNECTED;
  303. }
  304. return $obj ;
  305. }else{
  306. $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  307. socket_set_timeout($this- >connection, 0, 250000);
  308. $greeting = $this->get_data();
  309. if(is_resource($this->connection)){
  310. return $this->auth ? $this->ehlo() : $this->helo();
  311. }else{
  312. $this->errors[] = 'Failed to connect to server: '.$errstr;
  313. return FALSE;
  314. }
  315. }
  316. }
  317. /***************************************
  318. ** Function which handles sending the mail.
  319. ** Arguments:
  320. ** $params - Optional assoc array of parameters.
  321. ** Can contain:
  322. ** recipients - Indexed array of recipients
  323. ** from - The from address. (used in MAIL FROM,
  324. ** this will be the return path
  325. ** headers - Indexed array of headers, one header per array entry
  326. ** body - The body of the email
  327. ** It can also contain any of the parameters from the connect()
  328. ** function
  329. ***************************************/
  330. function send($params = array()){
  331. foreach($params as $key => $value){
  332. $this->set($key, $value);
  333. }
  334. if($this->is_connected()){
  335. // Do we auth or not? Note the distinction between the auth variable and auth() function
  336. if($this->auth){
  337. if(!$this->auth())
  338. return FALSE;
  339. }
  340. $this->mail($this->from);
  341. if(is_array($this->recipients))
  342. foreach($this->recipients as $value)
  343. $this->rcpt($value);
  344. else
  345. $this->rcpt($this->recipients);
  346. if(!$this->data())
  347. return FALSE;
  348. // Transparency
  349. $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
  350. $body = str_replace(CRLF.'.', CRLF.'..', $this->body);
  351. $body = $body[0] == '.' ? '.'.$body : $body;
  352. $this->send_data($headers);
  353. $this->send_data('');
  354. $this->send_data($body);
  355. $this->send_data('.');
  356. return (substr(trim($this->get_data()), 0, 3) === '250');
  357. }else{
  358. $this->errors[] = 'Not connected!';
  359. return FALSE;
  360. }
  361. }
  362. /***************************************
  363. ** Function to implement HELO cmd
  364. ***************************************/
  365. function helo(){
  366. if(is_resource($this->connection)
  367. AND $this->send_data('HELO '.$this->helo)
  368. AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
  369. return TRUE;
  370. }else{
  371. $this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
  372. return FALSE;
  373. }
  374. }
  375. /***************************************
  376. ** Function to implement EHLO cmd
  377. ***************************************/
  378. function ehlo(){
  379. if(is_resource($this->connection)
  380. AND $this->send_data('EHLO '.$this->helo)
  381. AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
  382. return TRUE;
  383. }else{
  384. $this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
  385. return FALSE;
  386. }
  387. }
  388. /***************************************
  389. ** Function to implement AUTH cmd
  390. ***************************************/
  391. function auth(){
  392. if(is_resource($this->connection)
  393. AND $this->send_data('AUTH LOGIN')
  394. AND substr(trim($error = $this->get_data()), 0, 3) === '334'
  395. AND $this->send_data(base64_encode($this->user)) // Send username
  396. AND substr(trim($error = $this->get_data()),0,3) === '334'
  397. AND $this->send_data(base64_encode($this->pass)) // Send password
  398. AND substr(trim($error = $this->get_data()),0,3) === '235' ){
  399. return TRUE;
  400. }else{
  401. $this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
  402. return FALSE;
  403. }
  404. }
  405. /***************************************
  406. ** Function that handles the MAIL FROM: cmd
  407. ***************************************/
  408. function mail($from){
  409. if($this->is_connected()
  410. AND $this->send_data('MAIL FROM:<'.$from.'>')
  411. AND substr(trim($this->get_data()), 0, 2) === '250' ){
  412. return TRUE;
  413. }else
  414. return FALSE;
  415. }
  416. /***************************************
  417. ** Function that handles the RCPT TO: cmd
  418. ***************************************/
  419. function rcpt($to){
  420. if($this->is_connected()
  421. AND $this->send_data('RCPT TO:<'.$to.'>')
  422. AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){
  423. return TRUE;
  424. }else{
  425. $this->errors[] = trim(substr(trim($error), 3));
  426. return FALSE;
  427. }
  428. }
  429. /***************************************
  430. ** Function that sends the DATA cmd
  431. ***************************************/
  432. function data(){
  433. if($this->is_connected()
  434. AND $this->send_data('DATA')
  435. AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
  436. return TRUE;
  437. }else{
  438. $this->errors[] = trim(substr(trim($error), 3));
  439. return FALSE;
  440. }
  441. }
  442. /***************************************
  443. ** Function to determine if this object
  444. ** is connected to the server or not.
  445. ***************************************/
  446. function is_connected(){
  447. return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
  448. }
  449. /***************************************
  450. ** Function to send a bit of data
  451. ***************************************/
  452. function send_data($data){
  453. if(is_resource($this->connection)){
  454. return fwrite($this->connection, $data.CRLF, strlen($data)+2);
  455. }else
  456. return FALSE;
  457. }
  458. /***************************************
  459. ** Function to get data.
  460. ***************************************/
  461. function &get_data(){
  462. $return = '';
  463. $line = '';
  464. if(is_resource($this->connection)){
  465. while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' '){
  466. $line = fgets($this->connection, 512);
  467. $return .= $line;
  468. }
  469. return $return;
  470. }else
  471. return FALSE;
  472. }
  473. /***************************************
  474. ** Sets a variable
  475. ***************************************/
  476. function set($var, $value){
  477. $this->$var = $value;
  478. return TRUE;
  479. }
  480. } // End of class
  481. ?>
复制代码


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template