首页 后端开发 php教程 取得随机字符串

取得随机字符串

Jul 25, 2016 am 09:11 AM

取得随机字符串
  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. ?>
复制代码


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1677
14
CakePHP 教程
1431
52
Laravel 教程
1334
25
PHP教程
1280
29
C# 教程
1257
24
说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? 说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

在PHP和Python之间进行选择:指南 在PHP和Python之间进行选择:指南 Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和Python:深入了解他们的历史 PHP和Python:深入了解他们的历史 Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

PHP和框架:现代化语言 PHP和框架:现代化语言 Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

为什么要使用PHP?解释的优点和好处 为什么要使用PHP?解释的优点和好处 Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles