php響應式網址導航z(php導航頁)

  • 1、php怎麼響應客戶端發送http請求
  • 2、linux下WebServer 怎麼實現PHP動態網頁響應!
  • 3、做個網站(php的),用許多的頁面有相同的部分,如導航條之類的,想做個模板,問下怎麼做,怎麼加載?
  • 4、怎樣用PHP來給網頁做導航欄
  • 5、把用PHP語言的網站改成響應式網站 需要做什麼?很麻煩嗎?
  • 6、PHP和DZ哪個適合做導航網站?

使用$_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可以輸出一些值到網頁中.

你直接去自己讀 http 服務器的接口相關代碼吧。

推薦個小型的 http 服務器程序

不過現在 cgi 方式是通用的,apache 的 so 方式調用貌似需要 php 這邊代碼的支持,也就是 apache 有 so 代碼接口,php 根據這個接口做的擴展式模塊。

這種方式也就兩種辦法,要麼你的 httpd 服務器自己實現 apache 的模塊接口函數,要麼就去改寫 php 的代碼,給自己的 httpd 製作一個對應的模塊功能。

當然還一個辦法,那就是乾脆集成 php 到你的服務器代碼里。不過注意你的程序協議。php 雖然不是 gpl 的,用的是他自己的 PHP lic ,類似 BSD 但貌似不是 copyleft 。

可以在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-cacheDirLevelsself::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;

}

}//end class

本文只需要讀者具備PHP、HTML的初步知識就可以基本讀懂了。 譯文:如大家所知PHP對於用數據庫驅動的網站(making database-driven sites)來講可謂功能強大,可是我們是否可以用它來做點其他事情呢?PHP給了我們所有我們期望的工具:for與while的循環結構、數學運算等等,還可以通過兩種方式來引用文件:直接引用或向服務器提出申請。其實何止這些,讓我們來看一個如何用它來做導航條的例子:完整的原代碼:!—— This “?” is how you indicate the start of a block of PHP code, —— ?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來完成這項工作: !—— This “?” is how you indicate the start of a block of PHP code, —— ?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只負責數據,改的話就該前端,推薦使用bootstrap

你的問題沒有搞清楚。

DZ是php的一個開源程序,主要用於論壇的。

你可以用CMS DEDE(織夢)、帝國CMS等做你想要的網站。

或者用專門的導航程序 如 phpLD(國外的,你Google一下)

原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126866.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若服務器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台服務器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • PHP數組去重詳解

    一、array_unique函數 array_unique是php中常用的數組去重函數,它基於值來判斷元素是否重複,具體使用方法如下: $array = array(‘a’, ‘b…

    編程 2025-04-24
  • PHP導出Excel文件

    一、PHP導出Excel文件列寬調整 當我們使用PHP導出Excel文件時,有時需要調整單元格的列寬。可以使用PHPExcel類庫中的setWidth方法來設置單元格的列寬。下面是…

    編程 2025-04-24
  • php擴展庫初探

    一、什麼是php擴展庫? PHP擴展庫(PHP extension)是一些用C語言編寫的動態鏈接庫,用於擴展PHP的功能。PHP擴展庫使得PHP可以與各種數據庫系統相連、SMTP、…

    編程 2025-04-23

發表回復

登錄後才能評論