小程序文章内页
1 2 3 |
<?php phpinfo(); ?> |
小程序文章内页
1 2 3 |
<?php phpinfo(); ?> |
Windows服务
— 启动MySQL
net start mysql
— 创建Windows服务
sc create mysql binPath= mysqld_bin_path(注意:等号与值之间有空格)
连接与断开服务器
mysql -h 地址 -P 端口 -u 用户名 -p 密码
SHOW PROCESSLIST — 显示哪些线程正在运行
SHOW VARIABLES — 显示系统变量信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
<?php $mimetypes = array( 'ez' => 'application/andrew-inset', 'hqx' => 'application/mac-binhex40', 'cpt' => 'application/mac-compactpro', 'doc' => 'application/msword', 'bin' => 'application/octet-stream', 'dms' => 'application/octet-stream', 'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream', 'exe' => 'application/octet-stream', 'class' => 'application/octet-stream', 'so' => 'application/octet-stream', 'dll' => 'application/octet-stream', 'oda' => 'application/oda', 'pdf' => 'application/pdf', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', 'smi' => 'application/smil', 'smil' => 'application/smil', 'mif' => 'application/vnd.mif', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'wbxml' => 'application/vnd.wap.wbxml', 'wmlc' => 'application/vnd.wap.wmlc', 'wmlsc' => 'application/vnd.wap.wmlscriptc', 'bcpio' => 'application/x-bcpio', 'vcd' => 'application/x-cdlink', 'pgn' => 'application/x-chess-pgn', 'cpio' => 'application/x-cpio', 'csh' => 'application/x-csh', 'dcr' => 'application/x-director', 'dir' => 'application/x-director', 'dxr' => 'application/x-director', 'dvi' => 'application/x-dvi', 'spl' => 'application/x-futuresplash', 'gtar' => 'application/x-gtar', 'hdf' => 'application/x-hdf', 'js' => 'application/x-javascript', 'skp' => 'application/x-koan', 'skd' => 'application/x-koan', 'skt' => 'application/x-koan', 'skm' => 'application/x-koan', 'latex' => 'application/x-latex', 'nc' => 'application/x-netcdf', 'cdf' => 'application/x-netcdf', 'sh' => 'application/x-sh', 'shar' => 'application/x-shar', 'swf' => 'application/x-shockwave-flash', 'sit' => 'application/x-stuffit', 'sv4cpio' => 'application/x-sv4cpio', 'sv4crc' => 'application/x-sv4crc', 'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex', 'texinfo' => 'application/x-texinfo', 'texi' => 'application/x-texinfo', 't' => 'application/x-troff', 'tr' => 'application/x-troff', 'roff' => 'application/x-troff', 'man' => 'application/x-troff-man', 'me' => 'application/x-troff-me', 'ms' => 'application/x-troff-ms', 'ustar' => 'application/x-ustar', 'src' => 'application/x-wais-source', 'xhtml' => 'application/xhtml+xml', 'xht' => 'application/xhtml+xml', 'zip' => 'application/zip', 'au' => 'audio/basic', 'snd' => 'audio/basic', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'kar' => 'audio/midi', 'mpga' => 'audio/mpeg', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'aif' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', 'm3u' => 'audio/x-mpegurl', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio', 'rpm' => 'audio/x-pn-realaudio-plugin', 'ra' => 'audio/x-realaudio', 'wav' => 'audio/x-wav', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-xyz', 'bmp' => 'image/bmp', 'gif' => 'image/gif', 'ief' => 'image/ief', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'jpe' => 'image/jpeg', 'png' => 'image/png', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'djvu' => 'image/vnd.djvu', 'djv' => 'image/vnd.djvu', 'wbmp' => 'image/vnd.wap.wbmp', 'ras' => 'image/x-cmu-raster', 'pnm' => 'image/x-portable-anymap', 'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'ppm' => 'image/x-portable-pixmap', 'rgb' => 'image/x-rgb', 'xbm' => 'image/x-xbitmap', 'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'igs' => 'model/iges', 'iges' => 'model/iges', 'msh' => 'model/mesh', 'mesh' => 'model/mesh', 'silo' => 'model/mesh', 'wrl' => 'model/vrml', 'vrml' => 'model/vrml', 'css' => 'text/css', 'html' => 'text/html', 'htm' => 'text/html', 'asc' => 'text/plain', 'txt' => 'text/plain', 'rtx' => 'text/richtext', 'rtf' => 'text/rtf', 'sgml' => 'text/sgml', 'sgm' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'wml' => 'text/vnd.wap.wml', 'wmls' => 'text/vnd.wap.wmlscript', 'etx' => 'text/x-setext', 'xsl' => 'text/xml', 'xml' => 'text/xml', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'mxu' => 'video/vnd.mpegurl', 'avi' => 'video/x-msvideo', 'movie' => 'video/x-sgi-movie', 'ice' => 'x-conference/x-cooltalk', ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
<?php //测试定时执行的代码 // include_once('test.php'); //加载配置 function a(){ return 1; } @ini_set('memory_limit', '64M'); //最大占用内存 ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 define('CRON_ROOT',dirname(__FILE__)); $lockfile = CRON_ROOT.'\\Returns\\lock.txt'; //自定义文件位置 if(!is_file($lockfile)){ file_put_contents($lockfile,''); //写入文件 }else{ die('文件已执行'); } //配置信息 可以使用修改成数据库设置,文件配置等等 $set['time'] = strtotime(date('2017-11-14 00:35:00',time())); //返现记录日期(当天),设置的时间 $set['type'] = 'fen'; //分钟 $set['bei'] = 0.1; //倍数,例如 3天,3小时,3周 $set['status'] = true; //开启定时任务,false 关闭定时任务 $set['url'] = 'http://localhost:808/cron.php'; //当前文件URL $now = time(); //当前时间 //分钟,小时,天,周 $rule = array( 'fen' =-->60, 'hour' =>60*60, 'day' =>60*60*24, 'week' =>60*60*24*7, ); $returns_dir = CRON_ROOT.'\\Returns\\'.date('Y-m').'\\'; //日志文件夹不存在创建 if(!is_dir($returns_dir)){ mkdir($returns_dir); } //时间计算, //1 当前时间 大于 返现设置时间,未返现,返现,等待下一次返现时间,执行脚本 //2 当前时间 大于 返现设置时间,已返现,等待下一次返现时间,执行脚本 if($now>=$set['time']){ $bei = 1; do { $nexttime = $set['time']+$rule[$set['type']]*$set['bei']*$bei; //下一次返现时间,并且要大于当前时间 $bei++; } while ( $now >= $nexttime ); $sleeptime = $nexttime-$now; //判断 当前时间 是否需要返现 $lasttime = $nexttime - ($rule[$set['type']]*$set['bei']); //当前返现时间 //当前返现时间小于 且 文件不存在(未返现) 且大于设置时间 需要返现 if($lasttime<=$now && $lasttime>=$set['time']){ $name = $returns_dir.date('Ymd-H-i-s',$lasttime).'.txt'; //当前返现日志名 //日志不存在创建 if(!is_file($name)){ //文件不存在,未赠款 file_put_contents($name,''); //写入文件 //开始执行 定时任务 // error_log(var_export(a(),true),3,'log.txt'); //执行的代码测试使用 } } }else{ $sleeptime = $set['time']-$now; } //判断是否开启定时返现功能 if($set['status']){ // 更精确的时间控制 // $ss = 1000000;//1秒 // usleep($time); // usleep($sleeptime*$ss); //1s 等于 一百万 $ss = 1; sleep($sleeptime); //1s file_get_contents($set['url']); }else{ unlink($lockfile); //限制运行多个脚本 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php /** * QueryList使用示例 * * 入门教程:http://doc.querylist.cc/site/index/doc/4 * * QueryList::Query(采集的目标页面,采集规则[,区域选择器][,输出编码][,输入编码][,是否移除头部]) * //采集规则 * $rules = array( * '规则名' => array('jQuery选择器','要采集的属性'[,"标签过滤列表"][,"回调函数"]), * '规则名2' => array('jQuery选择器','要采集的属性'[,"标签过滤列表"][,"回调函数"]), * .......... * [,"callback"=>"全局回调函数"] * ); */ require 'vendor/autoload.php'; use QL\QueryList; //论坛列表 $urllist = 'http://bbs.51credit.com/forum-216-1.html'; $baseurl = 'http://bbs.51credit.com/'; $ql1 = QueryList::Query($urllist,array( 'link'=>array('#threadlisttableid .new a','href','-em'), 'title'=>array('#threadlisttableid .new a','text','-em'), ),'','utf-8','gbk')->getData(function($item) use ($baseurl){ $data = array(); if(substr($item['link'],0,5)=='forum'){ return array('link'=>'','title'=>''); }else{ $data['link'] = $baseurl.$item['link']; $data['title'] = $item['title']; return $data; } }); print_r($ql1);exit; // 论坛某页面内容 $url = 'http://bbs.51credit.com/thread-3532454-1-1.html'; //采集某页面所有的图片 $ql = QueryList::Query($url,array( 'cont' => array('.t_fsz','text') ),'','utf-8','gbk') ->getData(function($item){ $data=$item['cont']; if(preg_match('/\(\);([\s\S]*)/i',$item['cont'],$as)){ $data = $as[1]; } return trim($data); }); //打印结果 print_r($ql);exit; // exit; /*************************************************/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
<?php class IndexAction extends BaseAction{ private $user_mod; //初始化 protected function _initialize() { parent::_initialize(); $this->user_mod = D('user'); } //404 public function index() { $this->_empty(); } //检测邮箱是否可以注册 public function checkemail() { if($this->isAjax()) { $email = $_POST['email']; $count = $this->user_mod->where('email="'.$email.'"')->count(); if($count) { $this->ajaxReturn(array('err' => 0,'msg' => '邮箱已存在!')); } $this->ajaxReturn(array('err' => 1,'msg' => '可以注册!')); } } //检测昵称是否可以注册 public function checkusername() { if($this->isAjax()) { $username = $_POST['username']; $count = $this->user_mod->where('username="'.$username.'"')->count(); if($count) { $this->ajaxReturn(array('err' => 0,'msg' => '昵称已存在!')); } $this->ajaxReturn(array('err' => 1,'msg' => '可以注册!')); } } //登陆 public function login() { //判断用户是否登陆 if($this->user) header('Location:/user'); if($this->isAjax()) { $username = trim($_POST['username']); $password = $_POST['password']; if(trim($username) == '' || $password == '') { $this->ajaxReturn(array('err' => 0,'msg' => '请填写用户名或密码!')); } if(strlen($password) < 6 || strlen($password) > 12) { $this->ajaxReturn(array('err' => 0,'msg' => '密码格式不正确!')); } $user = $this->user_mod->where("(username='".$username."' or phone='".$username."' or email='".$username."') and password='".$this->substr_pwd($password)."'")->find(); if(!$user) { $this->ajaxReturn(array('err' => 0,'msg' => '用户名或密码错误!')); } if($user['status'] == C('USER_STATUS_DISABLE')) { $this->ajaxReturn(array('err' => 0,'msg' => '此用户已被禁用!')); } if($user['status'] == C('USER_STATUS_UNACTIVATE')) { $this->ajaxReturn(array('err' => 0,'msg' => '该账号未激活!')); } //如果设置30天自动登设陆 设置本程序cookie $login_time = time(); if(isset($_POST['is_save']) && $_POST['is_save'] == 30) { $day = 30; $key = md5($user['id'].$user['username'].$login_time); cookie('user[id]',$user['id'],3600*24*$day); cookie('user[username]',$user['username'],3600*24*$day); cookie('user[login_time]',$login_time,3600*24*$day); cookie('user[key]',$key,3600*24*$day); } if($this->user_mod->where('id='.$user['id'])->save(array('last_login_ip' => $_SERVER['REMOTE_ADDR'], 'last_login_time' => $login_time))) { $_SESSION['xj_id'] = $user['id']; $_SESSION['xj_expire'] = time() + C('SESSION_EXPIRE'); $_SESSION['start_time'] = time(); $this->ajaxReturn(array('err' => 1,'msg' => '登录成功!')); } } $this->assign('title','用户登录'); $this->display(); } //注册 public function register() { //判断用户是否登陆 if($this->user) header('Location:/user'); //处理表单数据 if($this->isAjax()) { $data = $this->user_mod->create(); $code = trim($_POST['code']); //验证码 if ($_SESSION['verify'] != md5($code)) { $this->ajaxReturn(array('err' => 0,'msg' => '验证码错误!')); } if(trim($data['username']) == '' || trim($data['email']) == '' || $data['password'] == '') { $this->ajaxReturn(array('err' => 0,'msg' => '请完善注册信息!')); } if(strlen($data['password']) < 6 || strlen($data['password']) > 12) { $this->ajaxReturn(array('err' => 0,'msg' => '密码长度为6-12个字符!')); } if($data['password'] != $_POST['confirm_password']) { $this->ajaxReturn(array('err' => 0,'msg' => '两次密码不一样!')); } if(mb_strlen($data['username'],'utf8') < 2 || mb_strlen($data['username'], 'utf8') > 8) { $this->ajaxReturn(array('err' => 0,'msg' => '昵称长度为2-8个字符!')); } if($this->user_mod->where("username='".trim($data['username'])."' or email='".trim($data['email'])."'")->count()){ $this->ajaxReturn(array('err' => 0,'msg' => '昵称或邮箱已存在!')); } $time = time(); $data['password'] = $this->substr_pwd($data['password']); $data['created_time'] = $time; $data['register_ip'] = $_SERVER['REMOTE_ADDR']; $data['last_login_ip'] = $_SERVER['REMOTE_ADDR']; $data['last_login_time'] = $time; $data['level'] = C('USER_DEFAULT_LEVEL'); $data['status'] = C('USER_STATUS_UNACTIVATE'); $data['money'] = C('USER_DEFAULT_MONEY'); $data['avatar'] = '/uploads/avatar/avatar_'.$data['sex'].'.png'; if($user_id = $this->user_mod->add($data)) { $user = D('user')->find($user_id); //设置过期时间 //$_SESSION['xj_id'] = $user['id']; //$_SESSION['xj_expire'] = $time + C('SESSION_EXPIRE'); //发送注册成功邮件 $address = $data['email']; $title = '恭喜您注册成功!'; $token = md5($user['id'].$user['username'].$user['email'].$user['created_time']); $url = C('SITE_DOMAIN').'/account/activate?email='.$data['email'].'&token='.$token; $content = '请点击该链接<a href="'.$url.'"> '.$url.'</a> ,完成您的注册,激活该账号!'; $this->send_email($address,$title,$content); $this->ajaxReturn(array('err' => 1,'msg' => C('SITE_DOMAIN').'/account/registersuccess?email='.$data['email'])); } } $this->assign('title','用户注册'); $this->display(); } //注册成功 public function registersuccess() { $email = isset($_GET['email']) ? trim($_GET['email']) : ''; if($email) { $url = 'mail.'.substr($email, strpos($email, '@') + 1); $this->assign('email',$email); $this->assign('url', $url); $this->assign('title','注册成功'); $this->display(); }else { $this->msg('错误提示','非法访问!'); } } //激活账户 public function activate() { $email = isset($_GET['email']) ? trim($_GET['email']) : ''; $token = isset($_GET['token']) ? trim($_GET['token']) : ''; if($email && $token) { $user = $this->user_mod->where("email='".$email."'")->find(); $k = md5($user['id'].$user['username'].$user['email'].$user['created_time']); if($token == $k) { if($user && $user['status'] == C('USER_STATUS_UNACTIVATE')) { $this->user_mod->where('id='.$user['id'])->save(array('status' => C('USER_STATUS_NORMAL'))); } $this->assign('title','激活账户'); $this->display(); } }else { $this->msg('错误提示','非法访问!'); } } //找回密码 public function forgetpassword() { //判断用户是否登陆 if($this->user) header('Location:/user'); //处理表单数据 if($this->isAjax()) { $email = trim($_POST['email']); $code = trim($_POST['code']); if($email == '' || $code == '') { $this->ajaxReturn(array('err' => 0,'msg' => '邮箱和验证码不能为空!')); } //验证码 if ($_SESSION['verify'] != md5($code)) { $this->ajaxReturn(array('err' => 0,'msg' => '验证码错误!')); } $user = $this->user_mod->where("email='".$email."'")->find(); if(!$user) { $this->ajaxReturn(array('err' => 0,'msg' => '邮箱不存在!')); } $password = $this->rand_six_num(); //发送注册成功邮件 $address = $email; $title = '找回密码!'; $content = '以下是系统随机为您生成的临时密码,登陆后请修改!您的密码:'.$password; $this->send_email($address,$title,$content); //修改密码 $this->user_mod->where('id='.$user['id'])->save(array('password' => $this->substr_pwd($password))); $this->ajaxReturn(array('err' => 1,'msg' => '邮件已发送,请前往您的邮箱查收!')); } $this->assign('title','找回密码'); $this->display(); } public function qqlogin(){ $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'callback'; $_SESSION['state'] = md5(uniqid(rand(), TRUE)); $redirect_uri = $this->setting['site_domain'] . "/account/qq".$type; $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" . $this->setting['qq_app_key'] . "&redirect_uri=" . urlencode($redirect_uri) . "&state=" . $_SESSION['state']; header("Location:$login_url"); } public function qqcallback(){ $user_openid_mod = D('user_openid'); if($_REQUEST['state'] == $_SESSION['state']) //csrf { $token_url = "https://graph.qq.com/oauth2.0/token"; $aGetParam = array( "grant_type" => "authorization_code", "client_id" => $this->setting['qq_app_key'], "client_secret" => $this->setting['qq_app_secret'], "code" => $_REQUEST["code"], "redirect_uri" => $this->setting['site_domain']."/account/qqcallback" ); $res = $this->get_url($token_url,$aGetParam); if(trim($res)==''){ exit('无法获取认证!<br/>'); } if (strpos($res, "callback") !== false) { $lpos = strpos($res, "("); $rpos = strrpos($res, ")"); $res = substr($res, $lpos + 1, $rpos - $lpos -1); $msg = json_decode($res); if (isset($msg->error)) { echo "<h3>error:</h3>" . $msg->error; echo "<h3>msg :</h3>" . $msg->error_description; exit; } } parse_str($res, $res); $_SESSION["access_token"] = $res['access_token']; } $url = "https://graph.qq.com/oauth2.0/me"; $str=$this->get_url($url,array('access_token'=>$_SESSION['access_token'])); if (strpos($str, "callback") !== false) { $lpos = strpos($str, "("); $rpos = strrpos($str, ")"); $str = substr($str, $lpos + 1, $rpos - $lpos -1); } $res = json_decode($str); $_SESSION['openid']=$res->openid; $user_openid=$user_openid_mod->where("openid='".$res->openid."' and type='qq'")->find(); $is_new=false; if($user_openid){ $user=D('user')->where('openid="'.$res->openid.'"')->find(); if(count($user)>0){ //第二次登录 $_SESSION['xj_id'] = $user['id']; $_SESSION['xj_expire'] = time() + C('SESSION_EXPIRE'); if(D('user')->where('id='.$user['id'])->save(array('last_login_ip' => $_SERVER['REMOTE_ADDR'], 'last_login_time' => $last_time))) { header('Location:'.$this->setting['site_domain']); } }else{ $user_openid_mod->where("openid='".$res->openid."'")->delete(); $is_new=true; } }else{ $is_new=true; } if($is_new){ $url="https://graph.qq.com/user/get_user_info"; $param=array( 'access_token'=>$_SESSION['access_token'], "openid"=>$_SESSION['openid'], "oauth_consumer_key"=>$this->setting['qq_app_key'], "format"=>'json', ); $res= $this->get_url($url,$param); if($res==false){ exit('获取用户信息失败!'); } $res=json_decode($res); $qq_info=array('user_info'=>$res); $data=array( 'username'=>$res->nickname, 'openid' => $_SESSION['openid'], 'avatar' => $res->figureurl_2, //'img'=>$res->figureurl_2, 'last_login_time'=>time(), 'last_login_ip'=>$_SERVER['REMOTE_ADDR'], 'created_time'=>time(), 'status'=>C('USER_STATUS_UNBIND'), 'register_ip'=>$_SERVER['REMOTE_ADDR'], 'level' => C('USER_DEFAULT_LEVEL'), 'money' => C('USER_DEFAULT_MONEY'), ); $user_id = $this->user_mod->add($data); $_SESSION['xj_id'] = $user_id; $_SESSION['xj_expire'] = time() + C('SESSION_EXPIRE'); $data=array( 'type'=>'qq', 'uname'=>$data['username'], 'openid'=>$_SESSION['openid'], 'info'=>serialize($qq_info), ); $user_openid_mod->add($data); } header('Location:'.$this->setting['site_domain']);exit; } public function wblogin(){ $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'callback'; $_SESSION['state'] = md5(uniqid(rand(), TRUE)); $redirect_uri = $this->setting['site_domain'] . "/account/wb".$type; $login_url = "https://api.weibo.com/oauth2/authorize?client_id=".$this->setting['wb_app_key']."&response_type=code&redirect_uri=".urlencode($redirect_uri). "&state=" . $_SESSION['state']; header("Location:$login_url"); } public function wbcallback(){ $user_openid_mod = D('user_openid'); if($_REQUEST['state'] == $_SESSION['state']) //csrf { $token_url = "https://api.weibo.com/oauth2/access_token"; $aGetParam = array( "grant_type" => "authorization_code", "client_id" => $this->setting['wb_app_key'], "client_secret" => $this->setting['wb_app_secret'], "code" => $_REQUEST["code"], "redirect_uri" => $this->setting['site_domain']."/account/wbcallback" ); $res = $this->post_url($token_url,$aGetParam); if(trim($res)==''){ exit('无法获取认证!<br/>'); } /* if (strpos($res, "callback") !== false) { $lpos = strpos($res, "("); $rpos = strrpos($res, ")"); $res = substr($res, $lpos + 1, $rpos - $lpos -1); $msg = json_decode($res); if (isset($msg->error)) { echo "<h3>error:</h3>" . $msg->error; echo "<h3>msg :</h3>" . $msg->error_description; exit; } }*/ //parse_str($res, $res); $res = json_decode($res); $_SESSION["access_token"] = $res->access_token; } /* $url = "https://api.weibo.com/oauth2/get_token_info"; $str=$this->post_url($url,array('access_token'=>$_SESSION['access_token'])); if (strpos($str, "callback") !== false) { $lpos = strpos($str, "("); $rpos = strrpos($str, ")"); $str = substr($str, $lpos + 1, $rpos - $lpos -1); } $res = json_decode($str);*/ $_SESSION['openid']=$res->uid; $user_openid=$user_openid_mod->where("openid='".$res->uid."' and type='wb'")->find(); $is_new=false; if($user_openid){ $user=D('user')->where('openid="'.$res->uid.'"')->find(); if(count($user)>0){ //第二次登录 $_SESSION['xj_id'] = $user['id']; $_SESSION['xj_expire'] = time() + C('SESSION_EXPIRE'); if(D('user')->where('id='.$user['id'])->save(array('last_login_ip' => $_SERVER['REMOTE_ADDR'], 'last_login_time' => $last_time))) { header('Location:'.$this->setting['site_domain']); } }else{ $user_openid_mod->where("openid='".$res->uid."'")->delete(); $is_new=true; } }else{ $is_new=true; } if($is_new){ $url="https://api.weibo.com/2/users/show.json"; $param=array( 'access_token'=>$_SESSION['access_token'], "uid"=>$_SESSION['openid'], ); $res= $this->get_url($url,$param); if($res==false){ exit('获取用户信息失败!'); } $res=json_decode($res); $wb_info=array('user_info'=>$res); $data=array( 'username'=>$res->name, 'openid' => $_SESSION['openid'], 'avatar' => $res->avatar_large, //'img'=>$res->figureurl_2, 'last_login_time'=>time(), 'last_login_ip'=>$_SERVER['REMOTE_ADDR'], 'created_time'=>time(), 'status'=>C('USER_STATUS_UNBIND'), 'register_ip'=>$_SERVER['REMOTE_ADDR'], 'level' => C('USER_DEFAULT_LEVEL'), 'money' => C('USER_DEFAULT_MONEY'), ); $user_id = $this->user_mod->add($data); $_SESSION['xj_id'] = $user_id; $_SESSION['xj_expire'] = time() + C('SESSION_EXPIRE'); $data=array( 'type'=>'wb', 'uname'=>$data['username'], 'openid'=>$_SESSION['openid'], 'info'=>serialize($wb_info), ); $user_openid_mod->add($data); } header('Location:'.$this->setting['site_domain']);exit; } //退出 public function logout() { //清除session unset($_SESSION['xj_id']); unset($_SESSION['xj_expire']); //清除cookie cookie('user[id]',null); cookie('user[username]',null); cookie('user[login_time]',null); cookie('user[key]',null); echo "<script>location='/';</script>"; } /* * GET请求 */ private function get_url($sUrl,$aGetParam){ $oCurl = curl_init(); if(stripos($sUrl,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); } $aGet = array(); foreach($aGetParam as $key=>$val){ $aGet[] = $key."=".urlencode($val); } curl_setopt($oCurl, CURLOPT_URL, $sUrl."?".join("&",$aGet)); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($this->config["debug"])===1){ echo "<tr><td class='narrow-label'>请求地址:</td><td><p re>".$sUrl."</p re></td></tr>"; echo "<tr><td class='narrow-label'>GET参数:</td><td><p re>".var_export($aGetParam,true)."</p re></td></tr>"; echo "<tr><td class='narrow-label'>请求信息:</td><td><p re>".var_export($aStatus,true)."</p re></td></tr>"; if(intval($aStatus["http_code"])==200){ echo "<tr><td class='narrow-label'>返回结果:</td><td><p re>".$sContent."</p re></td></tr>"; if((@$aResult = json_decode($sContent,true))){ echo "<tr><td class='narrow-label'>结果集合解析:</td><td><p re>".var_export($aResult,true)."</p re></td></tr>"; } } } if(intval($aStatus["http_code"])==200){ return $sContent; }else{ //echo "<tr><td class='narrow-label'>返回出错:</td><td><p re>".$aStatus["http_code"].",请检查参数或者确实是腾讯服务器出错咯。</p re></td></tr>"; return FALSE; } } /* * POST 请求 */ private function post_url($sUrl,$aPOSTParam){ $oCurl = curl_init(); if(stripos($sUrl,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); } $aPOST = array(); foreach($aPOSTParam as $key=>$val){ $aPOST[] = $key."=".urlencode($val); } curl_setopt($oCurl, CURLOPT_URL, $sUrl); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($oCurl, CURLOPT_POST,true); curl_setopt($oCurl, CURLOPT_POSTFIELDS, join("&", $aPOST)); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($this->config["debug"])===1){ echo "<tr><td class='narrow-label'>请求地址:</td><td><p re>".$sUrl."</p re></td></tr>"; echo "<tr><td class='narrow-label'>POST参数:</td><td><p re>".var_export($aPOSTParam,true)."</p re></td></tr>"; echo "<tr><td class='narrow-label'>请求信息:</td><td><p re>".var_export($aStatus,true)."</p re></td></tr>"; if(intval($aStatus["http_code"])==200){ echo "<tr><td class='narrow-label'>返回结果:</td><td><p re>".$sContent."</p re></td></tr>"; if((@$aResult = json_decode($sContent,true))){ echo "<tr><td class='narrow-label'>结果集合解析:</td><td><p re>".var_export($aResult,true)."</p re></td></tr>"; } } } if(intval($aStatus["http_code"])==200){ return $sContent; }else{ echo "<tr><td class='narrow-label'>返回出错:</td><td><p re>".$aStatus["http_code"].",请检查参数或者确实是腾讯服务器出错咯。</p re></td></tr>"; return FALSE; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
<?php function UTF82JS($str) { $str=iconv("UTF-8","UNICODELITTLE",$str); $js=""; for($i=0;$i<strlen($str);$i+=2){ $js.=sprintf("%%u%02x%02x",ord($str[$i+1]),ord($str[$i])); } return $js; } function JS2UTF8($str) { // $str = strtr($arr,array('\\u'=>'%u')); //php的可以用这个 $unicode=''; for($i=0;$i<strlen($str);$i+=6){ list($a,$b)=sscanf(substr($str,$i),"%%u%02x%02x"); $unicode.=chr($b).chr($a); } echo iconv("UNICODELITTLE","UTF-8",$unicode); } $arr = array('a'=>'张三'); // $text =json_encode($arr); // echo iconv("UNICODELITTLE", "UTF-8", $text); header('content-type:text/html;charset=utf-8'); $arr = json_encode($arr,JSON_UNESCAPED_UNICODE); echo $arr; // echo json_encode($arr); // echo JS2UTF8(json_encode($arr)); // echo urlencode(json_encode($arr)); // $a = '{"a":"\u5f20\u4e09"}'; // $b = '{"b":"李四"}'; // echo UTF82JS($b); // echo unicode_decode($a); 5.4里面的处理 Php代码 json_encode($str, JSON_UNESCAPED_UNICODE); 5.4之前,有两种方法处理 方法1. Php代码 function encode_json($str){ $code = json_encode($str); return preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code); } 方法1.在实际应用中有个问题,部分字符会掉,不止为何,如字符串:"日期11.2"会被变成"日期.2" 方法2. 先对需要处理的做urlencode处理,然后json_encode,最后做urldecode处理 Php代码 function encode_json($str) { return urldecode(json_encode(url_encode($str))); } /** * */ function url_encode($str) { if(is_array($str)) { foreach($str as $key=>$value) { $str[urlencode($key)] = url_encode($value); } } else { $str = urlencode($str); } return $str; } function unicode_decode($name){ // 转换编码,将Unicode编码转换成可以浏览的utf-8编码 $pattern = '/([\w]+)|(\\\u([\w]{4}))/i'; preg_match_all($pattern, $name, $matches); if (!empty($matches)){ $name = ''; for ($j = 0; $j < count($matches[0]); $j++){ $str = $matches[0][$j]; if (strpos($str, '\\u') === 0){ $code = base_convert(substr($str, 2, 2), 16, 10); $code2 = base_convert(substr($str, 4), 16, 10); $c = chr($code).chr($code2); $c = iconv('UCS-2', 'UTF-8', $c); $name .= $c; }else{ $name .= $str; } } } return $name; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
<script type="text/javascript"> $(document).ready(function() { $('.list-num:lt(3)').addClass('list-numH'); //鼠标移到下载上面,下载图标变成橙色 var xzImg = $('.block-text-a').find('img').attr('src'); var xzImgH = xzImg.replace('.png', 'o.png'); $('.block-text-a').hover(function () { $(this).find('img').attr('src', xzImgH); }, function () { $(this).find('img').attr('src', xzImg); }); $('#zan').click(function(){ var zan=$(this); var id = zan.attr("rel"); $.post("{:U('index/pub/checkLogin')}",function(data){ if(!data){ layer.alert( ' - 您还未登录,现在跳转到登录界面'); setTimeout(function(){ window.location.href = "{:U('index/user/index')}"; },2000); } else{ $.ajax({ url:"{:U('index/user/zan')}", type:"post", data:"id="+id, datatype: "json" , cache:false, success:function(ret){ Zan(ret,"#zan"); } }); } }); }); //收藏 $('#collect').click(function(){ var collect=$(this); var id = collect.attr("rel"); $.post("{:U('index/pub/checkLogin')}",function(data){ if(!data){ layer.alert( ' - 您还未登录,现在跳转到登录界面'); setTimeout(function(){ window.location.href = "{:U('index/user/index')}"; },2000); }else{ $.ajax({ url:"{:U('index/user/collect')}", type:"post", data:"id="+id, datatype: "json" , cache:false, success:function(returned){ eval("var returned="+returned); if(returned.status =='ok') layer.alert(returned.msg); else layer.alert(returned.msg); } }); } }); }); var u = location.href; arr = u.substr(u.lastIndexOf('/')+1).split('.'); window.i=arr[0]; getpage(); }); function getList(obj,name){ // now-a // var u = location.href; // arr = u.substr(u.lastIndexOf('/')+1).split('.'); now = obj.id.split('_'); if(now[0] === 'price'){ if(now[1] === '2' ){ //精品 $('#price_0').removeClass('price').removeClass('now-a'); $('#price_2').addClass('now-a').addClass('price'); }else{ //免费 $('#price_2').removeClass('price').removeClass('now-a'); $('#price_0').addClass('now-a').addClass('price'); } }else if(now[0] === 'dow'||now[0] === 'feng'){ $(obj).parent().parent().find('.'+now[0]).removeClass(now[0]); $(obj).addClass(now[0]); $(obj).parent().parent().prev('button').empty().html($(obj).text()+' <span class="caret"></span>'); // .html($(obj).text()+' <span class="caret"></span>'); }else{ } where = $('.price').attr('id')+'|'+$('.feng').attr('id'); order = $('.dow').attr('id'); data = { 'name':name?name:window.i, 'where':where, 'order':order, 'ajax':1, 'page':$('.fyq').find('.activeone').attr('page'), }; $.ajax({ url: '/index/projectmanage/content', type: 'POST', dataType: 'json', data: data, success:function(json){ // alert(json.msg); if(json.data.div!==''){ $('#conte').empty().html(json.data.div); $('.fyq').empty().html(json.data.page); }else{ $('#conte').empty(); $('.fyq').empty(); } // console.log(json); } }); } function setvalue(obj){ if(obj.id.substr(4) === 'dow'){ $(obj).empty().html('最新 <span class="caret"></span>'); $(obj).next('ul').find('.dow').removeClass('dow'); }else{ $(obj).empty().html('全部风格 <span class="caret"></span>'); $(obj).next('ul').find('.feng').removeClass('feng'); } } function getpage(page,count){ count = count?count:{$count}; page = page?page:1; $.ajax({ url: '/index/projectmanage/ajax_ym_page', type: 'POST', dataType: 'html', data: { 'count':count, 'page':page, }, success:function(data){ $('.fyq').empty().html(data); // console.log(data); } }); } function getPageInfo(obj){ // console.log(window.i); $(obj).parent().find('.activeone').removeClass('activeone'); $(obj).find('span').addClass('activeone'); // console.log(obj); obj.id = 'page_s'; getList(obj,window.i); } // function getList2(where){ // // alert(location.href); // // window.location.href = '?v='+where; // } function getContent(obj){ var arr = obj.id.split('_'); window.i = arr[1]; getList(obj,arr[1]); } </script> |
最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数。
你可能自己写过无数个GET和POST请求,或者已经看过很多权威网站总结出的他们的区别,你非常清楚知道什么时候该用什么。
当你在面试中被问到这个问题,你的内心充满了自信和喜悦。
你轻轻松松的给出了一个“标准答案”:
(本标准答案参考自w3schools)
“很遗憾,这不是我们要的回答!”
请告诉我真相。。。
如果我告诉你GET和POST本质上没有区别你信吗?
让我们扒下GET和POST的外衣,坦诚相见吧!
GET和POST是什么?HTTP协议中的两种发送请求的方法。
HTTP是什么?HTTP是基于TCP/IP的关于数据如何在万维网中如何通信的协议。
HTTP的底层是TCP/IP。所以GET和POST的底层也是TCP/IP,也就是说,GET/POST都是TCP链接。GET和POST能做的事情是一样一样的。你要给GET加上request body,给POST带上url参数,技术上是完全行的通的。
那么,“标准答案”里的那些区别是怎么回事?
在我大万维网世界中,TCP就像汽车,我们用TCP来运输数据,它很可靠,从来不会发生丢件少件的现象。但是如果路上跑的全是看起来一模一样的汽车,那这个世界看起来是一团混乱,送急件的汽车可能被前面满载货物的汽车拦堵在路上,整个交通系统一定会瘫痪。为了避免这种情况发生,交通规则HTTP诞生了。HTTP给汽车运输设定了好几个服务类别,有GET, POST, PUT, DELETE等等,HTTP规定,当执行GET请求的时候,要给汽车贴上GET的标签(设置method为GET),而且要求把传送的数据放在车顶上(url中)以方便记录。如果是POST请求,就要在车上贴上POST的标签,并把货物放在车厢里。当然,你也可以在GET的时候往车厢内偷偷藏点货物,但是这是很不光彩;也可以在POST的时候在车顶上也放一些数据,让人觉得傻乎乎的。HTTP只是个行为准则,而TCP才是GET和POST怎么实现的基本。
但是,我们只看到HTTP对GET和POST参数的传送渠道(url还是requrest body)提出了要求。“标准答案”里关于参数大小的限制又是从哪来的呢?
在我大万维网世界中,还有另一个重要的角色:运输公司。不同的浏览器(发起http请求)和服务器(接受http请求)就是不同的运输公司。 虽然理论上,你可以在车顶上无限的堆货物(url中无限加参数)。但是运输公司可不傻,装货和卸货也是有很大成本的,他们会限制单次运输量来控制风险,数据量太大对浏览器和服务器都是很大负担。业界不成文的规定是,(大多数)浏览器通常都会限制url长度在2K个字节,而(大多数)服务器最多处理64K大小的url。超过的部分,恕不处理。如果你用GET服务,在request body偷偷藏了数据,不同服务器的处理方式也是不同的,有些服务器会帮你卸货,读出数据,有些服务器直接忽略,所以,虽然GET可以带request body,也不能保证一定能被接收到哦。
好了,现在你知道,GET和POST本质上就是TCP链接,并无差别。但是由于HTTP的规定和浏览器/服务器的限制,导致他们在应用过程中体现出一些不同。
你以为本文就这么结束了?
我们的大BOSS还等着出场呢。。。
这位BOSS有多神秘?当你试图在网上找“GET和POST的区别”的时候,那些你会看到的搜索结果里,从没有提到他。他究竟是什么呢。。。
GET和POST还有一个重大区别,简单的说:
GET产生一个TCP数据包;POST产生两个TCP数据包。
长的说:
对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器响应200(返回数据);
而对于POST,浏览器先发送header,服务器响应100 continue,浏览器再发送data,服务器响应200 ok(返回数据)。
也就是说,GET只需要汽车跑一趟就把货送到了,而POST得跑两趟,第一趟,先去和服务器打个招呼“嗨,我等下要送一批货来,你们打开门迎接我”,然后再回头把货送过去。
因为POST需要两步,时间上消耗的要多一点,看起来GET比POST更有效。因此Yahoo团队有推荐用GET替换POST来优化网站性能。但这是一个坑!跳入需谨慎。为什么?
1. GET与POST都有自己的语义,不能随便混用。
2. 据研究,在网络环境好的情况下,发一次包的时间和发两次包的时间差别基本可以无视。而在网络环境差的情况下,两次包的TCP在验证数据包完整性上,有非常大的优点。
3. 并不是所有浏览器都会在POST中发送两次包,Firefox就只发送一次。
现在,当面试官再问你“GET与POST的区别”的时候,你的内心是不是这样的?