Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Zeke Sikelianos

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

PHP for removing www from request and redirecting. (Useful for System.security.allowDomain issues)

<?
	// if www.domain.com, redirect to domain.com
	if (strtolower(substr($_SERVER['HTTP_HOST'], 0, 3)) == "www") {
		header("Location: http://rgcreative.com" . $_SERVER['REQUEST_URI']);
	}
	
	// Full path to current URL (including query string)
	//echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>

PHP: Custom substring replacement

// Replace a substring of which you only know the beginning and end
function replaceIndefiniteSubstring ($start_needle, $end_needle, $replacement, $target) {
	$start_pos = strpos($target, $start_needle);
	$end_pos = strpos($target, $end_needle) + strlen($end_needle);
	return substr_replace($target, $replacement, $start_pos, ($end_pos-$start_pos));
}

PHP: Get the name of the directory which contains the current script

function getCurrentDirectory() {
	$path = dirname($_SERVER['PHP_SELF']);
	$position = strrpos($path,'/') + 1;
	return substr($path,$position);
}

Reconstruct URL string in PHP

  // find out the domain:
  $domain = $_SERVER['HTTP_HOST'];
  // find out the path to the current file:
  $path = $_SERVER['SCRIPT_NAME'];
  // find out the QueryString:
  $queryString = $_SERVER['QUERY_STRING'];
  // put it all together:
  $url = "http://" . $domain . $path . "?" . $queryString;
  echo "The current URL is: " . $url . "<br />";
  
  // An alternative way is to use REQUEST_URI instead of both
  // SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
  $url2 = "http://" . $domain . $_SERVER['REQUEST_URI'];
  echo "The alternative way: " . $url2;
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS