Path Issues & Server Variables & URI

Example: http://192.168.147.128:888/index.php/case/candytour/category.php?id=7

  • __FILE__
    Example: C:\www\wikirex.com\case\candytour\admin\file-manager.php
  • dirname(__FILE__)
    Example: C:\www\wikirex.com\case\candytour\admin 
  • dirname(dirname(__FILE__))
    Example: C:\www\wikirex.com\case\candytour 
  • str_replace ( ‘\\’, ‘/’, dirname (__FILE__) );
    Example: C:/www/wikirex.com/case/candytour/admin 
  • $_SERVER[‘HTTP_HOST’]
    Example: 192.168.147.128:888 
  • $_SERVER[‘PHP_SELF’]
    Example: /case/candytour/admin/file-manager.php
  • $_SERVER[“REQUEST_URI”]
    /index.php/case/candytour/category.php?id=7 

    Note: If the URL is using default page like xxx.com/rex/?test=true
    The result will be: /rex/?test=true
  • $_SERVER[“PATH_INFO”]
    /case/candytour/category.php?id=7 
  • basename($_SERVER[“REQUEST_URI”])
    category.php?id=7
  • $_SERVER[‘DOCUMENT_ROOT’]
    /home/150399/domains/XXX.com.tw/html
  • dirname($_SERVER[‘PHP_SELF’])
    Example: /case/candytour/admin
  • basename(__FILE__)
    Example: file-manager.php
  • basename(__FILE__, “.php”)
    Example: file-manager
  • DIRECTORY_SEPARATOR
  • $path_parts = pathinfo(‘/www/htdocs/inc/lib.inc.php’); (since PHP 5.2.0)
    echo $path_parts[‘dirname’], “\n”; // /www/htdocs/inc
    echo $path_parts[‘basename’], “\n”; // lib.inc.php
    echo $path_parts[‘extension’], “\n”; // php
    echo $path_parts[‘filename’], “\n”; // lib.inc
  • parse_url(‘http://192.111.123.128:888/case/appmt/admin/update.php?doctor=1&action=update’)
    [scheme] => http
    [host] => 192.111.123.128
    [port] => 888
    [path] => /case/appmt/admin/update.php
    [query] => doctor=1&action=update

PHP Server variables: http://php.net/manual/en/reserved.variables.server.php

 

Ultimate Solution to get file name with query string:

PHP:

$page = get_file_name();

function get_file_name(){
  $page = $_SERVER["REQUEST_URI"];
  $p = strrpos($page, "/");
  return substr($page, $p+1);
}

HTML:

<ul>
        <li><a href="../en/<?php echo $page; ?>">English</a></li>
        <li><a href="../zhtw/<?php echo $page; ?>">繁體中文</a></li>
</ul>

Get Current Full URL:

if ( ! function_exists('current_url')){
	function current_url(){
		$prefix = 'http';
		if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'){
			$prefix = 'https';
		}

		$url = $prefix . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

		return $url;
	}
}

Append a new param to an URL:

if ( ! function_exists('append_params')){
	function append_params($url='', $params){
		if($url == false){
			$url = current_url();
		}
		//logg($url);

		$url_parts = parse_url($url);
		//logg($url_parts);

		//Convert current param string into array $current_params
		$current_params = array();
		if(isset($url_parts['query']) && $url_parts['query'] != false){
			parse_str($url_parts['query'], $current_params);
		}
		
		if($params != false){
			foreach ($params as $key => $value) {
				$current_params[$key] = $value;
			}
		}

		// Note that this will url_encode all values
		$url_parts['query'] = http_build_query($current_params);

		//Handle port number
		if(isset($url_parts['port']) && $url_parts['port'] != 80){
			$new_url = $url_parts['scheme'] . '://' . $url_parts['host'] . ':' . $url_parts['port'] . $url_parts['path'] . '?' . $url_parts['query'];
		}else{
			$new_url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'] . '?' . $url_parts['query'];
		}
		
		return $new_url;
	}
}

Encode file name of an URL:

if(!function_exists('urlencode_file_name')){
	function urlencode_file_name($url){
		$old_file_name = basename($url);
		$new_file_name = urlencode($old_file_name);
		$new_url = str_replace($old_file_name, $new_file_name, $url);
		return $new_url;
	}
}

 

One thought on “Path Issues & Server Variables & URI

  1. Hurrah, that’s what I was looking for, what a data! existing here at this webpage, thanks admin of this web site.

Comments are closed.