当前位置:Gxlcms > PHP教程 > 取得随机字符串

取得随机字符串

时间:2021-07-01 10:21:17 帮助过:11人阅读

取得随机字符串
  1. /*
  2. 作用:取得随机字符串
  3. 参数:
  4. 1、(int)$length = 32 #随机字符长度,默认为32
  5. 2、(int)$mode = 0 #随机字符类型,0为大小写英文和数字,1为数字,2为小写子木,3为大写字母,4为大小写字母,5为大写字母和数字,6为小写字母和数字
  6. 返回:取得的字符串
  7. 使用:
  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 = '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. 取得客户端信息
  48. /*
  49. 作用:取得客户端信息
  50. 参数:
  51. 返回:指定的资料
  52. 使用:
  53. $code = new clientGetObj;
  54. 1、浏览器:$str = $code->getBrowse();
  55. 2、IP地址:$str = $code->getIP();
  56. 4、操作系统:$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 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. $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. //下载自某e文网站
  246. /***************************************
  247. ** Filename.......: class.smtp.inc
  248. ** Project........: SMTP Class
  249. ** Version........: 1.00b
  250. ** Last Modified..: 30 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 will, 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 will 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 will 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 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. ?>

人气教程排行