- function zen_href_link( $page = '' , $parameters = '' , $connection = 'NONSSL' , $add_session_id = true, $search_engine_safe = true, $static = false, $use_dir_ws_catalog = true) {
- global $request_type , $session_started , $http_domain , $https_domain ;
- if (!zen_not_null( $page )) {
- die ( '</td></tr></table></td></tr></table><br /><br /><b class="note">Error!<br /><br />Unable to determine the page link!</b><br /><br /><!--' . $page . '<br />' . $parameters . ' -->' );
- }
- if ( $connection == 'NONSSL' ) {
- $link = HTTP_SERVER;
- } elseif ( $connection == 'SSL' ) {
- if (ENABLE_SSL == 'true' ) {
- $link = HTTPS_SERVER ;
- } else {
- $link = HTTP_SERVER;
- }
- } else {
- die ( '</td></tr></table></td></tr></table><br /><br /><b class="note">Error!<br /><br />Unable to determine connection method on a link!<br /><br />Known methods: NONSSL SSL</b><br /><br />' );
- }
- if ( $use_dir_ws_catalog ) {
- if ( $connection == 'SSL' && ENABLE_SSL == 'true' ) {
- $link .= DIR_WS_HTTPS_CATALOG;
- } else {
- $link .= DIR_WS_CATALOG;
- }
- }
- if (! $static ) {
- if (zen_not_null( $parameters )) {
- $link .= 'index.php?main_page=' . $page . "&" . zen_output_string( $parameters );
- } else {
- $link .= 'index.php?main_page=' . $page ;
- }
- } else {
- if (zen_not_null( $parameters )) {
- $link .= $page . "?" . zen_output_string( $parameters );
- } else {
- $link .= $page ;
- }
- }
- $separator = '&' ;
- while ( ( substr ( $link , -1) == '&' ) || ( substr ( $link , -1) == '?' ) ) $link = substr ( $link , 0, -1);
- // Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
- if ( ( $add_session_id == true) && ( $session_started == true) && (SESSION_FORCE_COOKIE_USE == 'False' ) ) {
- if (defined( 'SID' ) && zen_not_null(SID)) {
- $sid = SID;
- // } elseif ( ( ($request_type == 'NONSSL') && ($connection == 'SSL') && (ENABLE_SSL_ADMIN == 'true') ) || ( ($request_type == 'SSL') && ($connection == 'NONSSL') ) ) {
- } elseif ( ( ( $request_type == 'NONSSL' ) && ( $connection == 'SSL' ) && (ENABLE_SSL == 'true' ) ) || ( ( $request_type == 'SSL' ) && ( $connection == 'NONSSL' ) ) ) {
- if ( $http_domain != $https_domain ) {
- $sid = zen_session_name() . '=' . zen_session_id();
- }
- }
- }
- // clean up the link before processing
- while ( strstr ( $link , '&&' )) $link = str_replace ( '&&' , '&' , $link );
- while ( strstr ( $link , '&&' )) $link = str_replace ( '&&' , '&' , $link );
- if ( (SEARCH_ENGINE_FRIENDLY_URLS == 'true' ) && ( $search_engine_safe == true) ) {
- while ( strstr ( $link , '&&' )) $link = str_replace ( '&&' , '&' , $link );
- $link = str_replace ( '&' , '/' , $link );
- $link = str_replace ( '?' , '/' , $link );
- $link = str_replace ( '&' , '/' , $link );
- $link = str_replace ( '=' , '/' , $link );
- $separator = '?' ;
- }
- if (isset( $sid )) {
- $link .= $separator . zen_output_string( $sid );
- }
- // clean up the link after processing
- while ( strstr ( $link , '&&' )) $link = str_replace ( '&&' , '&' , $link );
- $link = preg_replace( '/&/' , '&' , $link );
- return $link ;
- }
zen_href_link 函数位于 /includes/functions/html_output.php 文件中,主要用于输出超级链接。它的参数如下表所示:
$page | 这个变量通常由 /includes/filename.php 文件中的常量取得 | |
$parameters | ||
$connection | ||
$add_session_id | ||
$search_engine_safe | ||
$static | ||
$use_dir_ws_catalog |
第一行代码,定义了 global 变量:$request_type,$session_started,$http_domain 和 $https_domain。(global 的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括 include 或 require 的所有文件。在函数体内定义的 global 变量,函数体外可以使用,在函数体外定义的 global 变量不能在函数体内使用。)
接下来的代码,
- if (!zen_not_null( $page ))
判断传递过来的 $page 变量是否为空。zen_not_null 函数,看名字猜,应该是判断它的参数不为空。前面加了一个逻辑非符号 !,这个条件语句的结果应该是,如果 $page 为空,则执行以下代码:
- die ( '</td></tr></table></td></tr></table><br /><br /><b class="note">Error!<br /><br />Unable to determine the page link!</b><br /><br /><!--' . $page . '<br />' . $parameters . ' -->' );
输出 die 后面括号的内容,然后中止程序执行。输出内容的后半部分容易理解,就是发出一条无法判断页面链接的警告。前半部分,只有关闭的 html 标签,table 的前面部分标签,还不知道是在哪里,有待研究。
然后,根据 $connection 参数,来决定是 http:// 链接,还是 https:// 链接。HTTP_SERVER 这个常量可以在 /includes/configure.php 文件中找到,实际就是网站的根目录。然后把 HTTP_SERVER 这个常量值赋值给 $link 这个变量。
如果 $connection 传递过来的,既不是 NONSSL,也不是 SSL,则会输出错误信息,然后中止执行程序。
判断参数 $use_dir_ws_catalog 是否为真,如果不是,则不执行任何代码,如果为真,执行以下代码:
- if ( $connection == 'SSL' && ENABLE_SSL == 'true' ) {
- $link .= DIR_WS_HTTPS_CATALOG;
- } else {
- $link .= DIR_WS_CATALOG;
- }
由于 $use_dir_ws_catalog 默认为真,所以将执行以上代码。这个是一个条件语句,如果 $connection 的值为 SSL,并且 ENABLE_SSL 这个常量为真,则把 DIR_WS_HTTPS_CATALOG 这个常量赋值给 $link;否则将把 DIR_WS_CATALOG 这个常量赋值给 $link。ENABLE_SSL,DIR_WS_HTTPS_CATALOG 和 DIR_WS_CATALOG 这三个常量都可以 /includes/configure.php 文件中找到。
下面看一个实例:
- zen_href_link(FILENAME_LOGIN, '' , 'SSL' );
以上代码,将产生以下的超级链接:
- http: //zencart/index.php?main_page=login&zenid=5n4b36fqt06jj9pciqtd1v3fs0
FILENAME_LOGIN 这个常量在 /includes/filenames.php 文件中被定义。
- define( 'FILENAME_LOGIN' , 'login' );
接下来将判断 $static 这个变量的真假,如果不为真,则执行以下代码:
- if (! $static ) {
- if (zen_not_null( $parameters )) {
- $link .= 'index.php?main_page=' . $page . "&" . zen_output_string( $parameters );
- } else {
- $link .= 'index.php?main_page=' . $page ;
- }
- } else {
- if (zen_not_null( $parameters )) {
- $link .= $page . "?" . zen_output_string( $parameters );
- } else {
- $link .= $page ;
- }
- }