php 利用 fsockopen 模拟 post 提交
作者:蘑菇 日期:2010-04-21 00:31
- <?php
- $infoary = array("username"=>"username","password"=>"password");
- $encodestr = encode($infoary);
- $fp = fsockopen("localhost",80,$errno,$errmsg);
- $params.= "POST /admin/index.php?action=login HTTP/1.1\r\n";
- $params.= "Host: localhost\r\n";
- $params.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5\r\n";
- $params.= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
- $params.= "Accept-Language: zh-cn,zh;q=0.5\r\n";
- $params.= "Accept-Encoding: gzip,deflate\r\n";
- $params.= "Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n";
- $params.= "Keep-Alive: 300\r\n";
- $params.= "Connection: keep-alive\r\n";
- $params.= "Referer: http://localhost/admin/index.php\r\n";
- $params.= "Content-Type: application/x-www-form-urlencoded\r\n";
- $params.= "Content-Length: ".strlen($encodestr)."\r\n\r\n";
- $params.= $encodestr;
- fwrite($fp, $params);
- while(!feof($fp)){
- echo fgets($fp, 4096);
- }
- fclose($fp);
- function encode($params)
- {
- $i = 0;
- foreach($params as $k => $v){
- $i++;
- $str .= rawurlencode($k)."=".rawurlencode($v);
- if($i < count($params)) $str.= "&";
- }
- return $str;
- }
python 定时备份MySQL数据库
作者:蘑菇 日期:2010-04-19 16:50
于是写了个定时备份数据库的脚本,并发送邮件保存备份文件。
我们用到一个数据库配置文件: db.conf。 格式如下:
dbuser dbpass dbname
每行一条记录写下来,是我们要备份的数据库的列表。
下面我们要写到的是备份脚本:
#Author: Liubing
#Email : liubing@eduicc.com
#date : Mon, Apr 19th
import os
import time
import smtplib
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
#directory name
today = time.strftime("%Y-%m-%d")
#backup file name
tgzfile = today+".tar.gz"
#mkdir
if os.path.isdir(today):
os.system("rm -rf "+today)
os.mkdir(today)
#backup databases
fp = file("db.conf");
while True:
line = fp.readline();
if len(line) == 0:
break;
db_conf = line.rstrip('\n').split(' ');
bak_shell = "mysqldump -u"+db_conf[0]+" -p"+db_conf[1]+" "+db_conf[2]+" > "+today+"/"+db_conf[2]+"_"+today+".sql"
print bak_shell
os.system(bak_shell)
fp.close();
#create .tar.gz file
os.system("tar -zcvf "+tgzfile+" "+today)
os.system("rm -rf "+today)
#Send Email
From = "liubing@eduicc.com"
To = "liubing@eduicc.com"
server = smtplib.SMTP("localhost");
main_msg = email.MIMEMultipart.MIMEMultipart()
text_msg = email.MIMEText.MIMEText("Backup databases")
main_msg.attach(text_msg)
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
#read tgzfile
data = open(tgzfile, 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read())
data.close()
email.Encoders.encode_base64(file_msg)
#set email header
basename = os.path.basename(tgzfile)
file_msg.add_header('Content-Disposition', 'attachment', filename=basename)
main_msg.attach(file_msg)
main_msg['From'] = From
main_msg['To'] = To
main_msg['Subject'] = 'daily backup'
main_msg['Date'] = email.Utils.formatdate()
fullText = main_msg.as_string()
try:
server.sendmail(From, To, fullText)
finally:
server.quit()
os.system("rm -f "+tgzfile)
然后,我们改变db_backup.py的属性:
chmod 0755 db_backup.py
最后,我们把脚本加到crontab中
#backup at 3:00 every day
0 3 * * * /usr/bin/python /root/db_backup.py > /dev/null
Joomla 调用module的方法
作者:蘑菇 日期:2010-04-18 22:46
Joomla调用module一般有以下几个方法:
1. 写在模板首页
这种方法会受到Itemid的限制
2. 写在文章里
这种方法不会受到Itemid的限制,是内嵌在文章里的。就是说只要显示文章,就会显示引入的module
3. 用JModuleHelper类引入
$modules = JModuleHelper::getModules("position"); //这里会取到放在同一个位置的所有module,得到的是一个数组
echo JModuleHelper::renderModule($modules[index]); //这里的index是放在同一位置的module的索引
CentOS + Nginx + PHP + MySQL 环境搭建
作者:蘑菇 日期:2010-04-18 22:09
一、软件环境
CentOS 5.4 :
Nginx 0.8 : http://nginx.org/download/nginx-0.8.35.tar.gz
PHP 5.2.10 : http://www.eduicc.com/soft/php-5.2.10.tar.gz
MySQL 5.4.15 : http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.45.tar.gz/from/http://mysql.ntu.edu.tw/
二、安装
1. 准备工作
这一步我称它为“准备工作”,其实是安装一些系统编译及其他环境
yum -y install patch make gcc gcc-c++ gcc-g77 flex bison libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel
#安装编码格式转换库
wget http://www.eduicc.com/soft/libiconv-1.13.tar.gz
tar -zxvf libiconv-1.13.tar.gz
cd libiconv-1.13
./configure --prefix=/usr/local
make && make install
cd ..
#安装libmcrypt库
wget http://www.eduicc.com/soft/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
ldconfig
cd libltdl
./configure --enable-libltdl-install
make && make install
cd ../../
#安装php-mhash扩展库
wget http://www.eduicc.com/soft/mhash-0.9.9.9.tar.gz
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install
cd ..
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
wget http://www.eduicc.com/soft/mcrypt-2.6.8.tar.gz
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
make && make install
cd ..
2. 安装MySQL
cd mysql-5.1.54
./configure --prefix=/usr/local/mysql --enable-assembler --with-charset=utf8 --enable-thread-safe-client --with-extra-charsets=all --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile
make && make install
cd ..
#添加mysql用户和用户组
groupadd mysql
useradd -g mysql mysql
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.
#设置mysql开机自启动
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --level 345 mysql on
#增加mysql动态链接库文件
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
echo "/usr/local/lib" >>/etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
#添加root密码
/etc/init.d/mysql start
/usr/local/mysql/bin/mysqladmin -u root password root
/etc/init.d/mysql restart
/etc/init.d/mysql stop
#去掉mysql集群服务
chkconfig mysql-ndb off
chkconfig mysql-ndb-mgm off
3. 安装PHP
#我们用php-fpm来管理fastcgi
wget http://www.eduicc.com/soft/php-5.2.10-fpm-0.5.11.diff.gz
tar -zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
! 这里我们要先将autoconf降低版本,否则可能会出现: buildconf: Your version of autoconf likely contains buggy cache code.
yum -y install autoconf213
export PHP_AUTOCONF=/usr/bin/autoconf2.13
cd php-5.2.10
./buildconf --force
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-ftp --without-pear
make ZEND_EXTRA_LIBS='-libiconv'
make install
cp php.ini-dist /usr/local/php/etc/
4. 安装nginx
#安装pcre (Perl Compatible Regular Expressions正则表达式库)
wget http://www.eduicc.com/soft/pcre-7.9.tar.gz
tar -zxvf pcre-7.9.tar.gz
cd pcre-7.9
./configure
make && make install
cd ..
tar -zxvf nginx-0.8.35.tar.gz
cd nginx-0.8.35
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
#有关nginx.conf和php-fpm.conf的配置可以参照:nginx.conf 和 php-fpm.conf
5. 测试
我们先来写一个脚本
vi /etc/init.d/run
写入以下内容
ulimit -SHn 51200
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
启动测试:
chown 0755 /etc/init.d/run
/etc/init.d/run
/etc/init.d/mysql start
好了,在/var/www/localhost下写个phpinfo测试下吧!
感谢老婆的大力支持!


