Getting the current full URL in PHP
Sometimes, you might want to get the current full URL in PHP. Here is how you do that. Add the following code to a page:
function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on" ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/".$s;
$port = ($_SERVER["SERVER_PORT"] == "80″) ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}
You can now get the full URL using the line:
Removing empty elements in a PHP array
This works like the @Trim formula in Notes/Domino
/**
* Trims an array from empty elements. *
* @param $a the array to trim.
* @return a new array with the empty elements removed. */
function array_trim($a) {
$j = 0;
for ($i = 0;
$i <
count($a);
$i++) { if
($a[$i] != "" { $b[$j++] = $a[$i];
} } return $b;
}
Use it like this:
$a[0]="";
$a[1]="An entry";
$a[2]="Another one";
$a[3]="";
$b=array_trim($a);
The resulting $b
array will have two entries, with the two empty ones removed.
How to figure out your server's real name
It is sometimes necessary to figure out the real server name. There are several ways to do this, but in PHP, a simple way is this:
$IP = gethostbyname ($SERVER_NAME);
$server = gethostbyaddr($IP);
echo "<br />Server IP: $IP";
echo "<br />Server Name: $server";
If you do not want to see the IP address of your website, you can use this single line of code:
echo "Server Name: " . gethostbyaddr (gethostbyname ($SERVER_NAME));