作者归档:℃冻番茄

ubuntu8.10快速架设nginx2.6+php5.2.6+mysql5.0 [修改1]

nginx的高效率是出了名了,最近想用它把服务器上的apache换掉,所以先在本地机上做了下测试。

下面为记录下的步骤!

第一步,安装mysql
sudo apt-get install mysql-server

第二步,安装php-cgi及所需gd库等
sudo apt-get install php5-cli php5-cgi php5-mysql php5-gd php5-mcrypt

第三步,安装nginx
sudo apt-get install nginx

第四步,从lighttpd中取得spawn-fcgi

apt-get install lighttpd

cp /usr/bin/spawn-fcgi ./
apt-get remove lighttpd

cp spawn-fcgi /usr/bin/

sudo vi /usr/bin/php-fastcgi

    #!/bin/sh
    /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -g www-data -f /u    sr/bin/php-cgi
    #注意 -C 10 表示开启10个php-cgi进程!

sudo chmod 755 /usr/bin/php-fastcg

sudo vi /etc/init.d/init-fastcgi

C++代码
  1. #!/bin/bash   
  2.   
  3. PHP_SCRIPT=/usr/bin/php-fastcgi   
  4.   
  5. RETVAL=0   
  6.   
  7. case "$1" in   
  8.   
  9.     start)   
  10.   
  11.       $PHP_SCRIPT   
  12.   
  13.       RETVAL=$?   
  14.   
  15.   ;;   
  16.   
  17.     stop)   
  18.   
  19.       killall -9 php5-cgi   
  20.   
  21.       RETVAL=$?   
  22.   
  23.   ;;   
  24.   
  25.     restart)   
  26.   
  27.       killall -9 php5-cgi
  28.   
  29.       $PHP_SCRIPT   
  30.   
  31.       RETVAL=$?   
  32.   
  33.   ;;   
  34.   
  35.     *)   
  36.   
  37.       echo "Usage: php-fastcgi {start|stop|restart}"  
  38.   
  39.       exit 1   
  40.   
  41.   ;;   
  42.   
  43. esac   
  44.   
  45. exit $RETVAL   
  46.   
  47.   

chmod 755 /etc/init.d/php-fastcgi

开启spawn-fcgi
/etc/init.d/php-fastcgi start

写入开机启动!
update-rc.d php-fastcgi defaults

查看php-cgi进程
ps ax | grep php5-cgi

第五步,配置nginx
sudo vi /etc/nginx/sites-enabled/default

server {
    listen   80;
    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /home/xzy/www;
        index  index.html index.htm index.php;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /images {
        root   /usr/share;
        autoindex on;
    }

    #error_page  404  /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/nginx-default;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
        #proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #去除php支持的注释!
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #为防止php.ini里的cgi.fix_pathinfo 不为1的话,就直接修下php脚本运行的路径!
        fastcgi_param SCRIPT_FILENAME /home/xzy/www$fastcgi_script_name;

        include /etc/nginx/fastcgi_params;        
    }

    # deny access to .htaccess files, if Apache’s document root
    # concurs with nginx’s one
    #开启支持.htaccess
    location ~ /\.ht {
        deny  all;
    }
     #博客的图片较多,更改较少,将它们在浏览器本地缓存15天,可以提高下次打开我博客的页面加载速度。
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 15d;
    }
}

sudo gedit /etc/nginx/fastcgi_params

最后一行注释掉

#fastcgi_param  REDIRECT_STATUS    200;

重启nginx   sudo /etc/init.d/nginx restart 看是否运行正常,正常的话,环境就算搭配成功了!
在/home/xzy/www上新建一个a.php测试下phpinfo是否正常!
vi /home/xzy/www/a.php
<?php phpinfo();?>

最后优化下nginx
vi /etc/nginx/nginx.conf

user www-data;
#修改nginx的进程数,每个进程占内存10~15m 按服务器配置和应用需求来设
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log    /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    gzip  on;

    #开启并设置gzip配置   对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

}

小内存机器ubuntu下安装Nginx+php+Mysql+phpmyadmin

1,安装nginx,执行以下命令,很快完成,不过目前apg-get方式安装默认是0.5.33的版本

sudo apt-get install nginx

配置文件默认安装位置:

  [quote]conf: /etc/nginx/nginx.conf
  bin:/usr/sbin/nginx
  vhost: /etc/nginx/sites-enable/default
  cgi-params: /etc/nginx/fastcgi-params[/quote]

  建一个虚拟Server

  server {
  listen 80;
  server_name www.23day.com;
  access_log /var/log/nginx/home.ucenter.access.log;

  location / {
  root /var/www/23day.com;
  index index.php;

  location ~ \.php$ {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /var/www/23day.com$fastcgi_script_name;
  include /etc/nginx/fastcgi_params;
  }
  }

2,安装php-cgi模块

执行sudo apt-get install php5-cgi

配置文件默认安装位置:

  php-cgi: /usr/bin/php-cgi
  php5-cgi: /usr/bin/php5-cgi
  cgi config: /usr/bin/cgi/php.ini [/quote]

修改php.ini文件的cgi.fix_pathinfo数据为1,默认为0 cgi.fix_pathinfo=1; 这样php-cgi方能正常使用SCRIPT_FILENAME这个变量.

  3,安装spawn-fcgi spawn-fcgi是lighttpd的一个用来控制php-cgi的工具.

如果系统没有安装GCC编译环境,刚需要在安装lighttpd之前要安装build-essential工具包,执行以下命令

  sudo apt-get install build-essential
  wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
  tar -xvf lighttpd-1.4.19.tar.gz
  cd lighttpd-1.4.19/
  sudo apt-get install libpcre3-dev
  ./configure –without-zlib –without-bzip2
  make
  sudo cp src/spawn-fcgi /usr/local/bin/spawn-fcgi

这样cgi控制器就安装完成.

  4.启动测试系统.启动fast_cgi:

  spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi

  注意:ip,端口与nginx服务器中的cgi-pass要对应. -C表示打开几个cgi进程

  启动nginx

  sudo /etc/init.d/nginx start

  好了,如果没有出错信息,则说明配置成功了,现在写个phpinfo测试下吧!

  最后,附上我的/etc/nginx/sites-enable/default的配置文件,此配置文件启用了rewrite功能

  server {
  listen 80;
  server_name localhost;

  access_log /var/log/nginx/localhost.access.log;

  location / {
  root /var/www/nginx-default;
  index index.php;

  if (-f $request_filename/index.html){
  rewrite (.*) $1/index.html break;
  }
  if (-f $request_filename/index.php){
  rewrite (.*) $1/index.php;
  }
  if (!-f $request_filename){
  rewrite (.*) /index.php;
  }

  }

  #error_page 404 /404.html;

  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
  root /var/www/nginx-default;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #proxy_pass http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
  include /etc/nginx/fastcgi_params;
  }

  # deny access to .htaccess files, if Apache’s document root
  # concurs with nginx’s one
  #
  #location ~ /\.ht {
  #deny all;
  #}
  }

  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #listen 8000;
  #listen somename:8080;
  #server_name somename alias another.alias;

  #location / {
  #root html;
  #index index.html index.htm;
  #}
  #}

  # HTTPS server
  #
  #server {
  #listen 443;
  #server_name localhost;

  #ssl on;
  #ssl_certificate cert.pem;
  #ssl_certificate_key cert.key;

  #ssl_session_timeout 5m;

  #ssl_protocols SSLv2 SSLv3 TLSv1;
  #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  #ssl_prefer_server_ciphers on;

  #location / {
  #root html;
  #index index.html index.htm;
  #}
  #}

 

nginx.conf优化

 user  www www;

#Nginx每个进程耗费10M~12M内存,这里只开启一个Nginx进程,节省内存。
worker_processes 1;

error_log  /data1/logs/nginx_error.log  crit;

pid        /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

http
{
include       mime.types;
default_type  application/octet-stream;

#charset  gb2312;
   
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
   
sendfile on;
tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度。
gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone  crawler  $binary_remote_addr  10m;

server
{
  listen       80;
  server_name  blog.s135.com www.s135.com s135.com *.s135.com;
  index index.html index.htm index.php;
  root  /data0/htdocs/blog;

  #limit_conn   crawler  20;    

  #针对Bo-Blog系统的Rewrite静态化
  rewrite ^/post/([0-9]+).htm$ /read.php?$1 last;
  rewrite ^/post/([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2 last;
  rewrite ^/post/([0-9]+)_([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2&part=$3 last;
  rewrite ^/index_([0-9]+)_([0-9]+).htm$ /index.php?mode=$1&page=$2 last;
  rewrite ^/star_([0-9]+)_([0-9]+).htm$ /star.php?mode=$1&page=$2 last;
  rewrite ^/category_([0-9]+).htm$ /index.php?go=category_$1 last;
  rewrite ^/category_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=category_$1&mode=$2&page=$3 last;
  rewrite ^/archive_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2 last;
  rewrite ^/archive_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;
  rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3 last;
  rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;

  location ~ .*\.(php|php5)?$
  {
    #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    #fastcgi_pass  127.0.0.1:9000;

    fastcgi_index index.php;
    include fcgi.conf;
  }

  location ~ /read.php
  {
    #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    #fastcgi_pass  127.0.0.1:9000;

    fastcgi_index index.php;
    include fcgi.conf;
  }
 
  #博客的图片较多,更改较少,将它们在浏览器本地缓存15天,可以提高下次打开我博客的页面加载速度。
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
    expires      15d;
  }

  #博客会加载很多JavaScript、CSS,将它们在浏览器本地缓存1天,访问者在看完一篇文章或一页后,再看另一篇文件或另一页的内容,无需从服务器再次下载相同的JavaScript、CSS,提高了页面显示速度。
  location ~ .*\.(js|css)?$
  {
    expires      1d;
  }  

  log_format  access  ‘$remote_addr – $remote_user [$time_local] "$request" ‘
            ‘$status $body_bytes_sent "$http_referer" ‘
            ‘"$http_user_agent" $http_x_forwarded_for’;
  access_log  /data1/logs/access.log  access;
  }
}

 

PHP 5.2.6(FastCGI)的配置优化

[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="1"
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"

eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"

 

今天开始写一个通用的企业网站程序

最近要帮我哥的公司做一个企业网站,再加以我叔公司的网站到现在还是半成品就挂到网上了,心中老是过意不去,但是自己都去好好解决一下,最近也有空了,准备花上一周左右的时间,把这两个企业网站做掉。

做完这二个网站后,自己也准备做一个网站自己用了,时间过得真快,还有二个月就快过年了。。。感慨一下

下面是今天用了一下午修改了下自己以前的网站后台ui,后台界面就不重新做了凑合着用了。。

主是把数据库的视图查询部分做了下,然后完善了下框架的路由!

 

PHP代码
  1. <?php  
  2. class Model{  
  3.   
  4.     public $db;  
  5.   
  6.     public function __construct()  
  7.     {  
  8.         $this->db= new PDO(conf(‘dbType’).‘:host=’.conf(‘dbHost’).‘;dbname=’.conf(‘dbName’), conf(‘dbUser’), conf(‘dbPass’));  
  9.         $this->db->exec(‘SET NAMES ‘.conf(‘charset’));   
  10.     }  
  11.   
  12.     function query($sql)  
  13.     {  
  14.         $rs = $this->db->query($sql);  
  15.         return $rs;  
  16.     }  
  17.   
  18.   
  19.     function beginTransaction()  
  20.     {  
  21.         return $this->db->beginTransaction();  
  22.     }  
  23.   
  24.     function commit()  
  25.     {  
  26.         return $this->db->commit();  
  27.     }  
  28.   
  29.     function rollBack()  
  30.     {  
  31.         return $this->db->rollBack();  
  32.     }  
  33.   
  34.     function exec($sql)  
  35.     {  
  36.         return $this->db->exec($sql);  
  37.     }  
  38.   
  39.     function lastInsertId()  
  40.     {  
  41.         return $this->db->lastInsertId();  
  42.     }  
  43.   
  44.   
  45.     function setAttribute($attr$value)  
  46.     {  
  47.         return $this->db->setAttribute($attr$value);  
  48.     }  
  49.   
  50.   
  51.     function insert($table,$data)  
  52.     {  
  53.         $table=conf(‘dbprefix’).$table;  
  54.         $fields = "";  
  55.         $values = "";  
  56.         foreach($data as $field=>$value)  
  57.         {  
  58.             $fields .= "`$field`, ";  
  59.             $values .= "’$value’, ";  
  60.         }  
  61.           
  62.         $fields = substr_replace($fields"", -2, 1);  
  63.         $values = substr_replace($values"", -2, 1);  
  64.   
  65.         $sql = "insert into `$table` ($fields) values ($values)";  
  66.         //echo $sql;  
  67.         $rs = $this->db->query($sql) ;  
  68.         return $this->db->lastInsertId();  
  69.     }  
  70.   
  71.     function del($table,$keyarr){  
  72.         $table=conf(‘dbprefix’).$table;  
  73.         $where = "";  
  74.         foreach($keyarr as $key=>$key_value)  
  75.         {  
  76.             $where .= "`$key`=’$key_value’";  
  77.         }  
  78.         $sql = "delete from `$table` where $where";  
  79.         $rs = $this->db->query($sql);  
  80.         return $rs;  
  81.     }  
  82.   
  83.     function update($table,$data$keyarr)  
  84.     {  
  85.         $table=conf(‘dbprefix’).$table;  
  86.         $set = "";  
  87.         foreach($data as $field=>$value)  
  88.         {  
  89.             $set .= "`$field`=’$value’, ";  
  90.         }  
  91.         $set = substr_replace($set"", -2, 1);  
  92.   
  93.         $where = "";  
  94.         foreach($keyarr as $key=>$key_value)  
  95.         {  
  96.             $where .= "`$key`=’$key_value’";  
  97.         }  
  98.           
  99.   
  100.         $sql = "update `$table` set $set where $where";  
  101.         //echo $sql;  
  102.         $rs = $this->db->query($sql);  
  103.           
  104.         return $rs;  
  105.     }  
  106.   
  107.     function find($table,$where=,$field=‘*’,$order=,$group=)  
  108.     {  
  109.         $sql=$this->_map($table,$where,$field,$order,,$group);  
  110.         //echo $sql;  
  111.         return $this->_find($sql);  
  112.     }  
  113.       
  114.     function findAll($table,$where=,$field=‘*’,$order=,$limit=,$group=)  
  115.     {  
  116.         $sql=$this->_map($table,$where,$field,$order,$limit,$group);  
  117.         return $this->_findAll($sql);  
  118.     }  
  119.   
  120.     function count($table,$where=,$field=‘*’,$order=,$group=)  
  121.     {  
  122.         $sql=$this->_map($table,$where,$field,$order,,$group);  
  123.         return $this->_count($sql);  
  124.     }  
  125.       
  126.     function _count($sql)  
  127.     {  
  128.         $rs=$this->query($sql);  
  129.         $count=$rs->fetchColumn();  
  130.         $count=emptyempty($count)?0:$count;  
  131.         return $count;  
  132.     }  
  133.       
  134.     function _find($sql)  
  135.     {  
  136.         $rs=$this->query($sql);  
  137.         $row=$rs->fetch(PDO::FETCH_ASSOC);  
  138.         return $row;  
  139.     }  
  140.       
  141.     function _findAll($sql)  
  142.     {  
  143.         $rs=$this->query($sql);  
  144.         $row=$rs->fetchall(PDO::FETCH_ASSOC);  
  145.         return $row;  
  146.     }  
  147.      
  148.     function _map($table,$where=,$field=‘*’,$order=,$limit=,$group=)  
  149.     {  
  150.         $order=emptyempty($order)?:‘order by ‘.$order;  
  151.         $limit=emptyempty($group)?:‘limit ‘.$limit;  
  152.         $group=emptyempty($group)?:‘group by ‘.$group;  
  153.         $w=;  
  154.           
  155.         if(!emptyempty($where)){  
  156.             if(is_array($where)){  
  157.                 $w.=‘where’;  
  158.                 foreach($where as $k=>$v){  
  159.                     $w.=" $k=’$v’ and";  
  160.                 }  
  161.                 $w=substr($w,0,strlen($w)-3);  
  162.             }else{  
  163.                 $w=$where;  
  164.             }  
  165.         }  
  166.           
  167.         if(is_array($table)){  
  168.             $table1=conf(‘dbprefix’).$table[0];  
  169.             //echo $table1;  
  170.             $count=count($table);  
  171.             $joinleft=;  
  172.             array_shift($table);  
  173.             foreach($table as $v){  
  174.                 foreach($v as $k2=>$v2){  
  175.                     $k2=conf(‘dbprefix’).$k2;  
  176.                     foreach($v2 as $k3=>$v3){  
  177.                         $l="$table1.$k3=$k2.$v3";  
  178.                     }  
  179.                     $joinleft.="left join $k2 on $l ";  
  180.                 }  
  181.                   
  182.             }  
  183.             $sql="select $field from $table1 $joinleft $w $order $group $limit";  
  184.         }else{  
  185.             $sql="select $field from $table $w  $order $group $limit";  
  186.         }  
  187.         return $sql;  
  188.     }  
  189.   
  190. }  
  191. //fetch(‘PDO_FETCH_ASSOC’)  
  192. ?>  

 

网站后台界面 调整了部分css,对ff的兼容性更好些

11.jpg

网站的url形式

2222.jpg

简单网站统计功能的实现(PV IP 真实访客数)

主要统计三个数值,网站的pv 和ip 以及真实访客数 因为有详细的统计记录,所以可以实现的功能还有很大的扩展!把tallydate使用内存表的话,速度就会更快了

ss.jpg

需要用到二张表

 

SQL代码
  1. CREATE TABLE `tally` (   
  2.   `datedate NOT NULL,   
  3.   `pvtotal` int(10) NOT NULL,   
  4.   `iptotal` int(10) NOT NULL,   
  5.   `dltotal` int(10) NOT NULL,   
  6.   PRIMARY KEY  (`date`)   
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;   
  8.   
  9.   
  10.   
  11. CREATE TABLE `tallydata` (   
  12.   `tdid` int(10) NOT NULL auto_increment,   
  13.   `ip` int(10) NOT NULL,   
  14.   `cookie` varchar(32) NOT NULL,   
  15.   `datedate NOT NULL,   
  16.   `timeint(10) NOT NULL,   
  17.   `uri` varchar(255) default NULL,   
  18.   `referer` varchar(255) default NULL,   
  19.   PRIMARY KEY  (`tdid`)   
  20. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;   

 

统计的调用文件(用于所在统计的页面进行js调入):

(注:因项目使用的是ThinkPHP框架,所以贴出的只是代码片段!因为代码刚完成,所以没有做优化以及注释)

PHP代码
  1. <?php    
  2. class TallyAction extends Action{   
  3.     public function index(){   
  4.         if($_SERVER[‘SERVER_NAME’]!=‘fullyee.com’ && $_SERVER[‘SERVER_NAME’]!=‘www.fullyee.com’ && $_SERVER[‘SERVER_NAME’]!=‘company.fullyee.com’exit;   
  5.         $add[‘ip’]=ip2long(get_client_ip());   
  6.         if(!Cookie::is_set(‘fullyeetally’)){   
  7.             $value=md5(microtime().$add[‘ip’].rand());   
  8.                
  9.             $overTime=mktime(0,0,0,date(‘m’),date(‘d’)+1,date(‘Y’))-time();   
  10.             Cookie::set("fullyeetally",$value,time()+$overTime);   
  11.         }   
  12.         $fullyeetally=Cookie::get(‘fullyeetally’);   
  13.         $add[‘cookie’]=$fullyeetally;   
  14.         $add[‘date’]=date(‘Y-m-d’);   
  15.         $add[‘time’]=time();   
  16.         $add[‘uri’]=$_SERVER[‘SERVER_NAME’].$_SERVER[‘REQUEST_URI’];   
  17.         $add[‘referer’]=$_SERVER[‘HTTP_REFERER’];   
  18.         $tallydata_=D(‘Tallydata’);   
  19.         $tallydata_->create($add);   
  20.         $tallydata_->add();   
  21.            
  22.     }   
  23.   
  24.     public function saveData()   
  25.     {   
  26.         $tally_=D(‘Tally’);   
  27.         $tallydata_=D(‘Tallydata’);   
  28.         $nowDate=date(‘Y-m-d’,time()-3600*24);   
  29.   
  30.         $now[‘date’]=$nowDate;   
  31.         $now[‘iptotal’]=$this->gototal($nowDate,‘ip’);   
  32.         $now[‘pvtotal’]=$tallydata_->count(array(‘date’=>$nowDate),‘tdid’);   
  33.         $now[‘dltotal’]=$this->gototal($nowDate,‘cookie’);   
  34.   
  35.         if($tally=$tally_->find(array(‘date’=>$nowDate))){   
  36.             $tally_->save(array(‘iptotal’=>$now[‘iptotal’],‘pvtotal’=>$now[‘pvtotal’],‘dltotal’=>$now[‘dltotal’]),array(‘date’=>$nowDate));   
  37.         }else{   
  38.             $tally_->create($now);   
  39.             $tally_->add();   
  40.         }   
  41.         $timeDel=time()-3600*24*50;   
  42.         $tallydata_->query("delete from `tallydate` where `time`<$timeDel");   
  43.         echo ‘Success ‘+date(‘Y-m-d H:i:s’);   
  44.     }   
  45.   
  46.     function gototal($nowDate,$a)   
  47.     {   
  48.         $tallydata_=D(‘Tallydata’);   
  49.         $now[‘iptotal’]=$tallydata_->query("select count(distinct $a) from `tallydata` where `date`=’$nowDate’ ");   
  50.         return $now[‘iptotal’][0]["count(distinct $a)"];   
  51.     }   
  52. }    
  53. ?>  

程序中的 saveDate 部分,用于服务器的定时运行,最简单的方法就是用linux的crontab在一个访问人数比较少的时间wget一下saveDate代码就行了。。这段代码的作用是,把前一天的访问数据全部统计,以天为单位写入tally表!并且删除一定时间外的统计记录。默认是删除50天以前的全部统计记录

显示部分:

 

PHP代码
  1. <?php   
  2. class configAction extends Action{   
  3.     function _initialize(){   
  4.         header("Content-Type:text/html; charset=utf-8");   
  5.     }   
  6.        
  7.     public function tally()   
  8.     {   
  9.         $tally_=D(‘Tally’);   
  10.         $tallydata_=D(‘Tallydata’);   
  11.         $nowDate=date(‘Y-m-d’);   
  12.         $now[‘date’]=$nowDate;   
  13.         $now[‘iptotal’]=$this->gototal($nowDate,‘ip’);   
  14.         $now[‘pvtotal’]=$tallydata_->count(array(‘date’=>$nowDate),‘tdid’);   
  15.         $now[‘dltotal’]=$this->gototal($nowDate,‘cookie’);   
  16.            
  17.         if($tally=$tally_->find(array(‘date’=>$nowDate))){   
  18.             $tally_->save(array(‘iptotal’=>$now[‘iptotal’],‘pvtotal’=>$now[‘pvtotal’],‘dltotal’=>$now[‘dltotal’]),array(‘date’=>$nowDate));   
  19.         }else{   
  20.             $tally_->create($now);   
  21.             $tally_->add();   
  22.         }   
  23.         $today[‘pv’]=$now[‘pvtotal’];   
  24.         $today[‘ip’]=$now[‘iptotal’];   
  25.         $today[‘dl’]=$now[‘dltotal’];   
  26.         $yesterdayDate=date(‘Y-m-d’,time()-3600*24);   
  27.         //echo $yesterdayDate;   
  28.         $yesterday=$tally_->find(array(‘date’=>$yesterdayDate));   
  29.         //dump($yesterday);   
  30.         $yesterday[‘pv’]=isset($yesterday[‘pvtotal’])?$yesterday[‘pvtotal’]:‘0’;   
  31.         $yesterday[‘ip’]=isset($yesterday[‘iptotal’])?$yesterday[‘iptotal’]:‘0’;   
  32.         $yesterday[‘dl’]=isset($yesterday[‘dltotal’])?$yesterday[‘dltotal’]:‘0’;   
  33.   
  34.         $maxpv=$this->gomax(‘pvtotal’);   
  35.         $maxip=$this->gomax(‘iptotal’);   
  36.         $maxdl=$this->gomax(‘dltotal’);   
  37.         $max[‘pv’]=$maxpv[‘pvtotal’];   
  38.         $max[‘pvdate’]=$maxpv[‘date’];   
  39.         $max[‘ip’]=$maxip[‘iptotal’];   
  40.         $max[‘ipdate’]=$maxip[‘date’];   
  41.         $max[‘dl’]=$maxdl[‘dltotal’];   
  42.         $max[‘dldate’]=$maxdl[‘date’];     
  43.         $this->assign(‘today’,$today);   
  44.         $this->assign(‘yesterday’,$yesterday);   
  45.         $this->assign(‘max’,$max);   
  46.         $this->assign(‘nowtime’,date(‘Y年m月d日 H:i:s’));   
  47.         $this->display();   
  48.     }   
  49.     function gomax($a)   
  50.     {   
  51.         $tally_=D(‘Tally’);   
  52.         $max=$tally_->query("select * from `tally` order by `$a` desc limit 1");   
  53.         return $max[0];   
  54.     }   
  55.        
  56.     function gototal($nowDate,$a)   
  57.     {   
  58.         $tallydata_=D(‘Tallydata’);   
  59.         $now[‘iptotal’]=$tallydata_->query("select count(distinct $a) from `tallydata` where `date`=’$nowDate’ ");   
  60.         return $now[‘iptotal’][0]["count(distinct $a)"];   
  61.     }   
  62. }   
  63. ?>  

 

jquery使用Jcrop插件轻松实现上传图片后选取区域做头像

jquery使用Jcrop插件轻松实现上传图片后选取区域做头像

一般网站上传头像部分会比较麻烦,如果完全程序控制的话,程序把上传的图片自动裁切成指定大小的头像的话,很有可能会破坏头像的整体美观。所以现在大多数web2.0网站都有用js或flash来实现头像的上传与选取!

现在用jquery加上jcrop插件的话,就可以非常简单的实现这些功能!如果再加上编写的一些ajax处理部分的话就基本上可以满足一般web2.0的使用要求了

 aaaa1.jpg

aaaa2.jpg

PHP代码
  1. <?php  
  2. if($_GET[‘act’]==‘saveThumb’){  
  3.     $targ_w = $targ_h = 150;  
  4.     $jpeg_quality = 100;  
  5.   
  6.     $src = $_POST[‘bigImage’];  
  7.     $img_r = imagecreatefromjpeg($src);  
  8.     $dst_r = ImageCreateTrueColor( $targ_w$targ_h );  
  9.   
  10.     imagecopyresampled($dst_r,$img_r,0,0,$_POST[‘x’],$_POST[‘y’],$targ_w,$targ_h,$_POST[‘w’],$_POST[‘h’]);  
  11.   
  12.     header(‘Content-type: image/jpeg’);  
  13.     imagejpeg($dst_r,null,$jpeg_quality);  
  14.       
  15.     exit;  
  16. }  
  17. ?>  
  18. <html>  
  19.     <head>  
  20.   
  21.         <script src="jquery.pack.js"></script>  
  22.         <script src="jquery.Jcrop.pack.js"></script>  
  23.           
  24.         <link rel="stylesheet" href="jquery.Jcrop.css" type="text/css" />  
  25.           
  26.         <script language="Javascript">  
  27.   
  28.             $(function(){  
  29.   
  30.                   
  31.             });  
  32.               
  33.             function goss(){  
  34.   
  35.                 jQuery(‘#cropbox’).Jcrop({  
  36.                     onChange: showPreview,  
  37.                     onSelect: showPreview,  
  38.                     onSelect: updateCoords,  
  39.                     aspectRatio: 1  
  40.                 });  
  41.   
  42.             }  
  43.   
  44.             function updateCoords(c)  
  45.             {  
  46.                 $(‘#x’).val(c.x);  
  47.                 $(‘#y’).val(c.y);  
  48.                 $(‘#w’).val(c.w);  
  49.                 $(‘#h’).val(c.h);  
  50.             };  
  51.   
  52.             function checkCoords()  
  53.             {  
  54.                 if ($(‘#x’).val()==){  
  55.                     alert(‘请先上传头像然后选择裁切头像最后进行保存!’);  
  56.                     return false;  
  57.                 }  
  58.             };  
  59.               
  60.             function showPreview(coords)  
  61.             {  
  62.                 var rx = 150 / coords.w;  
  63.                 var ry = 150 / coords.h;  
  64.                 var w2=$("#bigwidth").val();  
  65.                 var h2=$("#bigheight").val();  
  66.                 jQuery(‘#preview’).css({  
  67.                     width: Math.round(rx * w2) + ‘px’,  
  68.                     height: Math.round(ry * h2) + ‘px’,  
  69.                     marginLeft: ‘-‘ + Math.round(rx * coords.x) + ‘px’,  
  70.                     marginTop: ‘-‘ + Math.round(ry * coords.y) + ‘px’  
  71.                 });  
  72.             }  
  73.   
  74.         </script>  
  75.   
  76.     </head>  
  77.   
  78.     <body>  
  79. <?php  
  80. if($_GET[‘act’]==‘upload’){  
  81.     if($_POST[‘upload’]==‘upload’){  
  82.           
  83.         $uploaddir =‘upload/’;  
  84.         $uploadfile = $uploaddir . basename($_FILES[‘file’][‘name’]);  
  85.         //print_r($_FILES[‘file’]);  
  86.         //echo $uploadfile;  
  87.       
  88.         if (move_uploaded_file($_FILES[‘file’][‘tmp_name’], $uploadfile)) {  
  89.             list($w$h$type$attr)=getimagesize($uploadfile);  
  90.             $str=;  
  91.             if($w>550){  
  92.                 $str="width:550px;";  
  93.             }  
  94.             if($h>550){  
  95.                 $str.="  height:550px;";  
  96.             }  
  97.             $str=emptyempty($str)?:"style=’ ".$str." ‘";  
  98.             $f1="<img src=’$uploadfile’ border=0 $str id=’cropbox’ >";  
  99.             $f2="<img src=’$uploadfile’ border=0  $str id=’preview’ >";  
  100.   
  101.               
  102.               
  103.             echo ‘<script language="javascript">parent.$("#showBig").html("’.$f1.‘");parent.$("#showThumb").html("’.$f2.‘");parent.goss();parent.$("#bigwidth").val("’.$w.‘");parent.$("#bigheight").val("’.$h.‘");parent.$("#bigImage").val("’.$uploadfile.‘");</script>’;  
  104.                   
  105.         }else {  
  106.             echo "<script>alert(‘文件上传失败!’);</script>";  
  107.         }  
  108.           
  109.     }  
  110. ?>  
  111.   
  112. <div style="margin:0px;font-size:12px;">  
  113. <FORM ACTION="?act=upload" METHOD=POST enctype="multipart/form-data">  
  114.   
  115.     <input type="file" name="file" id="file" />  
  116.   
  117.   <input type="submit" name="button" id="button" value="提交" />  
  118.   
  119.   <input name="upload" type="hidden" id="upload" value="upload" /><input type="hidden" name="MAX_FILE_SIZE" value="3000000" />  
  120. </FORM>  
  121. </div>  
  122.   
  123.   <?php  
  124.   exit;  
  125. }  
  126. ?>  
  127.   
  128.   
  129. <div id="showBig" style="width:500px;height:500px;border:2px solid #E6E0CE;padding:3px;"></div>  
  130. <iframe style="width:500px;height:60px;padding:0px;" src="?act=upload"></iframe>  
  131.   
  132.   
  133. <div id="showThumb" style="width:152px;height:152px;border:1px solid #cccccc;padding:1px; overflow: hidden;"></div>  
  134. <div style="margin-top:20px;">  
  135.     <form action="?act=saveThumb" method="post" onsubmit="return checkCoords();">  
  136.         <input type="hidden" id="bigImage" name="bigImage" />  
  137.         <input type="hidden" id="bigwidth" name="bigwidth" />  
  138.         <input type="hidden" id="bigheight" name="bigheight" />  
  139.         <input type="hidden" id="x" name="x" />  
  140.         <input type="hidden" id="y" name="y" />  
  141.         <input type="hidden" id="w" name="w" />  
  142.         <input type="hidden" id="h" name="h" />  
  143.         <input type="submit" value="保存用户头像" />  
  144.     </form>  
  145. </div>  
  146.     </body>  
  147.   
  148. </html>  

 

 

jcrop.zip

jquery使用Jcrop插件轻松实现上传图片后选取区域做头像

jquery使用Jcrop插件轻松实现上传图片后选取区域做头像

一般网站上传头像部分会比较麻烦,如果完全程序控制的话,程序把上传的图片自动裁切成指定大小的头像的话,很有可能会破坏头像的整体美观。所以现在大多数web2.0网站都有用js或flash来实现头像的上传与选取!

现在用jquery加上jcrop插件的话,就可以非常简单的实现这些功能!如果再加上编写的一些ajax处理部分的话就基本上可以满足一般web2.0的使用要求了

PHP代码
  1. <?php  
  2. if($_GET[‘act’]==‘saveThumb’){  
  3.     $targ_w = $targ_h = 150;  
  4.     $jpeg_quality = 100;  
  5.   
  6.     $src = $_POST[‘bigImage’];  
  7.     $img_r = imagecreatefromjpeg($src);  
  8.     $dst_r = ImageCreateTrueColor( $targ_w$targ_h );  
  9.   
  10.     imagecopyresampled($dst_r,$img_r,0,0,$_POST[‘x’],$_POST[‘y’],$targ_w,$targ_h,$_POST[‘w’],$_POST[‘h’]);  
  11.   
  12.     header(‘Content-type: image/jpeg’);  
  13.     imagejpeg($dst_r,null,$jpeg_quality);  
  14.       
  15.     exit;  
  16. }  
  17. ?>  
  18. <html>  
  19.     <head>  
  20.   
  21.         <script src="jquery.pack.js"></script>  
  22.         <script src="jquery.Jcrop.pack.js"></script>  
  23.           
  24.         <link rel="stylesheet" href="jquery.Jcrop.css" type="text/css" />  
  25.           
  26.         <script language="Javascript">  
  27.   
  28.             $(function(){  
  29.   
  30.                   
  31.             });  
  32.               
  33.             function goss(){  
  34.   
  35.                 jQuery(‘#cropbox’).Jcrop({  
  36.                     onChange: showPreview,  
  37.                     onSelect: showPreview,  
  38.                     onSelect: updateCoords,  
  39.                     aspectRatio: 1  
  40.                 });  
  41.   
  42.             }  
  43.   
  44.             function updateCoords(c)  
  45.             {  
  46.                 $(‘#x’).val(c.x);  
  47.                 $(‘#y’).val(c.y);  
  48.                 $(‘#w’).val(c.w);  
  49.                 $(‘#h’).val(c.h);  
  50.             };  
  51.   
  52.             function checkCoords()  
  53.             {  
  54.                 if ($(‘#x’).val()==){  
  55.                     alert(‘请先上传头像然后选择裁切头像最后进行保存!’);  
  56.                     return false;  
  57.                 }  
  58.             };  
  59.               
  60.             function showPreview(coords)  
  61.             {  
  62.                 var rx = 150 / coords.w;  
  63.                 var ry = 150 / coords.h;  
  64.                 var w2=$("#bigwidth").val();  
  65.                 var h2=$("#bigheight").val();  
  66.                 jQuery(‘#preview’).css({  
  67.                     width: Math.round(rx * w2) + ‘px’,  
  68.                     height: Math.round(ry * h2) + ‘px’,  
  69.                     marginLeft: ‘-‘ + Math.round(rx * coords.x) + ‘px’,  
  70.                     marginTop: ‘-‘ + Math.round(ry * coords.y) + ‘px’  
  71.                 });  
  72.             }  
  73.   
  74.         </script>  
  75.   
  76.     </head>  
  77.   
  78.     <body>  
  79. <?php  
  80. if($_GET[‘act’]==‘upload’){  
  81.     if($_POST[‘upload’]==‘upload’){  
  82.           
  83.         $uploaddir =‘upload/’;  
  84.         $uploadfile = $uploaddir . basename($_FILES[‘file’][‘name’]);  
  85.         //print_r($_FILES[‘file’]);  
  86.         //echo $uploadfile;  
  87.       
  88.         if (move_uploaded_file($_FILES[‘file’][‘tmp_name’], $uploadfile)) {  
  89.             list($w$h$type$attr)=getimagesize($uploadfile);  
  90.             $str=;  
  91.             if($w>550){  
  92.                 $str="width:550px;";  
  93.             }  
  94.             if($h>550){  
  95.                 $str.="  height:550px;";  
  96.             }  
  97.             $str=emptyempty($str)?:"style=’ ".$str." ‘";  
  98.             $f1="<img src=’$uploadfile’ border=0 $str id=’cropbox’ >";  
  99.             $f2="<img src=’$uploadfile’ border=0  $str id=’preview’ >";  
  100.   
  101.               
  102.               
  103.             echo ‘<script language="javascript">parent.$("#showBig").html("’.$f1.‘");parent.$("#showThumb").html("’.$f2.‘");parent.goss();parent.$("#bigwidth").val("’.$w.‘");parent.$("#bigheight").val("’.$h.‘");parent.$("#bigImage").val("’.$uploadfile.‘");</script>’;  
  104.                   
  105.         }else {  
  106.             echo "<script>alert(‘文件上传失败!’);</script>";  
  107.         }  
  108.           
  109.     }  
  110. ?>  
  111.   
  112. <div style="margin:0px;font-size:12px;">  
  113. <FORM ACTION="?act=upload" METHOD=POST enctype="multipart/form-data">  
  114.   
  115.     <input type="file" name="file" id="file" />  
  116.   
  117.   <input type="submit" name="button" id="button" value="提交" />  
  118.   
  119.   <input name="upload" type="hidden" id="upload" value="upload" /><input type="hidden" name="MAX_FILE_SIZE" value="3000000" />  
  120. </FORM>  
  121. </div>  
  122.   
  123.   <?php  
  124.   exit;  
  125. }  
  126. ?>  
  127.   
  128.   
  129. <div id="showBig" style="width:500px;height:500px;border:2px solid #E6E0CE;padding:3px;"></div>  
  130. <iframe style="width:500px;height:60px;padding:0px;" src="?act=upload"></iframe>  
  131.   
  132.   
  133. <div id="showThumb" style="width:152px;height:152px;border:1px solid #cccccc;padding:1px; overflow: hidden;"></div>  
  134. <div style="margin-top:20px;">  
  135.     <form action="?act=saveThumb" method="post" onsubmit="return checkCoords();">  
  136.         <input type="hidden" id="bigImage" name="bigImage" />  
  137.         <input type="hidden" id="bigwidth" name="bigwidth" />  
  138.         <input type="hidden" id="bigheight" name="bigheight" />  
  139.         <input type="hidden" id="x" name="x" />  
  140.         <input type="hidden" id="y" name="y" />  
  141.         <input type="hidden" id="w" name="w" />  
  142.         <input type="hidden" id="h" name="h" />  
  143.         <input type="submit" value="保存用户头像" />  
  144.     </form>  
  145. </div>  
  146.     </body>  
  147.   
  148. </html>  

下载

 

jcrop.zip

在windows下启用php的mail()函数进行发信

其实要想在windows下使用php的mail()函数进行发信的话,只要机器里安装了smtp就可以了

当然iis有内置的smtp,可是如果web服务器安装的是apache的话总不可能为了一个smtp而再去安装一个iis吧

所以找了个简单的smtp服务器软件(1st SMTP Server)找的是一个老版本的,才700多k,没有其它的无用功能!

先下载好1st SMTP Server后,运行里面的注册机,再运行主程序,进行注册。注册完了后就可以关闭主程序窗口了,在任务栏里双击1st SMTP Server图标,可以看到它的主界面!这时,应该smtp服务就正常运行了,像dns和smtp端口之类的默认就好了。

未命名.jpg

第二步。设置php.ini

找到

[mail function]
SMTP = localhost
smtp_port = 25
sendmail_from = web@phpd.cn

 

如果加了;的话,去掉就可以了,apache重启一下,这样php的mail()就可以正常工作了

 

1st SMTP Server 下载

fstsmtp.rar

 

今天花了一天时间整了个简单的框架

写时好像老是受thinkphp及fleaphp的影响,所以写了个不伦不类

而且好像效率上也不太行,光框架就占内存221k,执行速度也在0.02秒左右 如果加上smarty的话,内存立即增到500多k执行时间0.03秒左右!

以后把它再优化一下,写这个框架的初衷就是为了快速,低消耗!写下来后才发现要改正的地方还不少!

目前框架还是个半成品,只是实现了控制器部分,视图用的是smarty 然后数据模型部分现在还没有完善,缓存处理方面还没写!

未命名.jpg