PHP: on the day
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:
print(selfURL());
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));
Tags: snipets
1 Comment + Add Comment
Got anything to say? Go ahead and leave a comment!
Tags
adsense
anime
Arch Linux
blogging
bê bối
bí ẩn
bản phân phối Linux
bảo mật
bộ gõ tiếng việt
chicken soup
cảm xúc
cập nhật
dịch vụ trực tuyến
featured
firefox
giáo dục
giải trí
hackintosh
hệ điều hành
lịch sử
máy chủ
nghề nghiệp
ngôn ngữ lập trình
người đẹp
ngẫu nhiên
phần cứng
phần mềm
phần mềm Ubuntu
quan điểm
quảng cáo
sức khỏe
thể thao
thống kê
thủ thuật
tình yêu
virus
viết blog
võ lâm truyền kỳ
văn hóa
văn học
wordpress posts
xã hội
âm nhạc
điện ảnh
động vật




Posted under:
Nói chung Vãi đạn ông :d