Jump to content
Sign in to follow this  
jsn06

PHP: on the day

Recommended Posts

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:

<?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:

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

 

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

 

<?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:

<?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:

<?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:

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

 

:P johnny06

Share this post


Link to post
Share on other sites

<_< 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

Share this post


Link to post
Share on other sites
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...