Heim > Backend-Entwicklung > PHP-Tutorial > 取得随机字符串

取得随机字符串

WBOY
Freigeben: 2016-07-25 09:11:13
Original
818 Leute haben es durchsucht
取得随机字符串
  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 $num = rand(0, $l);
  41. $result .= $str[$num];
  42. }
  43. return $result;
  44. }
  45. }
  46. 取得客户端信息
  47. /*
  48. 作用:取得客户端信息
  49. 参数:
  50. 返回:指定的资料
  51. 使用:
  52. $code = new clientGetObj;
  53. 1、浏览器:$str = $code->getBrowse();
  54. 2、IP地址:$str = $code->getIP();
  55. 4、操作系统:$str = $code->getOS();
  56. */
  57. class clientGetObj
  58. {
  59. function getBrowse()
  60. {
  61. global $_SERVER;
  62. $Agent = $_SERVER['HTTP_USER_AGENT'];
  63. $browser = '';
  64. $browserver = '';
  65. $Browser = array('Lynx', 'MOSAIC', 'AOL', 'Opera', 'JAVA', 'MacWeb', 'WebExplorer', 'OmniWeb');
  66. for($i = 0; $i if(strpos($Agent, $Browsers[$i])){
  67. $browser = $Browsers[$i];
  68. $browserver = '';
  69. }
  70. }
  71. if(ereg('Mozilla', $Agent) && !ereg('MSIE', $Agent)){
  72. $temp = explode('(', $Agent);
  73. $Part = $temp[0];
  74. $temp = explode('/', $Part);
  75. $browserver = $temp[1];
  76. $temp = explode(' ', $browserver);
  77. $browserver = $temp[0];
  78. $browserver = preg_replace('/([d.]+)/', '\1', $browserver);
  79. $browserver = $browserver;
  80. $browser = 'Netscape Navigator';
  81. }
  82. if(ereg('Mozilla', $Agent) && ereg('Opera', $Agent)) {
  83. $temp = explode('(', $Agent);
  84. $Part = $temp[1];
  85. $temp = explode(')', $Part);
  86. $browserver = $temp[1];
  87. $temp = explode(' ', $browserver);
  88. $browserver = $temp[2];
  89. $browserver = preg_replace('/([d.]+)/', '\1', $browserver);
  90. $browserver = $browserver;
  91. $browser = 'Opera';
  92. }
  93. if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent)){
  94. $temp = explode('(', $Agent);
  95. $Part = $temp[1];
  96. $temp = explode(';', $Part);
  97. $Part = $temp[1];
  98. $temp = explode(' ', $Part);
  99. $browserver = $temp[2];
  100. $browserver = preg_replace('/([d.]+)/','\1',$browserver);
  101. $browserver = $browserver;
  102. $browser = 'Internet Explorer';
  103. }
  104. if($browser != ''){
  105. $browseinfo = $browser.' '.$browserver;
  106. } else {
  107. $browseinfo = false;
  108. }
  109. return $browseinfo;
  110. }
  111. function getIP ()
  112. {
  113. global $_SERVER;
  114. if (getenv('HTTP_CLIENT_IP')) {
  115. $ip = getenv('HTTP_CLIENT_IP');
  116. } else if (getenv('HTTP_X_FORWARDED_FOR')) {
  117. $ip = getenv('HTTP_X_FORWARDED_FOR');
  118. } else if (getenv('REMOTE_ADDR')) {
  119. $ip = getenv('REMOTE_ADDR');
  120. } else {
  121. $ip = $_SERVER['REMOTE_ADDR'];
  122. }
  123. return $ip;
  124. }
  125. function getOS ()
  126. {
  127. global $_SERVER;
  128. $agent = $_SERVER['HTTP_USER_AGENT'];
  129. $os = false;
  130. if (eregi('win', $agent) && strpos($agent, '95')){
  131. $os = 'Windows 95';
  132. }
  133. else if (eregi('win 9x', $agent) && strpos($agent, '4.90')){
  134. $os = 'Windows ME';
  135. }
  136. else if (eregi('win', $agent) && ereg('98', $agent)){
  137. $os = 'Windows 98';
  138. }
  139. else if (eregi('win', $agent) && eregi('nt 5.1', $agent)){
  140. $os = 'Windows XP';
  141. }
  142. else if (eregi('win', $agent) && eregi('nt 5', $agent)){
  143. $os = 'Windows 2000';
  144. }
  145. else if (eregi('win', $agent) && eregi('nt', $agent)){
  146. $os = 'Windows NT';
  147. }
  148. else if (eregi('win', $agent) && ereg('32', $agent)){
  149. $os = 'Windows 32';
  150. }
  151. else if (eregi('linux', $agent)){
  152. $os = 'Linux';
  153. }
  154. else if (eregi('unix', $agent)){
  155. $os = 'Unix';
  156. }
  157. else if (eregi('sun', $agent) && eregi('os', $agent)){
  158. $os = 'SunOS';
  159. }
  160. else if (eregi('ibm', $agent) && eregi('os', $agent)){
  161. $os = 'IBM OS/2';
  162. }
  163. else if (eregi('Mac', $agent) && eregi('PC', $agent)){
  164. $os = 'Macintosh';
  165. }
  166. else if (eregi('PowerPC', $agent)){
  167. $os = 'PowerPC';
  168. }
  169. else if (eregi('AIX', $agent)){
  170. $os = 'AIX';
  171. }
  172. else if (eregi('HPUX', $agent)){
  173. $os = 'HPUX';
  174. }
  175. else if (eregi('NetBSD', $agent)){
  176. $os = 'NetBSD';
  177. }
  178. else if (eregi('BSD', $agent)){
  179. $os = 'BSD';
  180. }
  181. else if (ereg('OSF1', $agent)){
  182. $os = 'OSF1';
  183. }
  184. else if (ereg('IRIX', $agent)){
  185. $os = 'IRIX';
  186. }
  187. else if (eregi('FreeBSD', $agent)){
  188. $os = 'FreeBSD';
  189. }
  190. else if (eregi('teleport', $agent)){
  191. $os = 'teleport';
  192. }
  193. else if (eregi('flashget', $agent)){
  194. $os = 'flashget';
  195. }
  196. else if (eregi('webzip', $agent)){
  197. $os = 'webzip';
  198. }
  199. else if (eregi('offline', $agent)){
  200. $os = 'offline';
  201. }
  202. else {
  203. $os = 'Unknown';
  204. }
  205. return $os;
  206. }
  207. }
  208. //修改自q3boy
  209. class cnStrObj
  210. {
  211. function substrGB ($str = '', $start = '', $len = ''){
  212. if($start == 0 || $start == ''){
  213. $start = 1;
  214. }
  215. if($str == '' || $len == ''){
  216. return false;
  217. }
  218. for($i = 0; $i $tmpstr = (ord($str[$i]) >= 161 && ord($str[$i]) = 161 && ord($str[$i+1]) if ($i >= $start && $i {
  219. $tmp .=$tmpstr;
  220. }
  221. }
  222. return $tmp;
  223. }
  224. function isGB ($str)
  225. {
  226. $strLen = strlen($str);
  227. $length = 1;
  228. for($i = 0; $i $tmpstr = ord(substr($str, $i, 1));
  229. $tmpstr2 = ord(substr($str, $i+1, 1));
  230. if(($tmpstr = 247) && ($tmpstr2 = 247)){
  231. $legalflag = false;
  232. } else {
  233. $legalflag = true;
  234. }
  235. }
  236. return $legalflag;
  237. }
  238. }
  239. //下载自某e文网站
  240. /***************************************
  241. ** Filename.......: class.smtp.inc
  242. ** Project........: SMTP Class
  243. ** Version........: 1.00b
  244. ** Last Modified..: 30 September 2001
  245. ***************************************/
  246. define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
  247. define('SMTP_STATUS_CONNECTED', 2, TRUE);
  248. class smtp{
  249. var $connection;
  250. var $recipients;
  251. var $headers;
  252. var $timeout;
  253. var $errors;
  254. var $status;
  255. var $body;
  256. var $from;
  257. var $host;
  258. var $port;
  259. var $helo;
  260. var $auth;
  261. var $user;
  262. var $pass;
  263. /***************************************
  264. ** Constructor function. Arguments:
  265. ** $params - An assoc array of parameters:
  266. **
  267. ** host - The hostname of the smtp server Default: localhost
  268. ** port - The port the smtp server runs on Default: 25
  269. ** helo - What to send as the HELO command Default: localhost
  270. ** (typically the hostname of the
  271. ** machine this script runs on)
  272. ** auth - Whether to use basic authentication Default: FALSE
  273. ** user - Username for authentication Default:
  274. ** pass - Password for authentication Default:
  275. ** timeout - The timeout in seconds for the call Default: 5
  276. ** to fsockopen()
  277. ***************************************/
  278. function smtp($params = array()){
  279. if(!defined('CRLF'))
  280. define('CRLF', "\r\n", TRUE);
  281. $this->timeout = 5;
  282. $this->status = SMTP_STATUS_NOT_CONNECTED;
  283. $this->host = 'localhost';
  284. $this->port = 25;
  285. $this->helo = 'localhost';
  286. $this->auth = FALSE;
  287. $this->user = '';
  288. $this->pass = '';
  289. $this->errors = array();
  290. foreach($params as $key => $value){
  291. $this->$key = $value;
  292. }
  293. }
  294. /***************************************
  295. ** Connect function. This will, when called
  296. ** statically, create a new smtp object,
  297. ** call the connect function (ie this function)
  298. ** and return it. When not called statically,
  299. ** it will connect to the server and send
  300. ** the HELO command.
  301. ***************************************/
  302. function connect($params = array()){
  303. if(!isset($this->status)){
  304. $obj = new smtp($params);
  305. if($obj->connect()){
  306. $obj->status = SMTP_STATUS_CONNECTED;
  307. }
  308. return $obj;
  309. }else{
  310. $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  311. socket_set_timeout($this->connection, 0, 250000);
  312. $greeting = $this->get_data();
  313. if(is_resource($this->connection)){
  314. return $this->auth ? $this->ehlo() : $this->helo();
  315. }else{
  316. $this->errors[] = 'Failed to connect to server: '.$errstr;
  317. return FALSE;
  318. }
  319. }
  320. }
  321. /***************************************
  322. ** Function which handles sending the mail.
  323. ** Arguments:
  324. ** $params - Optional assoc array of parameters.
  325. ** Can contain:
  326. ** recipients - Indexed array of recipients
  327. ** from - The from address. (used in MAIL FROM,
  328. ** this will be the return path
  329. ** headers - Indexed array of headers, one header per array entry
  330. ** body - The body of the email
  331. ** It can also contain any of the parameters from the connect()
  332. ** function
  333. ***************************************/
  334. function send($params = array()){
  335. foreach($params as $key => $value){
  336. $this->set($key, $value);
  337. }
  338. if($this->is_connected()){
  339. // Do we auth or not? Note the distinction between the auth variable and auth() function
  340. if($this->auth){
  341. if(!$this->auth())
  342. return FALSE;
  343. }
  344. $this->mail($this->from);
  345. if(is_array($this->recipients))
  346. foreach($this->recipients as $value)
  347. $this->rcpt($value);
  348. else
  349. $this->rcpt($this->recipients);
  350. if(!$this->data())
  351. return FALSE;
  352. // Transparency
  353. $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
  354. $body = str_replace(CRLF.'.', CRLF.'..', $this->body);
  355. $body = $body[0] == '.' ? '.'.$body : $body;
  356. $this->send_data($headers);
  357. $this->send_data('');
  358. $this->send_data($body);
  359. $this->send_data('.');
  360. return (substr(trim($this->get_data()), 0, 3) === '250');
  361. }else{
  362. $this->errors[] = 'Not connected!';
  363. return FALSE;
  364. }
  365. }
  366. /***************************************
  367. ** Function to implement HELO cmd
  368. ***************************************/
  369. function helo(){
  370. if(is_resource($this->connection)
  371. AND $this->send_data('HELO '.$this->helo)
  372. AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
  373. return TRUE;
  374. }else{
  375. $this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
  376. return FALSE;
  377. }
  378. }
  379. /***************************************
  380. ** Function to implement EHLO cmd
  381. ***************************************/
  382. function ehlo(){
  383. if(is_resource($this->connection)
  384. AND $this->send_data('EHLO '.$this->helo)
  385. AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
  386. return TRUE;
  387. }else{
  388. $this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
  389. return FALSE;
  390. }
  391. }
  392. /***************************************
  393. ** Function to implement AUTH cmd
  394. ***************************************/
  395. function auth(){
  396. if(is_resource($this->connection)
  397. AND $this->send_data('AUTH LOGIN')
  398. AND substr(trim($error = $this->get_data()), 0, 3) === '334'
  399. AND $this->send_data(base64_encode($this->user)) // Send username
  400. AND substr(trim($error = $this->get_data()),0,3) === '334'
  401. AND $this->send_data(base64_encode($this->pass)) // Send password
  402. AND substr(trim($error = $this->get_data()),0,3) === '235' ){
  403. return TRUE;
  404. }else{
  405. $this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
  406. return FALSE;
  407. }
  408. }
  409. /***************************************
  410. ** Function that handles the MAIL FROM: cmd
  411. ***************************************/
  412. function mail($from){
  413. if($this->is_connected()
  414. AND $this->send_data('MAIL FROM:')
  415. AND substr(trim($this->get_data()), 0, 2) === '250' ){
  416. return TRUE;
  417. }else
  418. return FALSE;
  419. }
  420. /***************************************
  421. ** Function that handles the RCPT TO: cmd
  422. ***************************************/
  423. function rcpt($to){
  424. if($this->is_connected()
  425. AND $this->send_data('RCPT TO:')
  426. AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){
  427. return TRUE;
  428. }else{
  429. $this->errors[] = trim(substr(trim($error), 3));
  430. return FALSE;
  431. }
  432. }
  433. /***************************************
  434. ** Function that sends the DATA cmd
  435. ***************************************/
  436. function data(){
  437. if($this->is_connected()
  438. AND $this->send_data('DATA')
  439. AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
  440. return TRUE;
  441. }else{
  442. $this->errors[] = trim(substr(trim($error), 3));
  443. return FALSE;
  444. }
  445. }
  446. /***************************************
  447. ** Function to determine if this object
  448. ** is connected to the server or not.
  449. ***************************************/
  450. function is_connected(){
  451. return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
  452. }
  453. /***************************************
  454. ** Function to send a bit of data
  455. ***************************************/
  456. function send_data($data){
  457. if(is_resource($this->connection)){
  458. return fwrite($this->connection, $data.CRLF, strlen($data)+2);
  459. }else
  460. return FALSE;
  461. }
  462. /***************************************
  463. ** Function to get data.
  464. ***************************************/
  465. function &get_data(){
  466. $return = '';
  467. $line = '';
  468. if(is_resource($this->connection)){
  469. while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' '){
  470. $line = fgets($this->connection, 512);
  471. $return .= $line;
  472. }
  473. return $return;
  474. }else
  475. return FALSE;
  476. }
  477. /***************************************
  478. ** Sets a variable
  479. ***************************************/
  480. function set($var, $value){
  481. $this->$var = $value;
  482. return TRUE;
  483. }
  484. } // End of class
  485. ?>
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage