分类 编程相关 下的文章

今天突然发现“二奶”上装的Windows7被错误格式化了,于是想办法重装。折腾了一个晚上,直到11点半,还没搞好。

其实只要用个WinPE启动盘,备份一下C盘的MBR,再用个Ghost装上新系统就完了。但是手上没有WinPE的可启动U盘,也没有能够在Linux上能够使用的制作工具。当然,用过WINE,也用过KVM上运行的WinXP,都不能很好地访问U盘。也折腾过用YUMI制作的启动盘,但不懂其menu的设置,手动添加的iso文件不能启动。最后放弃,只好明天回公司再弄。

洗澡时,不禁在想,其实装个Win7还有没有意义?Lubuntu已经很好地满足我自己的需求了,Chromium、MPlayer、Eclipse、Nginx、PHP等,都能满足日常开发和娱乐的需求。网上支付的话,建行的小额支付,可以用手机验证。至于游戏,WINE能够运行一些经典的游戏,还有手机也能玩一些好玩的。至于那些“大作”,早就没心思去“折腾”了。何不借此机会,把Windows铲除干净?

还是先睡吧~过了明天,就是周末了~

PS.发现近来特健忘,看来有必要开发个任务备忘的手机软件!

首先PHP没有自带定时器(例如每隔几秒自动执行一下某函数)。要实现这个功能,只有三个方法:1)利用外部脚本实现定时执行;2)利用客户端的JavaScript定时执行;3)根据访问请求来实现,但前提是网站的访问量足够大。利用外部脚本实现定时执行,只能是使用虚拟主机或者自己架设服务器才能使用(在虚拟空间上运行,就只能考虑使用JavaScript)。

网上搜到的资料,用PHP页面也可以实现定时器的。主要思路是使用死循环,再加延时执行(即sleep或者usleep函数)。相关示例代码如下:

//------------begin file: timer.php------------
<?php
//每半秒执行记录一下当前时间

//写文件函数
function write_txt() {
    $filePath = 'timer_test.txt';
    if(!file_exists($filePath)){
        $fp = fopen($filePath, 'ab');
        fclose($fp);
    }
    $str = "\r\n" . date('Y-m-d H:i:s');
    $fp = fopen($filePath, 'ab'); //a为打开文件后指向文件末尾,b为以二进制打开文件
    fwrite($fp, $str);
    fclose($fp);
}

ignore_user_abort(); //即使关掉浏览器,PHP也可继续执行
set_time_limit(0); //不设置脚本超时时间,可以无限执行下去
$interval = 30; //时间间隔为30秒
do{
    //定时执行的代码
    write_txt();
    sleep($interval); //等待,sleep的参数单位是秒,usleep的参数单位是毫秒
}while(true); //死循环
//------------end file: timer.php------------

执行时还有个问题,用浏览器打开运行后,网站的其它连接都卡住且不能打开。网上搜到的解析是,PHP不支持多线程。最后只有在服务器上,用以下命令运行:

php timer.php &

终于,在上个星期,匆匆发布了用PHP编写的网站。

其实,与其说是“编写”,还不如说是“搭建”────基于Codeignter框架,采用SQLite,构建而成。基本实现了用户登录、用户管理、简单的笔记管理等。总算实现了N年前,做个PHP网站的计划。

然后考虑了一下Javascript框架采用哪个的问题。Google了一下,主流框架都比较大,例如prototype、jQuery、ExtJs等,根本不适合手机访问,特别是我这种2.5G的网络。于是再找一些轻量级的,例如$dom、zepto、snack等,但都不能完全满足需求。最后,考虑还是自己写一个。很简单,屏蔽各种浏览器的差异,简化一些语句和功能。具体一点,就是统一获取常用数值的函数、$()函数和ajax函数。希望可以尽快开工吧。

由于想把Raspberry Pi改造成无线中继,所以插了两个USB网卡上去。后来发现系统启动后,无线网卡的名称每次都不一定相同,以致启动桥接服务的shell脚本出错。但笔记本上的Lubuntu 12.10却是无线网卡每次的名称都不变。同样是基于Debian,Raspbian也应该可以实现无线网卡名称固定(其实就是跟MAC绑定网络接口名称)。翻查了资料,还是Debian的Wiki比较靠谱。见:http://wiki.debian.org/udev

最后还是比较懒,从Lubuntu 12.10上复制文件 /etc/udev/rules.d/70-persistent-net.rules 到Raspbian上,再改改来用就可以了。该文件修改如下:

## begin the file /etc/udev/rules.d/70-persistent-net.rules #################
# USB Ethernet (usb)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="aa:bb:cc:dd:ee:ff", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

# USB device 0x0cf3:0x1006 (Mercury MW54U)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="aa:bb:cc:dd:ee:ff", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"

# USB device 0x0bda:0x8171 (Netcore NW338)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="aa:bb:cc:dd:ee:ff", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan1"
## end the file /etc/udev/rules.d/70-persistent-net.rules #################

其中aa:bb:cc:dd:ee:ff要替换成网卡对应的MAC码。

入手Raspberry Pi Model B (512MB) 已经一段时间了。装上官方的Raspbian,感觉跟手头上的AO522没区别。同样是基于Debian,同样是LXDE桌面。也试了下Xbian (XMBC + Raspbian),感觉非常棒,只是菜单操作有点卡。可惜手上的SD卡空间不够大,否则,还可以玩玩Android。

玩过后,正是按计划进行。首先是作为无线路由来使用。其实就跟Lubuntu共享WiFi一样,测试过程中,还改良了一下以前写的sharewifi脚本(以前的版本:http://www.foxail.org/blog/?p=490)。但是还有一个问题,执行该脚本的stop操作后,系统不会把建立的mon.wlan0接口关闭,需要重启系统,wifi才能从共享模式变成正常的接收模式。

1)sharewifi脚本,设置共享wifi,我放在/opt下,内容如下:(首先要安装hostapd和dnsmasq,执行sudo apt-get install hostapd dnsmasq即可)

### begin the file /opt/sharewifi #########################################
#!/bin/sh

# Setup wireless AP, share the Internet from interface0 to interface1
# USAGE: sharewifi [ start | stop ] interface0 interface1
# EXAMPLE: sharewifi start wlan1 wlan0

help( )
{
    cat << HELP
    Setup wireless AP, share the Internet from interface0 to interface1
    USAGE: sharewifi [ help | start | stop ] interface0 interface1
    EXAMPLE: sharewifi start wlan1 wlan0

    List clients:
    cat /var/lib/misc/dnsmasq.leases
    HELP
    exit 0
}

start( )
{
    echo Starting share wifi ......
    echo Share Internet $port_in to $port_out

    # Configure iptable rules
    iptables -F
    iptables -t nat -A POSTROUTING -s $ip_prefix.0/24 -o $port_in -j MASQUERADE
    iptables -A FORWARD -s $ip_prefix.0/24 -o $port_in -j ACCEPT
    iptables -A FORWARD -d $ip_prefix.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -i $port_in -j ACCEPT

    # Log the message of route
    #iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 80 -j LOG --log-prefix "NEW_HTTP_CONN: "

    # Save iptable rules
    sh -c "iptables-save > /etc/iptables.rules"

    # Configure hostapd
    hostapd_conf=/etc/hostapd/hostapd.conf
    [ -f $hostapd_conf ] && rm $hostapd_conf
    echo >> $hostapd_conf interface=$port_out
    echo >> $hostapd_conf driver=nl80211
    echo >> $hostapd_conf ssid=foxrpi-ap
    echo >> $hostapd_conf channel=1
    echo >> $hostapd_conf hw_mode=g
    echo >> $hostapd_conf auth_algs=1
    echo >> $hostapd_conf wpa=3
    echo >> $hostapd_conf wpa_passphrase=1234567890
    echo >> $hostapd_conf wpa_key_mgmt=WPA-PSK
    echo >> $hostapd_conf wpa_pairwise=TKIP CCMP
    echo >> $hostapd_conf rsn_pairwise=CCMP
    chmod 755 $hostapd_conf

    # Configure /etc/dnsmasq.conf
    dnsmasq_conf=/etc/dnsmasq.conf
    [ -f $dnsmasq_conf ] && rm $dnsmasq_conf
    echo >> $dnsmasq_conf interface=$port_out
    echo >> $dnsmasq_conf bind-interfaces #这个是只监听wlan0,没有之会检测所有卡
    echo >> $dnsmasq_conf except-interface=lo
    echo >> $dnsmasq_conf dhcp-range=$ip_prefix.10,$ip_prefix.110,6h #设置dhcp地址范
    chmod 755 $dnsmasq_conf

    # Enable routing
    sysctl net.ipv4.ip_forward=1

    #killall named
    /etc/init.d/hostapd stop
    ifconfig $port_out $ip_prefix.1
    hostapd -B $hostapd_conf
    /etc/init.d/dnsmasq restart

    echo Sucess share wifi

    exit 0
}

stop( )
{
    echo Stopping share wifi ......
    echo Stop share Internet $port_in to $port_out

    # Configure iptable rules
    iptables -F

    # Log the message of route
    #iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 80 -j LOG --log-prefix "NEW_HTTP_CONN: "

    # Save iptable rules
    sh -c "iptables-save > /etc/iptables.rules"

    # Configure hostapd
    hostapd_conf=/etc/hostapd/hostapd.conf
    [ -f $hostapd_conf ] && rm $hostapd_conf

    # Configure /etc/dnsmasq.conf
    dnsmasq_conf=/etc/dnsmasq.conf
    [ -f $dnsmasq_conf ] && rm $dnsmasq_conf

    # Enable routing
    sysctl net.ipv4.ip_forward=0

    #killall named
    /etc/init.d/hostapd stop
    /etc/init.d/dnsmasq stop
    ifconfig $port_out down
    ifconfig $port_out del $ip_prefix.1
    ifconfig $port_out up

    echo Sucess stop share wifi

    exit 0
}

ip_prefix=192.168.23 #wifi路由所用网段

#port_in is the network interface which connected to Internet, and default wlan1.
port_in=wlan1

#port_out is the network interface which will be setup AP, and default wlan0.
port_out=wlan0

if [ -n "$2" ]; then
    port_in=$2

    if [ -n "$3" ]; then
        port_out=$3
    fi
fi

case "$1" in
"help" )
    help ;;
"start" )
    start ;;
"stop" )
    stop ;;
*)
    help ;;
esac
### end the file /opt/sharewifi #########################################

2)设置开机自动启动,先编写开机启动的脚本。在/etc/init.d下建立文件sharewifi,文件内容如下:

### begin the file /etc/init.d/sharewifi ######################################### 
#!/bin/sh 
### BEGIN INIT INFO # Provides: sharewifi 
# Required-Start: $syslog 
# Required-Stop: $syslog 
# Default-Start: 2 3 4 5 
# Default-Stop: 0 1 6 
# Short-Description: starts the Wireless AP server 
# Description: starts the Wireless AP using start-stop-daemon 
### END INIT INFO
sharewifi=/opt/sharewifi
NAME=sharewifi
DESC=sharewifi

# Include nginx defaults if available
if [ ! -f $sharewifi ]; then
    echo "Can't find the file $sharewifi"
    exit 1
fi

case "$1" in
start)
    echo -n "Starting $DESC: "
    sh $sharewifi start eth0 wlan0
    echo "$NAME."
    ;;

stop)
    echo -n "Stopping $DESC: "
    sh $sharewifi stop eth0 wlan0
    echo "$NAME."
    ;;

restart)
    echo -n "Restarting $DESC: "
    sh $sharewifi stop eth0 wlan0
    sleep 1
    sh $sharewifi start eth0 wlan0
    echo "$NAME."
    ;;

*)
    echo "Usage: $NAME {start|stop|restart}" >&2
    exit 1
    ;;
esac

exit 0
### end the file /etc/init.d/sharewifi #########################################

3)执行以下命令,即可设置开机启动:

sudo update-rc.d /etc/init.d/sharewifi defaults

为了重新安装Lubuntu 12.10,需要备份原来的12.04。因为很多资料还在上面,装好系统就可以直接复制过来了。如果直接复制到Windows分区,权限信息会丢失。于是找了以下方案:

方案一:GHOST备份。

GHOST默认不支持EXT4文件系统,需要先设置options->Image/Tape,选择Image All。

优点:
备份速度快,备份文件小。

缺点:
1)不能查看、提取备份文件。本来用Ghost Explorer可以查看或提取,设置修改备份文件的,但是ETX4的备份文件什么也没显示。
2)需要在DOS、Windows等非Linux系统上进行。

方案二:dd命令。参考资料:http://blog.csdn.net/shendl/article/details/7384755
1)U盘启动Linux LiveCD(现在Linux的发行版一般都支持LiveCD了),进入盘上的系统,打开命令行,执行如下命令,查看第一个硬盘(sda)的分区情况:

sudo fdisk -u -l /dev/sda

2)然后执行备份命令:

dd bs=512 count=[fdisk命令中最大的end数+1] if=/dev/sda1 of=/linux_bak.img

这样,就可以把硬盘上的第一个分区(sda1)数据全部copy到linux_bak.img文件中。
3)还原。进入Linux LiveCD,打开命令行,执行如下命令:

dd if=/linux_bak.img of=/dev/sda1

这样把备份文件还原到硬盘的第一个分区上了。
4)浏览或修改备份文件。执行以下命令:

sudo mkdir /mnt/bak chmod 777 /mnt/bak mount /linux_bak.img /mnt/bak

进入/mnt/bak文件夹,就可以浏览或者修改备份文件了。

优点:
1)一切都是Linux的操作。
2)可以对备份文件进行浏览、提取或修改文件。

缺点:
1)需要在Linux LiveCD上进行备份和还原。
2)只能是备份分区或整个硬盘,如果分区划分的空间比较大,生成的备份文件也一样大。
3)速度慢,特别是备份容量很大的分区。

方案三:tar命令。参开资料:http://www.yesure.net/archives/6984.html

1)备份,在Linux系统中执行以下命令:

sudo tar -cvpzf /media/disk/linux_bak.tgz --exclude=/proc --exclude=/lost+found --exclude=/mnt --exclude=/sys --exclude=/media /

2)还原:

sudo tar -xvpzf /media/disk/linux_bak.tgz -C /

优点:
1)可以在使用中的Linux上直接备份。
2)只备份存在的文件,且备份文件经过压缩,比原始文件小。
3)备份文件就是个Zip文件,可以直接打开浏览、修改。

缺点:
1)备份速度上不如GHOST。
2)备份文件比较大的话,打开速度很慢。

最后,我还是采用了第三个方案。资料上还提示,可以在新立得上装个sbackup(图形界面备份还原工具)。有空再试试吧。

今天很郁闷!做了个PHP页面,Chrome显示正常,IE显示却不正常。本来骂IE、骂微软是正常不过的了,但不是骂完就能解决问题的。

Google的答案是UTF-8的BOM问题。BOM(Byte Order Mark),即UTF-8签名(UTF-8 signature),是UTF编码方案里用于标识编码的标准标记。简单来说,PHP编译代码时,没有理会BOM,造成IE显示错误。也由于使用了CodeIgniter框架,即使改了View页面,也解决不了问题,最后修改了相关的文件,即系Route、Controller、Model等相关页面。简单来说,用UTF-8编码写PHP,就要统一使用无BOM的UTF-8。

感觉每次用新的东西都会这样那样的小问题。其实都是因为基于已有的经验来做,比如因为用Java而使用UTF-8。

Nginx中配置,去掉CodeIgniter URI中的index.php。参考了人家的文章和自己实践,得出配置如下(Nginx-1.2.0中通过):

location /php/ { # "/php" is the location of CodeIgniter
# Hide index.php in URI
rewrite ^(/php)/(?!index\.php|robots\.txt|images|js|style|fckeditor|upload)(.*)$ $1/index.php/$2 last;
}

把以上配置内容添加到Nginx的配置文件nginx.conf即可。

再看看这段配置,其实就是用URI跳转来实现,而且是用正则表达式匹配和替换。感觉很优美~

今天尝试使用CodeIgniter(一个高效的PHP框架)来做点东西,但是根据官方教程(http://codeigniter.org.cn/user_guide/)去做,也总是显示404页面。于是寻求Goolge帮助。原因很简单,就是Nginx不支持PATH_INFO,地址是一截一截的那种。最后找到的有效解决方法是http://linux008.blog.51cto.com/2837805/546489

主要的Nignx配置如下:

location ~ \.php { # no "$" at the end of "php"
# root f:/httpd/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(.*)$; #support PATH_INFO
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}

今天偶然查了下二级域名的设置。想想,既然买了个顶级域名,那二级域名应该可以随便设。立马试了下,果然可以!用了一年多的Blog(foxail.org/blog),可以通过 blog.foxail.org 来访问了。噢~突然一下子,世界又变美好了!哈哈哈~