本文目录一览:
- php怎么响应客户端发送http请求
- [linux下WebServer 怎么实现PHP动态网页响应!](#linux下WebServer 怎么实现PHP动态网页响应!)
- 做个网站(php的),用许多的页面有相同的部分,如导航条之类的,想做个模板,问下怎么做,怎么加载?
- 怎样用PHP来给网页做导航栏
- [把用PHP语言的网站改成响应式网站 需要做什么?很麻烦吗?](#把用PHP语言的网站改成响应式网站 需要做什么?很麻烦吗?)
- PHP和DZ哪个适合做导航网站?
php怎么响应客户端发送http请求
使用$_POST['参数名']
处理post方法提交的参数,$_GET['参数名']
处理get方法参数。
eg:
如果url 为: index.html?name=123pwd=123
<?php
$name = $_GET['name'];
$pwd = $_GET['pwd'];
do something;
?>
如果url 为: index.html
name=123pwd=123
<?php
$name = $_POST['name'];
$pwd = $_POST['pwd'];
do something;
?>
如果只是处理如何要跳转到其他页面,可以用header("Location: 文件名");
如果是网页和php混合,在需要使用<?php php语句; ?>
处理就行;使用echo
可以输出一些值到网页中.
linux下WebServer 怎么实现PHP动态网页响应!
你直接去自己读 http 服务器的接口相关代码吧。 推荐个小型的 http 服务器程序 不过现在 cgi 方式是通用的,apache 的 so 方式调用貌似需要 php 这边代码的支持,也就是 apache 有 so 代码接口,php 根据这个接口做的扩展式模块。 这种方式也就两种办法,要么你的 httpd 服务器自己实现 apache 的模块接口函数,要么就去改写 php 的代码,给自己的 httpd 制作一个对应的模块功能。 当然还一个办法,那就是干脆集成 php 到你的服务器代码里。不过注意你的程序协议。php 虽然不是 gpl 的,用的是他自己的 PHP lic ,类似 BSD 但貌似不是 copyleft 。
做个网站(php的),用许多的页面有相同的部分,如导航条之类的,想做个模板,问下怎么做,怎么加载?
可以在php中先:
include($tpl->template('moban.html'));
然后再再php中加载个Template.php 就可以了。 Template.php 代码如下:
<?php
class Template
{
public $templateDir = 'templates';
public $leftTag = '{';
public $rightTag = '}';
public $compileDir = 'cache';
public $compiledFileExt = '.TemplateCompiled.php';
public $templateFileExt = '.html'; //当display() cache() 不使用参数时使用
public $caching = false;
public $cacheDir = 'cache';
public $cacheDirLevels = 0; //缓存目录层次
public $cacheFileExt = '.TemplateCache.php';
public $cacheLifeTime = 3600; // 单位 秒
public $cacheID;
public $forceCompile = false;
public $lang=array();
private $cacheFile; //缓存文件,在_saveCache()中使用
private $realCacheID; //通过计算得出的缓存ID
const MAX_CACHE_DIR_LEVELS=16; //最大缓存目录层次数量
public function __construct($arrConfig = array())
{
foreach ($arrConfig as $key=>$val) {
$this->$key = $val;
}
if ($this->cacheDirLevels>self::MAX_CACHE_DIR_LEVELS) {
$this->cacheDirLevels=self::MAX_CACHE_DIR_LEVELS;
}
}
/**
* 判断缓存文件是否有效
*
* @param string $file
* @param string $cacheID
* @return boolean
*/
public function cached($file='',$cacheID='')
{
$file=$this->getTemplateFile($file);
$this->cacheID=$cacheID;
$cachefile=$this->getCacheFileName($file,$cacheID);
if ($this->caching && is_file($cachefile) && (filemtime($cachefile)+$this->cacheLifeTime)>time()) {
return true;
} else {
return false;
}
}
/**
* 返回模板文件完整路径
*
* @param string $file
* @return string
*/
private function getTemplateFile($file='')
{
if (!strlen($file)) {
$file=App::$controller.'_'.App::$action.$this->templateFileExt;
}
return $file;
}
/**
* 获取缓存文件完整路径
*
* @param string $file
* @param string $cacheID
* @return string
*/
private function getCacheFileName($file,$cacheID)
{
if (!strlen($this->realCacheID)) {
$this->realCacheID=$cacheID!=''?$cacheID:$_SERVER['SCRIPT_NAME'].$_SERVER['QUERY_STRING'];
}
$this->realCacheID.=$this->templateDir.$file.APP_NAME;
$md5id=md5($this->realCacheID);
$this->cacheDirLevel=$this->getCacheDirLevel($md5id);
return $this->cacheDir.$this->cacheDirLevel.'/'.$md5id.$this->cacheFileExt;
}
/**
* 获取缓存目录层次
*
*/
private function getCacheDirLevel($md5id)
{
$levels=array();
$levelLen=2;
for ($i=0; $i<$this->cacheDirLevels; $i++) {
$levels[]='TepmlateCache_'.substr($md5id,$i*$levelLen,$levelLen);
}
return !count($levels) ? '' : '/'.implode('/',$levels);
}
/**
* 在$this->compile()中替换$foo.var为数组格式$foo['var']
*
*/
private function compile_replace($str)
{
$str=preg_replace('/(\$[a-z_]\w*)\.([\w]+)/',"\\1['\\2']",$str);
return $this->leftTag.$str.$this->rightTag;
}
/**
* 编译模板文件
*
* @param string $file
* @return string
*/
private function compile($file='')
{
$file=$this->getTemplateFile($file);
$fullTplPath=$this->templateDir.'/'.$file;
$compiledFile=$this->compileDir.'/'.md5($fullTplPath).$this->compiledFileExt;
if ($this->forceCompile || !is_file($compiledFile) || filemtime($compiledFile)<=filemtime($fullTplPath)) {
$content=file_get_contents($fullTplPath);
$leftTag=preg_quote($this->leftTag);
$rightTag=preg_quote($this->rightTag);
$search=array(
'/'.$leftTag.'include ([\w\.\/-]+)'.$rightTag.'/i', //导入子模板
'/'.$leftTag.'(\$[a-z_]\w*)\.(\w+)'.$rightTag.'/i', //将模板标签{$foo.var}修改为数组格式{$foo['var']}
'/'.$leftTag.'(.+?\$[a-z_]\w*\.\w+.*?)'.$rightTag.'/ie', //将模板标签中的$foo.var修改为数组格式$foo['var']
'/'.$leftTag.'(else if|elseif) (.*?)'.$rightTag.'/i',
'/'.$leftTag.'for (.*?)'.$rightTag.'/i',
'/'.$leftTag.'while (.*?)'.$rightTag.'/i',
'/'.$leftTag.'(loop|foreach) (.*?) as (.*?)'.$rightTag.'/i',
'/'.$leftTag.'if (.*?)'.$rightTag.'/i',
'/'.$leftTag.'else'.$rightTag.'/i',
'/'.$leftTag."(eval) (.*?)".$rightTag.'/is',
'/'.$leftTag.'\/(if|for|loop|foreach|while)'.$rightTag.'/i',
'/'.$leftTag.'((( *(\+\+|--) *)*?(([_a-zA-Z][\w]*$.*?$)|\$((\w+)((\$|()$(\'|")?\$*\w*$(\'|")?(\)|\$))*)((-)?\$?(\w*)(\$(\'|")?(.*?)$(\'|")?\)|))){0,})( *\.?[^ \.]*? *)*?){1,})'.$rightTag.'/i',
'/'.$leftTag.'\%([\w]+)'.$rightTag.'/', //多语言
);
$replace=array(
'<?php include($tpl->template("\\1")); ?>',
$this->leftTag."\\1['\\2']".$this->rightTag,
"\$this->compile_replace('\\1')",
'<?php }else if (\\2){ ?>',
'<?php for (\\1) { ?>',
'<?php $__i=0; while (\\1) {$__i++; ?>',
'<?php $__i=0; foreach ((array)\\2 as \\3) { $__i++; ?>',
'<?php if (\\1){ ?>',
'<?php }else{ ?>',
'<?php \\2; ?>',
'<?php } ?>',
'<?php echo \\1;?>',
'<?php echo $this->lang["\\1"];?>',
);
$content=preg_replace($search,$replace,$content);
file_put_contents($compiledFile,$content,LOCK_EX);
}
return $compiledFile;
}
/**
* 根据是否使用缓存,输出缓存文件内容
*
* @param string $tplFile
* @param string $cacheID
*/
public function cache($tplFile,$cacheID='')
{
$this->cacheID=$cacheID;
$cacheFile=$this->getCacheFileName($file,$cacheID);
if ($this->cached($file,$cacheID)) {
readfile($cacheFile);
exit;
} elseif ($this->caching) {
ob_start(array($this,'_saveCache'));
$this->cacheFile=$cacheFile;
}
}
/**
* 返回编译后的模板文件完整路径
*
* @param string $file
* @return string
*/
public function template($file='')
{
$file=$this->getTemplateFile($file);
return $this->compile($file);
}
/**
* 回调函数,供cache()函数使用
*
* @param string $output
* @return string
*/
public function _saveCache($output)
{
$cacheDir=$this->cacheDir.$this->cacheDirLevel;
is_dir($cacheDir) or mkdir($cacheDir,0777,true);
file_put_contents($this->cacheFile,$output,LOCK_EX);
return $output;
}
}
?>
怎样用PHP来给网页做导航栏
本文只需要读者具备PHP、HTML的初步知识就可以基本读懂了。译文:如大家所知PHP对于用数据库驱动的网站(making database-driven sites)来讲可谓功能强大,可是我们是否可以用它来做点其他事情呢?PHP给了我们所有我们期望的工具:for与while的循环结构、数学运算等等,还可以通过两种方式来引用文件:直接引用或向服务器提出申请。其实何止这些,让我们来看一个如何用它来做导航条的例子: 完整的原代码:
<?php
# and this "#" makes this a PHP comment.
$full_path = getenv("REQUEST_URI");
$root = dirname($full_path);
$page_file = basename($full_path);
$page_num = substr($page_file, strrpos($page_file, "_") + 1, strpos($page_file, ".html") - (strrpos($page_file, "_") + 1));
$partial_path = substr($page_file, 0, strrpos($page_file, "_"));
$prev_page_file = $partial_path . "_" . (string)($page_num-1) . ".html";
$next_page_file = $partial_path . "_" . (string)($page_num+1) . ".html";
$prev_exists = file_exists($prev_page_file);
$next_exists = file_exists($next_page_file);
if ($prev_exists) {
print "<a href=\"$root/$prev_page_file\">previous</a>";
if ($next_exists) {
print " | ";
}
if ($next_exists) {
print "<a href=\"$root/$next_page_file\">next</a>";
}
}
?>
代码分析:OK! 前面做了足够的铺垫工作,现在让我们来看看如何来用PHP来完成这项工作:
<?php
# and this "#" makes this a PHP comment.
$full_path = getenv("REQUEST_URI");
$root = dirname($full_path);
$page_file = basename($full_path);
PHP函数getenv()
用来取得环境变量的值,REQUEST_URI
的值是紧跟在主机名后的部分URL,假如URL是,那它的值就为/dinner/tuna_1.html
。现在我们将得到的那部分URL放在变量$full_path
中,再用dirname()
函数来从URL中抓取文件目录,用basename()
函数取得文件名,用上面的例子来讲dirname()
返回值:/dinner/
;basename()
返回:tuna_1.html
。接下来的部分相对有些技巧,假如我们的文件名以story_x
的格式命名,其中x代表页码,我们需要从中将我们使用的页码抽出来。当然文件名不一定只有一位数字的模式或只有一个下划线,它可以是tuna_2.html
,同样它还可以叫做tuna_234.html
甚至是candy_apple_3.html
,而我们真正想要的就是位于最后一个“_”和“.html”之间的东东。可采用如下方法:
$page_num = substr($page_file, strrpos($page_file, "_") + 1, strpos($page_file, ".html") - (strrpos($page_file, "_") + 1));
substr($string, $start,[$length])
函数给了我们字符串$string
中从$start
开始、长为$length
或到末尾的字串(方括号中的参数是可选项,如果省略$length
,substr就会返回给我们从$start
开始直到字符串末尾的字符串),正如每一个优秀的C程序员告诉你的那样,代表字符串开始的位置开始的数字是“0”而不是“1”。
函数strrpos($string, $what)
告诉我们字符串$what
在变量$string
中最后一次出现的位置,我们可以通过它找出文件名中最后一个下划线的位置在哪,同理,接着的strpos($string, $what)
告诉我们“.html”首次出现的位置。我们通过运用这三个函数取得在最后一个“”和“.html”之间的数字(代码中的strpos()
+1代表越过“”自己)。
剩下的部分很简单,首先为上页和下页构造文件名:
$partial_path = substr($page_file, 0, strrpos($page_file, "_"));
$prev_page_file = $partial_path . "_" . (string)($page_num-1) . ".html";
$next_page_file = $partial_path . "_" . (string)($page_num+1) . ".html";
(string)($page_num+1)
将数学运算$page_num+1
的结果转化为字符串类型,这样就可以用来与其他字串最终连接成为我们需要的文件名。
现在检查文件是否存在(这段代码假设所有的文件都位于同样的目录下),并最终给出构成页面导航栏的HTML代码。
把用PHP语言的网站改成响应式网站 需要做什么?很麻烦吗?
php不需要做什么,php只负责数据,改的话就该前端,推荐使用bootstrap
PHP和DZ哪个适合做导航网站?
你的问题没有搞清楚。 DZ是php的一个开源程序,主要用于论坛的。 你可以用CMS DEDE(织梦)、帝国CMS等做你想要的网站。 或者用专门的导航程序 如 phpLD(国外的,你Google一下)