PHP 网站安装实现程序

 更新时间:2016年11月25日 16:19  点击:1628
网站安装过程我们需要几处非常简单,一个是我们要让用户输入用户名密码然后连接数据库之后再把我们以前准备好.sql文件利用php读取并执行,最后简单配置一下站点,这样一个完整的php网站安装过程就完美的搞定了。

这次顺便做了一个install.php才发现难度其实并不大,还是文件写入操作而已,安装其实主要操作的还是数据库里的内容,先来看看文件里怎么写:(还是用的Codeigiter,对于使用其他框架或者手写而言,仅思路可参考,用了挺多CI自带的helper或者是library的)

 代码如下 复制代码
 $content = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');n";
 $content .= '$active_group'. "= 'default';n";
 $content .= '$active_record'." = TRUE;n";
 $content .= '$db'."['default']['hostname'] = '".$this->input->post('hostname')."';n";
 $content .= '$db'."['default']['username'] = '".$this->input->post('rootname')."';n";
 $content .= '$db'."['default']['password'] = '".$this->input->post('pass')."';n";
 $content .= '$db'."['default']['database'] = '".$this->input->post('book')."';n";
 $content .= '$db'."['default']['dbdriver'] = 'mysql';n";
 $content .= '$db'."['default']['dbprefix'] = '';n";
 $content .= '$db'."['default']['pconnect'] = TRUE;n";
 $content .= '$db'."['default']['db_debug'] = TRUE;n";
 $content .= '$db'."['default']['cache_on'] = FALSE;n";
 $content .= '$db'."['default']['cachedir'] = '';n";
 $content .= '$db'."['default']['char_set'] = 'utf8';n";
 $content .= '$db'."['default']['dbcollat'] = 'utf8_general_ci';n";
 $content .= '$db'."['default']['swap_pre'] = '';n";
 $content .= '$db'."['default']['autoinit'] = TRUE;n";
 $content .= '$db'."['default']['stricton'] = FALSE;";

在文件里用n来换行,因为里面包括了PHP的代码,这导致了我们只能用双引号避免冲突(否则的话就得用了,感觉工作量更大),针对$db,直接显示必须要用单引号,于是就出现了这个。

写入文件之后,接着我们需要做的是执行一系列安装操作,也就是CREATE TABLE,以及创建一个新用户用于登陆,在model里,我这么写:

 

 代码如下 复制代码
function install() {
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_category'")) == 0)
 $this->db->query("CREATE TABLE pr_category(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, category VARCHAR(100) NOT NULL UNIQUE, deadline INT NOT NULL)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_college'")) == 0)
 $this->db->query("CREATE TABLE pr_college(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL UNIQUE)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_level'")) == 0)
 $this->db->query("CREATE TABLE pr_level(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, level INT NOT NULL, name VARCHAR(20) NOT NULL)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_rates'")) == 0)
 $this->db->query("CREATE TABLE pr_rates(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, pid INT NOT NULL, ip VARCHAR(40) NOT NULL, category INT NOT NULL)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_users'")) == 0)
 $this->db->query("CREATE TABLE pr_users(uid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(45) NOT NULL UNIQUE, password VARCHAR(50) NOT NULL, level INT NOT NULL )");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_works'")) == 0)
 $this->db->query("CREATE TABLE pr_works(pid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100) NOT NULL, content TEXT, realname VARCHAR(20) NOT NULL, studentnum VARCHAR(20) NOT NULL, college INT NOT NULL, filename TEXT NOT NULL, category INT NOT NULL)");
 $query1 = $this->db->get_where('pr_level',array('level' => 1, 'name' => '普通用户'));
 $query2 = $this->db->get_where('pr_level',array('level' => 5, 'name' => '管理员'));
 $query3 = $this->db->get_where('pr_level',array('level' => 99, 'name' => '超级管理员'));
 if ($query1->num_rows() == 0)
 $this->db->query("INSERT INTO pr_level(level, name) VALUES (1, '普通用户')");
 if ($query2->num_rows() == 0)
 $this->db->query("INSERT INTO pr_level(level, name) VALUES (5, '管理员')");
 if ($query3->num_rows() == 0)
 $this->db->query("INSERT INTO pr_level(level, name) VALUES (99, '超级管理员')");
 $this->username = $this->input->post('username');
 $this->password = $this->input->post('password');
 $this->level = 99;
 $query4 = $this->db->get_where('pr_users',array('username' => $this->input->post('username')));
 if ($query4->num_rows() == 0) {
 $this->db->insert('pr_users', $this);
 return TRUE;
 } else {
 return FALSE;
 }
 }

其实这么写查询量很大效率又低,不过这有效的避免了上一次安装被打断之后重新安装遇到的麻烦,检测是否已经创建了某个表,是否已经有新用户了之类的。

 代码如下 复制代码
mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_category'"))

这句可以查询数据库中是否存在这个表。

执行完如果顺利的话,将install.php改个名字:

 代码如下 复制代码
 rename('application/controllers/install.php', 'application/controllers/install.lock');

在其他文件里加入检测install.php是否存在,如果存在的话就跳转到install.php,这样就做好了简单的安装流程了(必须放在model前,否则会提示没有数据库而不会跳转)。

 代码如下 复制代码
 if (file_exists('application/controllers/install.php'))
 redirect('install');

至于什么是否存在表之类的,因为CI会check而且优先级也高于我们自己写的错误提示,所以这里就不加了。

完整源码(MVC):
Controller:

 代码如下 复制代码

 <?php
class Install extends CI_Controller {
 function __construct() {
 parent::__construct();
 $this->load->helper('url');
 $this->load->helper('file');
 }
 function index() {
 $data['title'] = '安装向导';
 $this->load->helper('form');
 $this->load->library('form_validation');
 $this->form_validation->set_rules('hostname', '主机名', 'trim|required|xss_clean');
 $this->form_validation->set_rules('rootname', '数据库用户名', 'trim|required|xss_clean');
 $this->form_validation->set_rules('username', '用户名', 'trim|required|xss_clean');
 $this->form_validation->set_rules('password', '密码', 'trim|required|xss_clean|md5');
 $data['error'] = '';
 if ($this->form_validation->run() == FALSE) {
 $this->load->view('install', $data);
 } else {
 $config['hostname'] = $this->input->post('hostname');
 $config['username'] = $this->input->post('rootname');
 $config['password'] = $this->input->post('pass');
 $config['database'] = $this->input->post('book');
 $config['dbdriver'] = 'mysql';
 $config['dbprefix'] = '';
 $config['pconnect'] = TRUE;
 $config['db_debug'] = TRUE;
 $config['cache_on'] = FALSE;
 $config['cachedir'] = '';
 $config['char_set'] = 'utf8';
 $config['dbcollat'] = 'utf8_general_ci';
 $config['swap_pre'] = '';
 $config['autoinit'] = TRUE;
 $config['stricton'] = FALSE;
 if ($this->load->database($config, TRUE)) {
 $content = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');n";
 $content .= '$active_group'. "= 'default';n";
 $content .= '$active_record'." = TRUE;n";
 $content .= '$db'."['default']['hostname'] = '".$this->input->post('hostname')."';n";
 $content .= '$db'."['default']['username'] = '".$this->input->post('rootname')."';n";
 $content .= '$db'."['default']['password'] = '".$this->input->post('pass')."';n";
 $content .= '$db'."['default']['database'] = '".$this->input->post('book')."';n";
 $content .= '$db'."['default']['dbdriver'] = 'mysql';n";
 $content .= '$db'."['default']['dbprefix'] = '';n";
 $content .= '$db'."['default']['pconnect'] = TRUE;n";
 $content .= '$db'."['default']['db_debug'] = TRUE;n";
 $content .= '$db'."['default']['cache_on'] = FALSE;n";
 $content .= '$db'."['default']['cachedir'] = '';n";
 $content .= '$db'."['default']['char_set'] = 'utf8';n";
 $content .= '$db'."['default']['dbcollat'] = 'utf8_general_ci';n";
 $content .= '$db'."['default']['swap_pre'] = '';n";
 $content .= '$db'."['default']['autoinit'] = TRUE;n";
 $content .= '$db'."['default']['stricton'] = FALSE;";
 if (write_file('application/config/database.php', $content)) {
 $this->load->model('install_model');
 if ($this->install_model->install()) {
 rename('application/controllers/install.php', 'application/controllers/install.lock');
 $this->load->library('session');
 if (!empty($this->session->userdata['login'])) {
 $this->session->sess_destroy(); // destroy the session
 }
 redirect('poster_admin/login', 'refresh');
 } else {
 $data['error'] = '超级管理员用户名已存在';
 $this->load->view('install', $data);
 }
 } else {
 $data['error'] = '安装失败,无法写入文件';
 $this->load->view('install', $data);
 }
 }
 }
 }
}
?>
Model:

class Install_model extends CI_Model {
 function __construct() {
 parent::__construct();
 $this->load->database();
 $this->load->dbforge();
 }
 function install() {
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_category'")) == 0)
 $this->db->query("CREATE TABLE pr_category(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, category VARCHAR(100) NOT NULL UNIQUE, deadline INT NOT NULL)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_college'")) == 0)
 $this->db->query("CREATE TABLE pr_college(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL UNIQUE)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_level'")) == 0)
 $this->db->query("CREATE TABLE pr_level(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, level INT NOT NULL, name VARCHAR(20) NOT NULL)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_rates'")) == 0)
 $this->db->query("CREATE TABLE pr_rates(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, pid INT NOT NULL, ip VARCHAR(40) NOT NULL, category INT NOT NULL)");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_users'")) == 0)
 $this->db->query("CREATE TABLE pr_users(uid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(45) NOT NULL UNIQUE, password VARCHAR(50) NOT NULL, level INT NOT NULL )");
 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_works'")) == 0)
 $this->db->query("CREATE TABLE pr_works(pid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100) NOT NULL, content TEXT, realname VARCHAR(20) NOT NULL, studentnum VARCHAR(20) NOT NULL, college INT NOT NULL, filename TEXT NOT NULL, category INT NOT NULL)");
 $query1 = $this->db->get_where('pr_level',array('level' => 1, 'name' => '普通用户'));
 $query2 = $this->db->get_where('pr_level',array('level' => 5, 'name' => '管理员'));
 $query3 = $this->db->get_where('pr_level',array('level' => 99, 'name' => '超级管理员'));
 if ($query1->num_rows() == 0)
 $this->db->query("INSERT INTO pr_level(level, name) VALUES (1, '普通用户')");
 if ($query2->num_rows() == 0)
 $this->db->query("INSERT INTO pr_level(level, name) VALUES (5, '管理员')");
 if ($query3->num_rows() == 0)
 $this->db->query("INSERT INTO pr_level(level, name) VALUES (99, '超级管理员')");
 $this->username = $this->input->post('username');
 $this->password = $this->input->post('password');
 $this->level = 99;
 $query4 = $this->db->get_where('pr_users',array('username' => $this->input->post('username')));
 if ($query4->num_rows() == 0) {
 $this->db->insert('pr_users', $this);
 return TRUE;
 } else {
 return FALSE;
 }
 }
}
View:

<!DOCTYPE html>
<html lang="Zh-CN">
<html>
<head>
 <meta charset="utf-8"/>
 <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
 <link rel="stylesheet" href="<?php echo base_url() . 'css/bootstrap.css' ?>">
 <link rel="stylesheet" href="<?php echo base_url() . 'css/login.css' ?>">
 <link rel="icon" type="image/x-icon" href="<?php echo base_url() . 'favicon.ico' ?>"/>
 <script src="<?php echo base_url() . 'js/jquery-1.9.0.js' ?>"></script>
 <script src="<?php echo base_url() . 'js/bootstrap.js' ?>"></script>
 <title><?php echo $title; ?> - SMU Poster</title>
</head>
<body>
<div class="container">
 <div class="row">
 <div class="col-sm-4 col-sm-offset-4">
 <div <?php $err = validation_errors(); echo !(empty($error) && empty($err)) ? 'class="alert alert-danger text-center"' : '' ?>><?php echo validation_errors(); ?>
 <?php echo $error ?></div>
 <?php echo form_open('install', array('class'=>'form-horizontal','role'=>'form')); ?>
 <div class="col-sm-sm-4 col-sm-offset-4"><h2>安装向导</h2></div>
 <div class="form-group">
 <div class="col-sm-9 col-sm-offset-2">
 <div class="input-group">
 <span class="input-group-addon"><span class="glyphicon glyphicon-cloud"></span></span>
 <input type="text" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="一般为localhost" id="hostname" name="hostname" placeholder="主机名" value="localhost"/>
 </div>
 </div>
 </div>
 <div class="form-group">
 <div class="col-sm-9 col-sm-offset-2">
 <div class="input-group">
 <span class="input-group-addon"><span class="glyphicon glyphicon-book"></span></span>
 <input type="text" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="数据库名如poster,请自行创建" id="book" name="book" placeholder="数据库名"/>
 </div>
 </div>
 </div>
 <div class="form-group">
 <div class="col-sm-9 col-sm-offset-2">
 <div class="input-group">
 <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
 <input type="text" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="连接数据库的账号" id="rootname" name="rootname" placeholder="数据库账号"/>
 </div>
 </div>
 </div>
 <div class="form-group">
 <div class="col-sm-9 col-sm-offset-2">
 <div class="input-group">
 <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
 <input type="password" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="连接数据库的密码" id="pass" name="pass" placeholder="数据库密码"/>
 </div>
 </div>
 </div>
 <div class="form-group">
 <div class="col-sm-9 col-sm-offset-2">
 <div class="input-group">
 <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
 <input type="text" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="注册一个后台超级管理员账号" class="form-control" id="username" name="username" placeholder="后台登录账号"/>
 </div>
 </div>
 </div>
 <div class="form-group">
 <div class="col-sm-9 col-sm-offset-2">
 <div class="input-group">
 <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
 <input type="password" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="后台超级管理员密码" class="form-control" id="password" name="password" placeholder="后台登陆密码"/>
 </div>
 </div>
 </div>
 <div class="form-group">
 <div class="col-sm-offset-3 col-sm-9">
 <button type="submit" class="btn btn-default">开始安装</button>
 </div>
 </div>
 </form>
 </div>
 </div>
</div>
<div id="footer">
 <div class="container">
 <p class="text-center copyright text-muted">Designed By <a href="http://www.111cn.net/">微软技术俱乐部</a></p>
 </div>
</div>
</body>
</html>

SAE域名绑定之后,一般是用CNAME方式将域名绑定到应用中。但是有时候我们需要用到A记录(比如说根域名,虽然在DNSPOD上可以设置CNAME记录,但很可能会影响到MX记录),而SAE的IP地址经常改变,ping应用二级域名得到的IP没多久就失效了(前些天网站因此几天打不开都没发现,我用的是教育网,自己能打开,但是电信线路变了)。还好DNSPOD有个功能叫D监控,可以帮你监控网站能否正常打开。如果发现宕机,DNSPOD会用邮件、短信、微信等方式提醒你。这里用到的是它的另一个通知方式,那就是URL回调

 “通过DNSPod 提供的D监控 URL 回调功能,您可以让宕机或恢复信息提交到您指定的 URL 上,从而更加灵活地处理各种通知信息。”

我们可以通过宕机之后的URL回调取得相关参数,并通过DNSPOD API实现自动修改记录的功能,再通过飞信发送宕机通知。

代码在后面,先说设置方法:

dnspod-monitor-callback

设置方法

1.点此下载代码,修改其中的参数为你自己的。

2.将代码上传到网站。

3.在DNSPOD开启D监控,在通知设置中回调URL一栏填入monitorCallback.php的地址,如http://blog.gimhoy.com/monitorCallback.php?rHost=hipic.sinaapp.com.其中rHost是SAE的二级域名。并设置回调密钥。

4.Enjoy~

dnspod-monitor-callback


代码
monitorCallback.php

 代码如下 复制代码

/*
* Copyright 2007-2014 Gimhoy Studio.
*
* @author Gimhoy
* @email contact@gimhoy.com
* @version 1.0.0
*/
$rHost = $_GET['rHost']; // SAE二级域名
if(empty($rHost)) $rHost = 'sinaapp.com';
$logName = 'monitorLog.txt'; //log 文件名
$logStorDomain = 'log'; // Storage Domain
$FetionNum = ''; //飞信登陆号码
$FetionPwd = ''; //飞信登陆密码
$MobileNum = ''; //接收通知短信的号码
$callback_key = 'MYKEY'; // 添加监控时设置的密钥

$monitor_id = $_POST['monitor_id']; // 监控编号
$domain_id = $_POST['domain_id']; // 域名编号
$domain = $_POST['domain']; // 域名名称
$record_id = $_POST['record_id']; // 记录编号
$sub_domain = $_POST['sub_domain']; // 主机名称
$record_line = $_POST['record_line']; // 记录线路
$ip = $_POST['ip']; // 记录IP
$status = $_POST['status']; // 当前状态
$status_code = $_POST['status_code']; // 状态代码
$reason = $_POST['reason']; // 宕机原因
$created_at = $_POST['created_at']; // 发生时间
$checksum = $_POST['checksum']; // 校检代码

if (md5($monitor_id. $domain_id. $record_id. $callback_key. $created_at) != $checksum) {
// 非法请求
echo 'BAD REQUEST';
} else {
// 开始处理
if ($status == 'Warn' || $status == 'Ok') {
// 宕机恢复
$msg = date("Y-m-d H:i:s").' '.$sub_domain.'.'.$domain."(".$record_line." ".$ip.")宕机恢复";
} elseif ($status == 'Down') {
// 宕机
$msg = date("Y-m-d H:i:s").' '.$sub_domain.'.'.$domain."(".$record_line." ".$ip.")在".$created_at."宕机。宕机原因:".$reason."可用IP:";
$ips = @gethostbyname($rHost);
include_once 'dnspod.class.php';
$newIP = $ips;
$data = array(
'domain_id' => $domain_id,
'record_id' => $record_id,
'sub_domain' => $sub_domain,
'record_type' => 'A',
'record_line' => $record_line,
'ttl' => '600',
'value' => $newIP
);
$dnspod = new dnspod();
$response = $dnspod->api_call('Record.Modify', $data);
if(isset($response['status']['code']) && $response['status']['code'] == 1) {
$msg = $msg.$newIP.'(已切换)';
}
else {
$msg = $msg.$newIP.'(切换失败,错误代码'.$response['status']['code'].')';
}
}
//飞信通知
require_once 'Fetion.class.php';
$fetion = new PHPFetion($FetionNum, $FetionPwd);
$result = $fetion->send($MobileNum, $msg);
if(strpos($result, '短信发送成功!') || strpos($result, '发送消息成功!')) { $r = "成功。";}else{$r = "失败。";}
$s = new SaeStorage();
$content = $s -> read($logStorDomain, $logName);
$content = $content.$msg.'。飞信通知'.$r.'
';
$s -> write($logStorDomain, $logName, $content);

// 处理完成
echo 'DONE';
}

dnspod.class.php

/*
* DNSPod API PHP Web 示例
* http://www.111cn.net/
*
* Copyright 2011, Kexian Li
* Released under the MIT, BSD, and GPL Licenses.
*
*/

class dnspod {
public function api_call($api, $data) {
if ($api == '' || !is_array($data)) {
exit('内部错误:参数错误');
}
$api = 'https://dnsapi.cn/' . $api;
$data = array_merge($data, array('login_email' => 'DNSPOD登陆账号', 'login_password' => 'DNSPOD登陆密码', 'format' => 'json', 'lang' => 'cn', 'error_on_empty' => 'yes'));

$result = $this->post_data($api, $data);

if (!$result) {
exit('内部错误:调用失败');
}
$results = @json_decode($result, 1);
if (!is_array($results)) {
exit('内部错误:返回错误');
}
if ($results['status']['code'] != 1) {
exit($results['status']['message']);
}
return $results;
}
private function post_data($url, $data) {
if ($url == '' || !is_array($data)) {
return false;
}
$ch = @curl_init();
if (!$ch) {
exit('内部错误:服务器不支持CURL');
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_USERAGENT, 'Gimhoy Monitor/1.0 (contact@gimhoy.com)');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}


Fetion.class.php

 代码如下 复制代码

header('Content-Type: text/html; charset=utf-8');
/**
* PHP飞信发送类
* @author quanhengzhuang
* @version 1.5.0
*/
class PHPFetion {
/**
* 发送者手机号
* @var string
*/
protected $_mobile;
/**
* 飞信密码
* @param string
*/
protected $_password;
/**
* Cookie字符串
* @param string
*/
protected $_cookie = '';
/**
* Uid缓存
* @var array
*/
protected $_uids = array();
/**
* csrfToken
* @param string
*/
protected $_csrfToten = null;
/**
* 构造函数
* @param string $mobile 手机号(登录者)
* @param string $password 飞信密码
*/
public function __construct($mobile, $password)
{
if ($mobile === '' || $password === '')
{
return;
}
$this->_mobile = $mobile;
$this->_password = $password;
$this->_login();
}
/**
* 析构函数
*/
public function __destruct() {
$this->_logout();
}
/**
* 登录
* @return string
*/
protected function _login()
{
$uri = '/huc/user/space/login.do?m=submit&fr=space';
$data = 'mobilenum='.$this->_mobile.'&password='.urlencode($this->_password);
$result = $this->_postWithCookie($uri, $data);

//解析Cookie
preg_match_all('/.*?rnSet-Cookie: (.*?);.*?/si', $result, $matches);
if (isset($matches[1]))
{
$this->_cookie = implode('; ', $matches[1]);
}
$result = $this->_postWithCookie('/im/login/cklogin.action', '');

return $result;
}
/**
* 获取csrfToken,给好友发飞信时需要这个字段
* @param string $uid 飞信ID
* @return string
*/
protected function _getCsrfToken($uid)
{
if ($this->_csrfToten === null)
{
$uri = '/im/chat/toinputMsg.action?touserid='.$uid;
$result = $this->_postWithCookie($uri, '');
preg_match('/name="csrfToken".*?value="(.*?)"/', $result, $matches);

$this->_csrfToten = isset($matches[1]) ? $matches[1] : '';
}

return $this->_csrfToten;
}

/**
* 向指定的手机号发送飞信
* @param string $mobile 手机号(接收者)
* @param string $message 短信内容
* @return string
*/
public function send($mobile, $message) {
if($message === '') {
return '';
}
// 判断是给自己发还是给好友发
if($mobile === $this->_mobile) {
return $this->_toMyself($message);
}
else if(strlen($mobile)===11){
$uid = $this->_getUid($mobile);
}
else {
$uid=$mobile;
}
return $uid === '' ? $this->_addFriend($mobile) : $this->_toUid($uid, $message);
}
protected function _getname() {
$uri = '/im/index/index.action';
$result = $this->_postWithCookie($uri, '#');
// 匹配
preg_match('/(.*?)</a>/si', $result, $matches);
return $matches[2];
}
/*
* 通过手机号增加好友
* @param string $number 手机号(要加的好友手机)
* @param string $nickname 你的名字,出现在对方的验证短信里
* @param string $buddylist 分组,默认为空
* @param string $localName 好友屏显名
* @return string
*/
protected function _addFriend($number) {
$uri = '/im/user/insertfriendsubmit.action';
$data = 'nickname='. urlencode($this->_getname()).'&buddylist=1&localName=&number='. $number .'&type=0';
$result = $this->_postWithCookie($uri, $data);
return $result;
}
/**
* 获取飞信ID
* @param string $mobile 手机号
* @return string
*/
protected function _getUid($mobile)
{
if (empty($this->_uids[$mobile]))
{
$uri = '/im/index/searchOtherInfoList.action';
$data = 'searchText='.$mobile;
$result = $this->_postWithCookie($uri, $data);
//匹配
preg_match('/toinputMsg.action?touserid=(d+)/si', $result, $matches);

$this->_uids[$mobile] = isset($matches[1]) ? $matches[1] : '';
}
return $this->_uids[$mobile];
}
/**
* 向好友发送飞信
* @param string $uid 飞信ID
* @param string $message 短信内容
* @return string
*/
protected function _toUid($uid, $message) {
$uri = '/im/chat/sendMsg.action?touserid='.$uid;
$csrfToken = $this->_getCsrfToken($uid);
$data = 'msg='.urlencode($message).'&csrfToken='.$csrfToken;
$result = $this->_postWithCookie($uri, $data);
return $result;
}
/**
* 给自己发飞信
* @param string $message
* @return string
*/
protected function _toMyself($message) {
$uri = '/im/user/sendMsgToMyselfs.action';
$result = $this->_postWithCookie($uri, 'msg='.urlencode($message));
return $result;
}
/**
* 退出飞信
* @return string
*/
protected function _logout() {
$uri = '/im/index/logoutsubmit.action';
$result = $this->_postWithCookie($uri, '');
return $result;
}
protected function getgroup() {
$uri = '/im/index/index.action';
$data = 'type=group';
$result = $this->_postWithCookie($uri, $data);
// 匹配
preg_match_all('/contactlistView.action?idContactList=(d+)/si', $result, $matches);
foreach($matches[1] as $k=>$v){
if( $k== 0 ){
$min = $v;
$max = $v;
}else if($v!=9998&&$v!=9999){
$min = min($min,$v);
$max = max($max,$v);
}
}
return $max;
}
public function getyou1() {
$list=$this->getgroup();
for($i=0;$i<=$list;$i++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&type=group';
$result = $this->_postWithCookie($uri, $data);
preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn);
if(!$listn[2]){continue;}
$shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n";
preg_match('/共(d+)页/si', $result, $zpage);
preg_match('/共(d+)</a>页/si', $result, $dpage);
isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4];
for($j=1;$j<=$page;$j++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&page='.$j;
$result = $this->_postWithCookie($uri, $data);
preg_match_all('/(.*?)</a>/si', $result, $matches);
if(!$matches[1][0]){break;}
for($x=0;$x<=9;$x++){
if(!$matches[1][$x]){continue;}
$shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n";
}
}
}
return $shuchu;
}
public function getyou() {
$list=$this->getgroup();
for($i=0;$i<=$list;$i++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&type=group';
$result = $this->_postWithCookie($uri, $data);
preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn);
if(!$listn[2]){continue;}
$shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n";
preg_match('/共(d+)页/si', $result, $zpage);
preg_match('/共(d+)</a>页/si', $result, $dpage);
isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4];
for($j=1;$j<=$page;$j++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&page='.$j;
$result = $this->_postWithCookie($uri, $data);
preg_match_all('/(.*?)</a>/si', $result, $matches);
if(!$matches[1][0]){break;}
for($x=0;$x<=9;$x++){
if(!$matches[1][$x]){continue;}
$shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n";
}
}
}
return $shuchu;
}
/**
* 携带Cookie向f.10086.cn发送POST请求
* @param string $uri
* @param string $data
*/
protected function _postWithCookie($uri, $data)
{
$fp = fsockopen('f.10086.cn', 80);
fputs($fp, "POST $uri HTTP/1.1rn");
fputs($fp, "Host: f.10086.cnrn");
fputs($fp, "Cookie: {$this->_cookie}rn");
fputs($fp, "Content-Type: application/x-www-form-urlencodedrn");
fputs($fp, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1rn");
fputs($fp, "Content-Length: ".strlen($data)."rn");
fputs($fp, "Connection: closernrn");
fputs($fp, $data);

$result = '';
while (!feof($fp))
{
$result .= fgets($fp);
}

fclose($fp);

return $result;
}
}

 

仔细的研究了一下,原来用php写的解压程序效率比想象的还是高很多的,既然这么好,干脆再优化一下后用到自己后台中,虽然现在大部分空间的控制面板中有压缩和解压这个功能,但是毕竟有时候有些麻烦。

做这个之前,没有接触过php压缩这一块,网上搜了一些,大多数都是php压缩类、压缩函数,少则几百行,多的就几千行代码。这对于我这种新手来说很摸不到头脑,再说我也不用这么复杂的功能。最后参考函数手册,理清楚了几个相关的函数后,就明白了怎么去整了。

PHP Zip File 函数
PHP Zip File 函数


记得要开启 zip ,把 php.ini 中的 extension=php_zip.dll 前面的分号去掉。


源码范例:

 代码如下 复制代码


<?php


//需开启配置 php_zip.dll

//phpinfo();

header("Content-type:text/html;charset=utf-8");


function get_zip_originalsize($filename, $path) {

//先判断待解压的文件是否存在

if(!file_exists($filename)){

die("文件 $filename 不存在!");

$starttime = explode(' ',microtime()); //解压开始的时间

//将文件名和路径转成windows系统默认的gb2312编码,否则将会读取不到

$filename = iconv("utf-8","gb2312",$filename);

$path = iconv("utf-8","gb2312",$path);

//打开压缩包

$resource = zip_open($filename);

$i = 1;

//遍历读取压缩包里面的一个个文件

while ($dir_resource = zip_read($resource)) {

//如果能打开则继续

if (zip_entry_open($resource,$dir_resource)) {

//获取当前项目的名称,即压缩包里面当前对应的文件名

$file_name = $path.zip_entry_name($dir_resource);

//以最后一个“/”分割,再用字符串截取出路径部分

$file_path = substr($file_name,0,strrpos($file_name, "/"));

//如果路径不存在,则创建一个目录,true表示可以创建多级目录

if(!is_dir($file_path)){

mkdir($file_path,0777,true);

}

//如果不是目录,则写入文件

if(!is_dir($file_name)){

//读取这个文件

$file_size = zip_entry_filesize($dir_resource);

//最大读取6M,如果文件过大,跳过解压,继续下一个

if($file_size<(1024*1024*6)){

$file_content = zip_entry_read($dir_resource,$file_size);

file_put_contents($file_name,$file_content);

}else{ www.111cn.net

echo "<p> ".$i++." 此文件已被跳过,原因:文件过大, -> ".iconv("gb2312","utf-8",$file_name)." </p>";

}

}

//关闭当前

zip_entry_close($dir_resource);

}

}

//关闭压缩包

zip_close($resource); 

$endtime = explode(' ',microtime()); //解压结束的时间

$thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);

$thistime = round($thistime,3); //保留3为小数

echo "<p>解压完毕!,本次解压花费:$thistime 秒。</p>";

}


$size = get_zip_originalsize('20131101.zip','1代潇瑞/');


?>


测试解压了一个300多KB的小文件,花了0.115秒,测试解压了一个30多MB的(网页文件,小文件比较多),花了20多秒。


php解压程序


跟系统比起来确实慢了一些,但是这也很不错了!刚刚入门,代码还不优良,但是实现了解压,而且代码也比网上的简介易懂,如果你看到这篇文章,相信对你是有帮助的!程序中用到了程序运行的时间,具体可以看看: 。

php解压程序


最后,我觉得:

 代码如下 复制代码

    //最大读取6M,如果文件过大,跳过解压,继续下一个

    if($file_size<(1024*1024*6)){

        $file_content = zip_entry_read($dir_resource,$file_size);

        file_put_contents($file_name,$file_content);

    }

这一块做的不太好,这样对大文件解压就没办法了,等会再优化一下。

需要安装imagick及其php扩展。一共有二个函数,都是从网上摘下来的,还没测试,这个方案应该可行,可以简单的仿照在线文档功能,有机会在项目中实践一下

第一种

 代码如下 复制代码
/**
* PDF2PNG  
* @param $pdf  待处理的PDF文件
* @param $path 待保存的图片路径
* @param $page 待导出的页面 -1为全部 0为第一页 1为第二页
* @return      保存好的图片路径和文件名
*/
 function pdf2png($pdf,$path,$page=0)

   if(!is_dir($path))
   {
       mkdir($path,true);
   }
   if(!extension_loaded('imagick'))
   { 
     echo '没有找到imagick!' ;
     return false;
   } 
   if(!file_exists($pdf))
   { 
      echo '没有找到pdf' ;
       return false; 
   }   www.111cn.net
   $im = new Imagick(); 
   $im->setResolution(120,120);   //设置图像分辨率
   $im->setCompressionQuality(80); //压缩比
   $im->readImage($pdf."[".$page."]"); //设置读取pdf的第一页
   //$im->thumbnailImage(200, 100, true); // 改变图像的大小
   $im->scaleImage(200,100,true); //缩放大小图像
   $filename = $path."/". time().'.png';
   if($im->writeImage($filename) == true)
   { 
      $Return  = $filename; 
   } 
   return $Return; 

$s = pdf2png('file/1371273225-ceshi_ppt.pdf','images');
echo '<div align="center"><img src="'.$s.'"></div>';

第二种

 代码如下 复制代码
function pdf2png($PDF,$Path){
   if(!extension_loaded('imagick')){
       return false;
   }
   if(!file_exists($PDF)){
       return false;
   }
   $IM = new imagick();
   $IM->setResolution(120,120);
   $IM->setCompressionQuality(100);
   $IM->readImage($PDF);
   foreach ($IM as $Key => $Var){
       $Var->setImageFormat('png');
       $Filename = $Path.'/'.md5($Key.time()).'.png';
       if($Var->writeImage($Filename) == true){
           $Return[] = $Filename;
       } www.111cn.net
   }
   return $Return;
}

创建一个jpg缩略图并显示出来
 

 代码如下 复制代码
<?php
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
// If 0 is provided as a width or height parameter,// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>

缩略GIF动画图片
 

 代码如下 复制代码
<?php
/* Create a new imagick object and read in GIF */
$im = new Imagick("example.gif");
/* Resize all frames */
foreach ($im as $frame) {
/* 50x50 frames */
$frame->thumbnailImage(50, 50);
/* Set the virtual canvas to correct size */
$frame->setImagePage(50, 50, 0, 0);
}/* Notice writeImages instead of writeImage */
$im->writeImages("example_small.gif", true);
?>
json_encode处理中文会有几种情况一种是null空,一种是u80e5这种符号,还有一种就是正常的中文了,下面一聚教程小编就来给你把这些问题一种整理一篇。

json_encode这个函数作用是起到不同语言之间数据传递的作用,尤其是php对js之间数据交换十分方便。

但是json_encode这个函数在使用的时候需要注意几点:

1 所传输的数据必须是UTF8格式的

2 需要对中文做一些URLENCODE处理


json_encode中文为null解决


原因分析:使用json_encode函数应应使用utf-8编码,我的页面用的是gbk.
解决:在json_encode函数前使用iconv('gbk','utf8')函数。

 代码如下 复制代码

function gbk2utf8($data){
if(is_array($data)){
return array_map('gbk2utf8', $data);
}
return iconv('gbk','utf-8',$data);
}

这样显示出来的会是如{"item1":1,"item2":"u4e2du6587"} 格式了,虽然空解决了,但是没显示汉字呀。

我们再来分析一下

例如:'胥'经过json_encode处理后变为'u80e5',最终的json中中文部分被替换为unicode编码。我们要解决的就是将对象转换为json并保证对象内部的中文在json中仍然是以正常的中文出现,现在看来只使用json_encode是不能达到目的的。www.111cn.net
  我的解决方法:先将类中的中文字段进行url编码(urlencode),然后再对对象进行json编码(jsonencode),最后url解码(urldecode)json,即最终的json,里面的中文依旧是那个中文!

 代码如下 复制代码

<?php
    $testJSON=array('name'=>'中文字符串','value'=>'test');
    //echo json_encode($testJSON);
    foreach ( $testJSON as $key => $value ) {
  $testJSON[$key] = urlencode ( $value );
    }
    echo urldecode ( json_encode ( $testJSON ) );
?>

查看输出结果为:

{“name”:”中文字符串”,”value”:”test”}

到此,成功地输出了中文字符。随意使用json_encode吧。这样子在PHP后台输出的JSON字符串在前台javascript中Ajax接收后eval出来也不会出现中文乱码,因为js在处理JSON格式数据是也是以UTF8的形式进行的,与PHP类似,故接收PHP页面的JSON字符串不会出现问题。


下面是对json_encode这个函数做一些完善

 代码如下 复制代码

<?php
/**************************************************************
*
* 使用特定function对数组中所有元素做处理
* @param string &$array 要处理的字符串
* @param string $function 要执行的函数
* @return boolean $apply_to_keys_also 是否也应用到key上
* @access public
*
*************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
}
 
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
 
/**************************************************************
* www.111Cn.net
* 将数组转换为JSON字符串(兼容中文)
* @param array $array 要转换的数组
* @return string 转换得到的json字符串
* @access public
*
*************************************************************/
function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
 
$array = array
(
'Name'=>'希亚',
'Age'=>20
);
 
echo JSON($array);
?>

[!--infotagslink--]

相关文章

  • PHP7快速编译安装的步骤

    编译安装非常的简单了我们现在的php版本已经到了php7了,下文小编来为各位介绍一篇关于PHP7快速编译安装的步骤,希望文章能够帮助到各位。 一、安装必要一些依赖 yum...2016-11-25
  • php语言实现redis的客户端

    php语言实现redis的客户端与服务端有一些区别了因为前面介绍过服务端了这里我们来介绍客户端吧,希望文章对各位有帮助。 为了更好的了解redis协议,我们用php来实现...2016-11-25
  • jQuery+jRange实现滑动选取数值范围特效

    有时我们在页面上需要选择数值范围,如购物时选取价格区间,购买主机时自主选取CPU,内存大小配置等,使用直观的滑块条直接选取想要的数值大小即可,无需手动输入数值,操作简单又方便。HTML首先载入jQuery库文件以及jRange相关...2015-03-15
  • Rstudio中安装package出现的问题及解决

    这篇文章主要介绍了Rstudio中安装package出现的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-06
  • 如何获取网站icon有哪些可行的方法

    获取网站icon,常用最简单的方法就是通过website/favicon.ico来获取,不过由于很多网站都是在页面里面设置favicon,所以此方法很多情况都不可用。 更好的办法是通过google提供的服务来实现:http://www.google.com/s2/favi...2014-06-07
  • JS实现的简洁纵向滑动菜单(滑动门)效果

    本文实例讲述了JS实现的简洁纵向滑动菜单(滑动门)效果。分享给大家供大家参考,具体如下:这是一款纵向布局的CSS+JavaScript滑动门代码,相当简洁的手法来实现,如果对颜色不满意,你可以试着自己修改CSS代码,这个滑动门将每一...2015-10-21
  • PHP编译安装后PHP-FPM使用笔记

    PHP-FPM我们相信各位用高版本的php经常使用到了,下面整理了一些关于PHP-FPM的笔记,有兴趣的可进来看看。 今天赶上了123System OPenVZ VPS全场半价的机会,购入了一...2016-11-25
  • jQuery+slidereveal实现的面板滑动侧边展出效果

    我们借助一款jQuery插件:slidereveal.js,可以使用它控制面板左右侧滑出与隐藏等效果,项目地址:https://github.com/nnattawat/slideReveal。如何使用首先在页面中加载jquery库文件和slidereveal.js插件。复制代码 代码如...2015-03-15
  • 安装和使用percona-toolkit来辅助操作MySQL的基本教程

    一、percona-toolkit简介 percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括: 检查master和slave数据的一致性 有效地对记录进行归档 查找重复的索...2015-11-24
  • Linux安装Pytorch1.8GPU(CUDA11.1)的实现

    这篇文章主要介绍了Linux安装Pytorch1.8GPU(CUDA11.1)的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-25
  • PHP+jQuery翻板抽奖功能实现

    翻板抽奖的实现流程:前端页面提供6个方块,用数字1-6依次表示6个不同的方块,当抽奖者点击6个方块中的某一块时,方块翻转到背面,显示抽奖中奖信息。看似简单的一个操作过程,却包含着WEB技术的很多知识面,所以本文的读者应该熟...2015-10-21
  • vscode安装git及项目开发过程

    这篇文章主要介绍了vscode安装git及项目开发过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-05-19
  • mac下Apache + MySql + PHP搭建网站开发环境

    首先为什不自己分别搭建Apache,PHP和MySql的环境呢?这样自己可以了解更多知识,说起来也更酷。可也许因为我懒吧,我是那种“既然有现成的,用就是了”的人。君子生非异也,善假于物也。两千年前的荀子就教导我们,要善于利用工具...2014-06-07
  • SQLMAP结合Meterpreter实现注入渗透返回shell

    sqlmap 是一个自动SQL 射入工具。它是可胜任执行一个广泛的数据库管理系统后端指印, 检索遥远的DBMS 数据库等,下面我们来看一个学习例子。 自己搭建一个PHP+MYSQ...2016-11-25
  • Visual Studio 2015下载和安装图文教程

    这篇文章主要为大家详细介绍了Visual Studio 2015下载和安装图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • Node调试工具JSHint的安装及配置教程

    现在我们介绍一种在Node下检查简单错误的JS代码验证工具JSHint。  JSHint的具体介绍参考http://www.jshint.com/about/,说直白点儿,JSHint就是一个检查JS代码规范与否的工具,它可以用来检查任何(包括server端和client端...2014-05-31
  • Centos中彻底删除Mysql(rpm、yum安装的情况)

    我用的centos6,mysql让我整出了各种问题,我想重装一个全新的mysql,yum remove mysql-server mysql之后再install并不能得到一个干净的mysql,原来的/etc/my.cnf依然没变,datadir里面的数据已没有任何变化,手动删除/etc/my.cn...2015-03-15
  • 在PyCharm中安装PaddlePaddle的方法

    这篇文章主要介绍了在PyCharm中安装PaddlePaddle的方法,本文给大家介绍的非常想详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-05
  • PHP实现今天是星期几的几种写法

    复制代码 代码如下: // 第一种写法 $da = date("w"); if( $da == "1" ){ echo "今天是星期一"; }else if( $da == "2" ){ echo "今天是星期二"; }else if( $da == "3" ){ echo "今天是星期三"; }else if( $da == "4"...2013-10-04
  • linux服务器快速卸载安装node环境(简单上手)

    这篇文章主要介绍了linux服务器快速卸载安装node环境(简单上手),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-22