Posts Tagged ‘php’

c java php 语言性能对比

同一台服务器测试

C      0.04 秒

Java   0.21 秒

PHP  2.06  秒

由此可算出 C 是 java性能的5倍,Java 是 PHP 性能的10倍

C

#include <stdio.h>
#include <sys/time.h>

#define BAILOUT 16
#define MAX_ITERATIONS 1000

int mandelbrot(double x, double y)
{
	double cr = y - 0.5;
	double ci = x;
	double zi = 0.0;
	double zr = 0.0;
	int i = 0;

	while(1) {
		i ++;
		double temp = zr * zi;
		double zr2 = zr * zr;
		double zi2 = zi * zi;
		zr = zr2 - zi2 + cr;
		zi = temp + temp + ci;
		if (zi2 + zr2 > BAILOUT)
			return i;
		if (i > MAX_ITERATIONS)
			return 0;
	}

}

int main (int argc, const char * argv[]) {
	struct timeval aTv;
	gettimeofday(&aTv, NULL);
	long init_time = aTv.tv_sec;
	long init_usec = aTv.tv_usec;

	int x,y;
	for (y = -39; y < 39; y++) {
		printf("\n");
		for (x = -39; x < 39; x++) {
			int i = mandelbrot(x/40.0, y/40.0);
			if (i==0)
				printf("*");
			else
				printf(" ");
		}
	}
	printf ("\n");

	gettimeofday(&aTv,NULL);
	double query_time = (aTv.tv_sec - init_time) + (double)(aTv.tv_usec - init_usec)/1000000.0;
	printf ("C Elapsed %0.2f\n", query_time);
    return 0;
}

Java

import java.util.*;

class Mandelbrot
{
	static int BAILOUT = 16;
	static int MAX_ITERATIONS = 1000;

	private static int iterate(float x, float y)
	{
		float cr = y-0.5f;
		float ci = x;
		float zi = 0.0f;
		float zr = 0.0f;
		int i = 0;
		while (true) {
			i++;
			float temp = zr * zi;
			float zr2 = zr * zr;
			float zi2 = zi * zi;
			zr = zr2 - zi2 + cr;
			zi = temp + temp + ci;
			if (zi2 + zr2 > BAILOUT)
				return i;
			if (i > MAX_ITERATIONS)
				return 0;
		}
	}

	public static void main(String args[])
	{
		Date d1 = new Date();
		int x,y;
		for (y = -39; y < 39; y++) {
			System.out.print("\n");
			for (x = -39; x < 39; x++) {
				if (iterate(x/40.0f,y/40.0f) == 0)
					System.out.print("*");
				else
					System.out.print(" ");

			}
		}
		Date d2 = new Date();
		long diff = d2.getTime() - d1.getTime();
		System.out.println("\nJava Elapsed " + diff/1000.0f);

	}
}

PHP

<?php
define("BAILOUT",16);
define("MAX_ITERATIONS",1000);

class Mandelbrot
{

	function Mandelbrot()
	{
		$d1 = microtime(1);
		for ($y = -39; $y < 39; $y++) {
			echo("\n");
			for ($x = -39; $x < 39; $x++) {
				if ($this->iterate($x/40.0,$y/40.0) == 0)
					echo("*");
				else
					echo(" ");

			}
		}
		$d2 = microtime(1);
		$diff = $d2 - $d1;
		printf("\nPHP Elapsed %0.2f", $diff);
	}

	function iterate($x,$y)
	{
		$cr = $y-0.5;
		$ci = $x;
		$zi = 0.0;
		$zr = 0.0;
		$i = 0;
		while (true) {
			$i++;
			$temp = $zr * $zi;
			$zr2 = $zr * $zr;
			$zi2 = $zi * $zi;
			$zr = $zr2 - $zi2 + $cr;
			$zi = $temp + $temp + $ci;
			if ($zi2 + $zr2 > BAILOUT)
				return $i;
			if ($i > MAX_ITERATIONS)
				return 0;
		}

	}

}

$m = new Mandelbrot();
?>

C语言实现的简单 Web 服务器

/*
* WebServer.c
*
*  Created on: Nov 3, 2012
*      Author: pavithra
*
* A web server in C language using only the standard libraries.
* The port number is passed as an argument.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#define EOL “\r\n”
#define EOL_SIZE 2
typedef struct {
char *ext;
char *mediatype;
} extn;
//Possible media types
extn extensions[] ={
{“gif”, “image/gif” },
{“txt”, “text/plain” },
{“jpg”, “image/jpg” },
{“jpeg”,”image/jpeg”},
{“png”, “image/png” },
{“ico”, “image/ico” },
{“zip”, “image/zip” },
{“gz”,  ”image/gz”  },
{“tar”, “image/tar” },
{“htm”, “text/html” },
{“html”,”text/html” },
{“php”, “text/html” },
{“pdf”,”application/pdf”},
{“zip”,”application/octet-stream”},
{“rar”,”application/octet-stream”},
{0,0} };
/*
A helper function
*/
void error(const char *msg) {
perror(msg);
exit(1);
}
/*
A helper function
*/
int get_file_size(int fd) {
struct stat stat_struct;
if (fstat(fd, &stat_struct) == -1)
return (1);
return (int) stat_struct.st_size;
}
/*
A helper function
*/
void send_new(int fd, char *msg) {
int len = strlen(msg);
if (send(fd, msg, len, 0) == -1) {
printf(“Error in send\n”);
}
}
/*
This function recieves the buffer
until an “End of line(EOL)” byte is recieved
*/
int recv_new(int fd, char *buffer) {
char *p = buffer; // Use of a pointer to the buffer rather than dealing with the buffer directly
int eol_matched = 0; // Use to check whether the recieved byte is matched with the buffer byte or not
while (recv(fd, p, 1, 0) != 0) // Start receiving 1 byte at a time
{
if (*p == EOL[eol_matched]) // if the byte matches with the first eol byte that is ‘\r’
{
++eol_matched;
if (eol_matched == EOL_SIZE) // if both the bytes matches with the EOL
{
*(p + 1 – EOL_SIZE) = ‘\0′; // End the string
return (strlen(buffer)); // Return the bytes recieved
}
} else {
eol_matched = 0;
}
p++; // Increment the pointer to receive next byte
}
return (0);
}
/*
A helper function: Returns the
web root location.
*/
char* webroot() {
// open the file “conf” for reading
FILE *in = fopen(“conf”, “rt”);
// read the first line from the file
char buff[1000];
fgets(buff, 1000, in);
// close the stream
fclose(in);
char* nl_ptr = strrchr(buff, ‘\n’);
if (nl_ptr != NULL)
*nl_ptr = ‘\0′;
return strdup(buff);
}
/*
Handles php requests
*/
void php_cgi(char* script_path, int fd) {
send_new(fd, “HTTP/1.1 200 OK\n Server: Web Server in C\n Connection: close\n”);
dup2(fd, STDOUT_FILENO);
char script[500];
strcpy(script, “SCRIPT_FILENAME=”);
strcat(script, script_path);
putenv(“GATEWAY_INTERFACE=CGI/1.1″);
putenv(script);
putenv(“QUERY_STRING=”);
putenv(“REQUEST_METHOD=GET”);
putenv(“REDIRECT_STATUS=true”);
putenv(“SERVER_PROTOCOL=HTTP/1.1″);
putenv(“REMOTE_HOST=127.0.0.1″);
execl(“/usr/bin/php-cgi”, “php-cgi”, NULL);
}
/*
This function parses the HTTP requests,
arrange resource locations,
check for supported media types,
serves files in a web root,
sends the HTTP error codes.
*/
int connection(int fd) {
char request[500], resource[500], *ptr;
int fd1, length;
if (recv_new(fd, request) == 0) {
printf(“Recieve Failed\n”);
}
printf(“%s\n”, request);
// Check for a valid browser request
ptr = strstr(request, ” HTTP/”);
if (ptr == NULL) {
printf(“NOT HTTP !\n”);
} else {
*ptr = 0;
ptr = NULL;
if (strncmp(request, “GET “, 4) == 0) {
ptr = request + 4;
}
if (ptr == NULL) {
printf(“Unknown Request ! \n”);
} else {
if (ptr[strlen(ptr) - 1] == ‘/’) {
strcat(ptr, “index.html”);
}
strcpy(resource, webroot());
strcat(resource, ptr);
char* s = strchr(ptr, ‘.’);
int i;
for (i = 0; extensions[i].ext != NULL; i++) {
if (strcmp(s + 1, extensions[i].ext) == 0) {
fd1 = open(resource, O_RDONLY, 0);
printf(“Opening \”%s\”\n”, resource);
if (fd1 == -1) {
printf(“404 File not found Error\n”);
send_new(fd, “HTTP/1.1 404 Not Found\r\n”);
send_new(fd, “Server : Web Server in C\r\n\r\n”);
send_new(fd, “<html><head><title>404 Not Found</head></title>”);
send_new(fd, “<body><p>404 Not Found: The requested resource could not be found!</p></body></html>”);
//Handling php requests
} else if (strcmp(extensions[i].ext, “php”) == 0) {
php_cgi(resource, fd);
sleep(1);
close(fd);
exit(1);
} else {
printf(“200 OK, Content-Type: %s\n\n”,
extensions[i].mediatype);
send_new(fd, “HTTP/1.1 200 OK\r\n”);
send_new(fd, “Server : Web Server in C\r\n\r\n”);
if (ptr == request + 4) // if it is a GET request
{
if ((length = get_file_size(fd1)) == -1)
printf(“Error in getting size !\n”);
size_t total_bytes_sent = 0;
ssize_t bytes_sent;
while (total_bytes_sent < length) {
//Zero copy optimization
if ((bytes_sent = sendfile(fd, fd1, 0,
length – total_bytes_sent)) <= 0) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
perror(“sendfile”);
return -1;
}
total_bytes_sent += bytes_sent;
}
}
}
break;
}
int size = sizeof(extensions) / sizeof(extensions[0]);
if (i == size – 2) {
printf(“415 Unsupported Media Type\n”);
send_new(fd, “HTTP/1.1 415 Unsupported Media Type\r\n”);
send_new(fd, “Server : Web Server in C\r\n\r\n”);
send_new(fd, “<html><head><title>415 Unsupported Media Type</head></title>”);
send_new(fd, “<body><p>415 Unsupported Media Type!</p></body></html>”);
}
}
close(fd);
}
}
shutdown(fd, SHUT_RDWR);
}
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
if (argc < 2) {
fprintf(stderr, “ERROR, no port provided\n”);
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error(“ERROR opening socket”);
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error(“ERROR on binding”);
listen(sockfd, 5);
clilen = sizeof(cli_addr);
/*
Server runs forever, forking off a separate
process for each connection.
*/
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error(“ERROR on accept”);
pid = fork();
if (pid < 0)
error(“ERROR on fork”);
if (pid == 0) {
close(sockfd);
connection(newsockfd);
exit(0);
} else
close(newsockfd);
} /* end of while */
close(sockfd);
return 0; /* we never get here */
}

window下扩展开发 (php5.3.5+vs2010)

开发环境的搭建
首先我们需要一个 PHP 的源码包,这个可以到http://www.php.net/downloads.php 去下载,记得我们是要源码包(Complete Source Code)而不是PHP 的二进制代码包(Windows Binaries)。本文所采用是 PHP 5.3.5 的源码包。
除此之外我们还需要一个 php5ts.lib 。这在PHP 二进制代码包的 dev 目录(php4ts.lib 则是直接放在二进制代码包的根目录)下可以找到。
将该源码包解压到某个目录(假定是D:\Work\PHP\work\php5,以后我们以 $PHP 指代该源码根目录),我们可以看到main、Zend、win32、TSRM、ext 等目录。在 ext 目录下有 ext_skel 和 ext_skel_win32.php 两个文件。
Ext_skel 是在 xNix 环境下的一个用于构建PHP 扩展,生成 PHP 扩展框架的自动化脚本。由于是在 xNix 环境下使用的,并且使用方法也比较简单,故本文不再赘述。具体使用方法可参见源码包根目录(即 $PHP)下的README.EXT_SKEL 文件。ext_skel_win32.php 顾名思义是用来创建 Win32 环境下扩展框架的的脚本。这个脚本需要 Cygwin (http://www.cygwin.com/) 的支持。使用方法和ext_skel 大同小异。本文所采用的是第三种方法:使用 VC 的向导手动创建一个项目文件。这种方法好处就是不需要 Cygwin 的支持,但在编译该扩展的 xNix 版本时仍然需要通过ext_skel 来创建一个相应的框架。
我们在这里使用的 IDE 是 VC++ 2010 Express Edition 。如果你的扩展将来需要分发到更多的地方,建议你使用 VC++ 6.0,这样可增加一定的兼容性。PHP 扩展在 VC++ 6.0 和 VC++ 2010里面的操作都差不多,但在 VC++ 2010 中需要进行一些额外的设置。
现在让我们打开 VC++ 2010,在菜单中选择 【File】 -> 【New】 -> 【Project】 来创建这个扩展的项目文件。
在【项目类型(Project Types)】中选择“Virual C++”,项目【模版(Templates)】为“Win32 Project”。在本例中我们的扩展名字为 phpmore,位于 $PHP\ext目录下。
提示:虽然一个扩展项目的存放位置并没有具体规定,但放到 $PHP\ext 目录下是一个惯例,这会避免很多不必要的麻烦。
在出现的 【Win32 应用程序向导(Win32 Application Wizard)】中点击左面的【应用程序设置(Application Settings)】,设置【程序类型(Application Type)】为“DLL”,并且将其设置为【空项目(Empty Project)】。
点击【完成(Finish)】就创建了该扩展的项目文件。此时你应该会在 $PHP\ext\phpmore 目录下找到该扩展的“解决方案(solution)”文件。在$PHP\ext\phpmore\ phpmore 目录下找到扩展的“项目(Project)”文件。
提示:按照 VS 2010 的说法,一个“解决方案(solution)”是由多个“项目(Project)”组成的,因此产生这样的目录结构是十分合理的。但对 PHP 扩展而言,由于需要在各个系统平台下运行,如果把所有平台的项目文件都放在扩展的根目录下面,就会给人一种非常凌乱的感觉。一个值得推荐的解决方法就是为每个平台都建立一个目录,各自包含相应的项目文件,而把源代码文件(*.c 和 *.h 等)放在扩展根目录或其他一个单独的目录。本文为了简单叙述起见,不再额外处理,但在实际应用过程中请注意源代码目录的合理分配。
现在我们将扩展的源代码文件(phpmore.c 和 phpmore.h ,当然此时是空文件)新建/添加到 phpmore 扩展的项目文件当中。
提示:在 VC++ 2010 中添加源代码文件时默认的后缀名为 .cpp,此时需要主动为文件添加上 .c 的扩展名。否则 VC 的编译器会将其默认为 C++ 代码而进行编译(当然这种设置也是可以改变的),这样就可能会产生一些编译错误。
为了能够很方便的引用 PHP 代码的头文件以及对项目进行编译,我们还需要对项目文件进行一些设置。请通过菜单【项目(Project)】-> 【phpmore 属性(Properties)】进入项目的属性设置页。这里我们先对项目的【Release】版进行配置。
先转到【C++】属性的【General】页填入“Additional Include Directories”:
$PHP;$PHP\main;$PHP\win32;$PHP\TSRM;$PHP\Zend。我们这里输入的绝对路径,但实际开发过程中最好填入相对路径。
再转到【C++】属性的【Preprocessor】页补充一些“Preprocessor Definitions”: ZEND_WIN32;PHP_WIN32;ZTS=1; ZEND_DEBUG=0; COMPILE_DL_PHPMORE 。前面3个是在 Win32 环境下开发所必加的预定义;ZEND_DEBUG=0表示扩展不创建为Debug 版本(因为现在是在配置Release 版本嘛~);COMPILE_DL_PHPMORE 用于是否将本扩展编译为一个“外部扩展(定义见文首)”。
在【C++】属性里面还需要设置的有:【Code Generation】页的“运行库(Runtime Library)”请设置为“Multi-threaded DLL (/MD)”;【Advanced】页的“编译方式(Compile As)”请设置为“Compile as C Code (/TC)”
此外还需要在【连接器(Linker)】属性的【Input】页添加一个“Additional Dependencies”: php5ts.lib 。你可以把 php5ts.lib 放到一个 VC++ 能找到的地方,比如项目文件的目录。
这样,整个扩展项目文件的Release 版本就配置好了。对于 Debug 版本可以有针对性的作一些改动。不过需要注意,一般的 PHP 二进制代码包不允许加载 Debug 版本的扩展,只有将 PHP 编译为 Debug 版本才能加载 Debug 版本的扩展。
提示:如果需要扩展在多种PHP版本中都可布署,那可以先设置一个基本配置(就像上例不设置 php5ts.lib),然后再创建一个继承自基本配置的新的配置-比如Release_PHP5-在这个 Release_PHP5 中额外设置一下 php5ts.lib 就可以了。有的扩展还不事先预定义 ZTS,而是额外再创建一个 Release_TS 的配置,道理是一样的。
在编译过程中会提示你找不到config.w32.h 这个文件 按以下步骤来操作
下载2个必要的包
http://www.php.net/extra/bindlib_w32.zip
http://www.php.net/extra/win32build.zip
把 这2个包的内容放一起,例如解压缩到 D:\win32build

请使用 Visual Studio Tools 下的 Visual Studio 命令提示 操作
进入D:\php-src\
执行buildconf.bat
建立一个临时环境变量,执行set path=%path%;D:\win32build\bin

执行 cscript /nologo configure.js –with-php-build=”../win32build” –without-libxml –disable-odbc
如果想 要No Thread Safe 模式就在上面的命令最后加上参数 –disable-zts

然后看看是不是main下面多了一个 config.w32.h~
还有一点,config.w32.h 里面 #define PHP_COMPILER_ID 改成和你正在用的 PHP 编译版本相同的编译器ID,例如VC6或者VC9,不然编译出来的扩展没法载入,说实话,这个ID的判断真的很傻很天真….
OK,现在万事俱备,只欠编码了,让我们这就开始吧!
所有的扩展都大致由4个部分组成:引用相关的头文件、Zend 模块的声明与相关函数实现、get_module() 函数的实现以及导出函数的声明和实现。

开发环境的搭建
首先我们需要一个 PHP 的源码包,这个可以到http://www.php.net/downloads.php 去下载,记得我们是要源码包(Complete Source Code)而不是PHP 的二进制代码包(Windows Binaries)。本文所采用是 PHP 5.3.5 的源码包。除此之外我们还需要一个 php5ts.lib 。这在PHP 二进制代码包的 dev 目录(php4ts.lib 则是直接放在二进制代码包的根目录)下可以找到。
将该源码包解压到某个目录(假定是D:\Work\PHP\work\php5,以后我们以 $PHP 指代该源码根目录),我们可以看到main、Zend、win32、TSRM、ext 等目录。在 ext 目录下有 ext_skel 和 ext_skel_win32.php 两个文件。Ext_skel 是在 xNix 环境下的一个用于构建PHP 扩展,生成 PHP 扩展框架的自动化脚本。由于是在 xNix 环境下使用的,并且使用方法也比较简单,故本文不再赘述。具体使用方法可参见源码包根目录(即 $PHP)下的README.EXT_SKEL 文件。ext_skel_win32.php 顾名思义是用来创建 Win32 环境下扩展框架的的脚本。这个脚本需要 Cygwin (http://www.cygwin.com/) 的支持。使用方法和ext_skel 大同小异。本文所采用的是第三种方法:使用 VC 的向导手动创建一个项目文件。这种方法好处就是不需要 Cygwin 的支持,但在编译该扩展的 xNix 版本时仍然需要通过ext_skel 来创建一个相应的框架。
我们在这里使用的 IDE 是 VC++ 2010 Express Edition 。如果你的扩展将来需要分发到更多的地方,建议你使用 VC++ 6.0,这样可增加一定的兼容性。PHP 扩展在 VC++ 6.0 和 VC++ 2010里面的操作都差不多,但在 VC++ 2010 中需要进行一些额外的设置。
现在让我们打开 VC++ 2010,在菜单中选择 【File】 -> 【New】 -> 【Project】 来创建这个扩展的项目文件。
在【项目类型(Project Types)】中选择“Virual C++”,项目【模版(Templates)】为“Win32 Project”。在本例中我们的扩展名字为 phpmore,位于 $PHP\ext目录下。
提示:虽然一个扩展项目的存放位置并没有具体规定,但放到 $PHP\ext 目录下是一个惯例,这会避免很多不必要的麻烦。
在出现的 【Win32 应用程序向导(Win32 Application Wizard)】中点击左面的【应用程序设置(Application Settings)】,设置【程序类型(Application Type)】为“DLL”,并且将其设置为【空项目(Empty Project)】。点击【完成(Finish)】就创建了该扩展的项目文件。此时你应该会在 $PHP\ext\phpmore 目录下找到该扩展的“解决方案(solution)”文件。在$PHP\ext\phpmore\ phpmore 目录下找到扩展的“项目(Project)”文件。
提示:按照 VS 2010 的说法,一个“解决方案(solution)”是由多个“项目(Project)”组成的,因此产生这样的目录结构是十分合理的。但对 PHP 扩展而言,由于需要在各个系统平台下运行,如果把所有平台的项目文件都放在扩展的根目录下面,就会给人一种非常凌乱的感觉。一个值得推荐的解决方法就是为每个平台都建立一个目录,各自包含相应的项目文件,而把源代码文件(*.c 和 *.h 等)放在扩展根目录或其他一个单独的目录。本文为了简单叙述起见,不再额外处理,但在实际应用过程中请注意源代码目录的合理分配。
现在我们将扩展的源代码文件(phpmore.c 和 phpmore.h ,当然此时是空文件)新建/添加到 phpmore 扩展的项目文件当中。
提示:在 VC++ 2010 中添加源代码文件时默认的后缀名为 .cpp,此时需要主动为文件添加上 .c 的扩展名。否则 VC 的编译器会将其默认为 C++ 代码而进行编译(当然这种设置也是可以改变的),这样就可能会产生一些编译错误。
为了能够很方便的引用 PHP 代码的头文件以及对项目进行编译,我们还需要对项目文件进行一些设置。请通过菜单【项目(Project)】-> 【phpmore 属性(Properties)】进入项目的属性设置页。这里我们先对项目的【Release】版进行配置。见图四。
先转到【C++】属性的【General】页填入“Additional Include Directories”: $PHP;$PHP\main;$PHP\win32;$PHP\TSRM;$PHP\Zend。我们这里输入的绝对路径,但实际开发过程中最好填入相对路径。
再转到【C++】属性的【Preprocessor】页补充一些“Preprocessor Definitions”: ZEND_WIN32;PHP_WIN32;ZTS=1; ZEND_DEBUG=0; COMPILE_DL_PHPMORE 。前面3个是在 Win32 环境下开发所必加的预定义;ZEND_DEBUG=0表示扩展不创建为Debug 版本(因为现在是在配置Release 版本嘛~);COMPILE_DL_PHPMORE 用于是否将本扩展编译为一个“外部扩展(定义见文首)”。
在【C++】属性里面还需要设置的有:【Code Generation】页的“运行库(Runtime Library)”请设置为“Multi-threaded DLL (/MD)”;【Advanced】页的“编译方式(Compile As)”请设置为“Compile as C Code (/TC)”
此外还需要在【连接器(Linker)】属性的【Input】页添加一个“Additional Dependencies”: php5ts.lib 。你可以把 php5ts.lib 放到一个 VC++ 能找到的地方,比如项目文件的目录。当然你若采用的是 PHP 4的源码包,请相应地把php5ts.lib 替换为 php4ts.lib 。
这样,整个扩展项目文件的Release 版本就配置好了。对于 Debug 版本可以有针对性的作一些改动。不过需要注意,一般的 PHP 二进制代码包不允许加载 Debug 版本的扩展,只有将 PHP 编译为 Debug 版本才能加载 Debug 版本的扩展。
提示:如果需要扩展在多种PHP版本中都可布署,那可以先设置一个基本配置(就像上例不设置 php5ts.lib),然后再创建一个继承自基本配置的新的配置-比如Release_PHP5-在这个 Release_PHP5 中额外设置一下 php5ts.lib 就可以了。有的扩展还不事先预定义 ZTS,而是额外再创建一个 Release_TS 的配置,道理是一样的。
OK,现在万事俱备,只欠编码了,让我们这就开始吧!
所有的扩展都大致由4个部分组成:引用相关的头文件、Zend 模块的声明与相关函数实现、get_module() 函数的实现以及导出函数的声明和实现。

为了简单叙述起见,我先列出本文例子的代码:

phpmore.h :

01 #ifndef PHPMORE_H
02 #define PHPMORE_H
03 extern zend_module_entry phpmore_module_entry;
04 #define phpext_phpmore_ptr &phpmore_module_entry
05
06 /* declaration of functions to be exported */
07 ZEND_FUNCTION(welcome_to_phpmore);
08 PHP_MINFO_FUNCTION(phpmore);
09
10 #define PHPMORE_VERSION "0.1.0"
11 #endif

phpmore.c :

01 #define _USE_32BIT_TIME_T 1
02 #include "php.h"
03 #include "phpmore.h"
04
05 zend_function_entry phpmore_functions[] =
06 {
07 ZEND_FE(welcome_to_phpmore, NULL)
08 {NULL, NULL, NULL}
09 };
10
11 zend_module_entry phpmore_module_entry =
12 {
13 STANDARD_MODULE_HEADER,
14 "PHP&More",
15 phpmore_functions,
16 NULL,
17 NULL,
18 NULL,
19 NULL,
20 PHP_MINFO(phpmore),
21 PHPMORE_VERSION,
22 STANDARD_MODULE_PROPERTIES
23 };
24
25 #if COMPILE_DL_PHPMORE
26 ZEND_GET_MODULE(phpmore)
27 #endif
28
29 PHP_MINFO_FUNCTION(phpmore)
30 {
31 php_info_print_table_start();
32 php_info_print_table_header(2, "PHP&More", "enabled");
33 php_info_print_table_row(2, "Version", PHPMORE_VERSION);
34 php_info_print_table_end();
35 }
36
37 ZEND_FUNCTION(welcome_to_phpmore)
38 {
39 zend_printf("Welcome to PHP&More!");
40 }

所有扩展都必须至少包含有 php.h ,这是一切的基础,因此必须首先在代码中引用(添加 #define _USE_32BIT_TIME_T 1 这一行是为了去掉 VC++ 2005 中 64 位时间格式的支持,在 VS.NET 2003 或 VC++ 6.0 中均无需这样做)。

接下来是扩展的 Zend 函数块的声明。定义了一个名为 phpmore_functions ,每一个元素都是一个 zend_function_entry 结构的 Zend 函数数组。该数组用来声明本扩展一共对外(即 PHP 脚本)提供了多少可用的(导出)函数。由于没有其他地方可以主动提供(导出)函数的个数,因此数组的最后一个元素必须为 {NULL, NULL, NULL},以便 Zend Engine 可以获知函数数组的元素列表是否结束。

然后就是整个扩展模块的声明。这是一个扩展“最高”层次的声明。全方位地提供了 Zend Engine 所需要的各种信息。上面所声明的导出函数列表也仅仅是用来填充它的一个字段而已。除此之外,这个模块声明还负责提供扩展名称(就是将来在 phpinfo() 函数中出现的那个扩展的名字,本例为“PHP&More”)、导出函数列表(本例为phpmore_functions)、模块启动函数(PHP_MINIT_FUNCTION,在模块第一次加载时被调用,本例为 NULL)、模块关闭函数(PHP_MSHUTDOWN_FUNCTION,在模块卸载关闭时被调用,本例为 NULL)、请求启动函数(在每个请求启动时被调用,本例为 NULL)、请求关闭函数(PHP_RINIT_FUNCTION,在每个请求关闭时被调用,本例为 NULL)、模块信息函数(PHP_RSHUTDOWN_FUNCTION,用于在 phpinfo() 中显示扩展的信息,本例为“PHP_MINFO_FUNCTION(phpmore)”)和模块版本(本例为 PHPMORE_VERSION ,定义在 phpmore.h )等其他信息。这几个模块函数的调用关系及顺序见图:

PHP 生存周期

模块声明后面就是 get_module() 函数的实现。这个函数的声明没有手动写出,而是使用了一个宏 ZEND_GET_MODULE(phpmore) 来声明。这也是在扩展开发中常用的一种手段,我们应该尽力地去使用宏。get_module() 函数用于向 Zend Engine 报告这是个外部扩展,这也可以使得我们能够通过 dl() 函数来手动加载它。

剩下的两段代码便是我们前面声明函数的具体实现。一个是模块信息函数,一个是对外导出的 welcome_to_phpmore 函数。模块信息函数对外输出了本扩展的启用状态和版本号,而 welcome_to_phpmore 函数则在 PHP 脚本调用 welcome_to_phpmore() 时对外输出字符串“Welcome to PHP&More!”。

一个扩展的大致结构就是这样。简单编译后我们就得到了一个 phpmore.dll 的文件。相应更改更改 php.ini 及重新启动 Web 服务器后,就可以启用这个扩展了。

nginx apache 执行php脚本限制

Apache环境规则内容如下:Apache执行php脚本限制 把这些规则添加到.htaccess文件中

RewriteEngine on RewriteCond % !^$

RewriteRule uploads/(.*).(php)$ – [F]

RewriteRule data/(.*).(php)$ – [F]

RewriteRule templets/(.*).(php)$ –[F]

nginx环境规则内容如下:nginx执行php脚本限制

LNMP有一个缺点就是目录权限设置上不如Apache,有时候网站程序存在上传漏洞或类似pathinfo的漏洞从而导致被上传了php木马,而给网站和服务器带来比较大危险。建议将网站目录的PHP权限去掉,当访问上传目录下的php文件时就会返回403错误。

首先要编辑nginx的虚拟主机配置,在fastcgi的location语句的前面按下面的内容添加:

location ~ /(data|uploads|templets)/.*\.(php|php5)?$ {

deny all;

}

图片缩略水印类


/**
 * 图片缩放水印类
 *
  * @version 1.0 ;
 *
 */
class cls_photo
{
    protected $waterrate = 0.2; //水印图标在图片上的比例
    protected $width = 300; //缩略图默认宽度
    protected $height = 200; //缩略图默认高度
    protected $padding = 5;  //水印图到边的距离
    protected $water_mark = "./water.png";
    protected $water_mark_pos = 5;//水印图片位置(1=左上角,2=右上角,3=左下角,4=右下角,5中央)
    protected $watermode = 0;// 0缩略图时不打水印 1缩略图时打水印
    protected $magick_handle;//图片操作句柄
    protected $format = array('jpg','gif','png','jpeg'); // 图片文件格式限定
    protected $smallpic_mode = 2;//默认模式 0为不生成缩略图, 1为裁切缩放 ,2为比例缩放 3为缩放填充模式

    /**
     * 设置图片类参数
     *
     * @param $arg 图片参数 多次可放入数组里 如下
     * @param $protected 参数值
     * array(
     *      'waterrate'=>0.2,
     *      'water_mark'=>'./water.png',
     *      'water_mark_pos'=>4,
     *      'smallpic_mode'=>1
     *      );
     * @return ture/false
     */
    public function set_args($arg,$val="")
    {
        $params = array('waterrate','water_mark','water_mark_pos','smallpic_mode','watermode','width','height');
        if(is_array($arg))
        {
            foreach ($arg as $k =>$v)
            {
                if(in_array($k,$params))
                {
                    $this->$k = $v;
                }
            }
        }
        else
        {
        	if(empty($val))
        	{
        	    return false;
        	}
        	else
        	{
        		if(in_array($arg,$params))
                {
                    $this->$arg = $val;
                }
        	}
        }
        return true;
    }

    /**
     * 图片缩放
     *
     * @param $src_file 源文件路径
     * @param $dst_file 目标文件路径
     * @return 缩略图片路径/false
     */
    public function scale($src_file,$dst_file="")
    {
        $dst_width  = $this->width;
        $dst_height = $this->height;
        $mode       = $this->smallpic_mode;
        $magic_water_handle = NewMagickWand();
        if (!MagickReadImage($magic_water_handle, $src_file))return false;

        //类型
        $srcext = strtolower(MagickGetImageFormat($magic_water_handle));
        if($srcext=='bmp')
        {
            $srcext = 'jpeg';
        }
        if(!in_array($srcext,$this->format))return false;
        //尺寸
        $src_width = MagickGetImageWidth($magic_water_handle);
        $src_height = MagickGetImageHeight($magic_water_handle);

        //裁切缩放模式
        if($mode == 1)
        {
            $pos_x=$pos_y = 0;//裁切临时位置
            $src_widthc = $src_width;//裁切临时宽度
            $src_heightc = $src_height;//裁切临时高度
            if($src_width/$src_height>$dst_width/$dst_height)
            {
                $src_widthc = $src_height*$dst_width/$dst_height;
                $pos_x = ($src_width-$src_widthc)/2;

            }
            else
            {
                $src_heightc = $src_width*$dst_height/$dst_width;
                $pos_y = ($src_height-$src_heightc)/2;
            }
        MagickCropImage($magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y);//裁切
        //因为MagickCropImage函数后,Gif 图像改,但画布不变
        $this->magick_handle = NewMagickWand();
        MagickNewImage($this->magick_handle,$src_widthc,$src_heightc,'#ffffff');
        MagickSetFormat($this->magick_handle,$srcext);
        MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0);
        //缩放
        MagickScaleImage($this->magick_handle, $dst_width, $dst_height);

        }
        //比例缩放模式
        if($mode == 2)
        {
            if($src_width/$src_height>$dst_width/$dst_height)
            {
                $dst_height=$dst_width*$src_height/$src_width;
            }
            else
            {
                $dst_width=$dst_height*$src_width/$src_height;
            }
            $this->magick_handle=$magic_water_handle;//替换
            MagickScaleImage($this->magick_handle, $dst_width, $dst_height);//缩放
        }
        //缩放填充模式
        if($mode == 3)
        {
            if($src_width/$src_height>$dst_width/$dst_height)
            {
                $dst_heightc=$dst_width*$src_height/$src_width;
                $dst_widthc=$dst_width;
            }
            else
            {
                $dst_widthc=$dst_height*$src_width/$src_height;
                $dst_heightc=$dst_height;
            }
              MagickScaleImage($magic_water_handle, $dst_widthc, $dst_heightc);//缩放
            $this->magick_handle = NewMagickWand();
            MagickNewImage($this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor);
            MagickSetFormat($this->magick_handle,$srcext);
            MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,($dst_width-$dst_widthc)/2,($dst_height-$dst_heightc)/2);
        }
        //打水印
        if($this->watermode == 1)
        {
            $this->set_mark();
        }
        if(empty($dst_file))
        {
            //建立临时文件
            $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
        }
        MagickWriteImage($this->magick_handle, $dst_file);
        return $dst_file;
    }    

    /**
     * 打水印
     *
     * @param $src_file 要打水印的图片路径
     * @param $dst_file 生产水印的文件保存路径,为空则生产随机临时文件
     * @return 水印文件路径/false
     */
    public function water_mark($src_file,$dst_file="")
    {
        $this->magick_handle = NewMagickWand();
        if (!MagickReadImage($this->magick_handle, $src_file))
        return false;
        $this->set_mark();
        if(empty($dst_file))
        {
            //建立临时文件
            $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
        }
        MagickWriteImage($this->magick_handle, $dst_file);
        return $dst_file;
    }

    /**
     * 对内接口
     * 给图片打水印
     *
     */
    protected  function set_mark()
    {

        //尺寸
        $dst_width = MagickGetImageWidth($this->magick_handle);
        $dst_height = MagickGetImageHeight($this->magick_handle);
        //处理水印图
        if ($this->water_mark && is_file($this->water_mark))
        {
            $magic_water_handle = NewMagickWand();
            MagickRemoveImage($magic_water_handle);
            if (MagickReadImage($magic_water_handle, $this->water_mark))
            {
                MagickScaleImage($magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight($magic_water_handle)/MagickGetImageWidth($magic_water_handle));//缩放水印到图片的1/5
                if ($this->water_mark_pos == 1)
                {
                    $left = $this->padding;
                    $top = $this->padding;
                }
                elseif ($this->water_mark_pos == 2)
                {
                    $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
                    $top = $this->padding;
                }
                elseif ($this->water_mark_pos == 3)
                {
                    $left = $this->padding;
                    $top = $dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
                }
                elseif ($this->water_mark_pos == 4)
                {
                    $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
                    $top =$dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
                }
                elseif ($this->water_mark_pos == 5)
                {
                    $left = ($dst_width-MagickGetImageWidth($magic_water_handle))/2;
                    $top =($dst_height -MagickGetImageHeight($magic_water_handle))/2;
                }
                MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top);
            }
        }
    }
}

Hello world!

怎么没出全呢。

echo 'hello';

/**
 * 获取搜索参数数据
 *
 */
function &get_search_params()
{
	global $args, $request_obj;
	$param_arr = array();
	foreach($args as $key)
	{
		if ($value = $request_obj->GetString($key))
		{
			$param_arr[$key] = $value;
		}
		else
		{
			$param_arr[$key] = NULL;
		}
	}

	return $param_arr;
}

我来测试一下代码高亮