Linux-Noob Forums

Full Version: PHP: on the day
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

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:



Code:
<?php

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:



Code:
<?php print(selfURL()); ?>




 

Removing empty elements in a PHP array, this works like the @Trim formula in Notes/Domino.

 



Code:
<?php
/**
* 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:



Code:
<?php
$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:



Code:
<?php
$IP = gethostbyname ($SERVER_NAME);
$server = gethostbyaddr($IP);
echo "Server IP: $IP";
echo "Server Name: $server";
?>




 

If you do not want to see the IP address of your website, you can use this single line of code:



Code:
<?php
echo "Server Name: " . gethostbyaddr (gethostbyname ($SERVER_NAME));
?>




 

:P johnny06


[img]<___base_url___>/uploads/emoticons/default_dry.png[/img] initially very dodgy with <br /> and 8002, 8003 stray bits in the code...

 

BUT once cleaned up (a quick edit) it revealed itself.

 

nice johnny06 :P