PHP Uptime Function


Written by

There is an updated version of this function.

I have been wanting to display the uptime of my Web Server so I wrote a little function.

It only supports Linux/Unix type systems but it checks for that and if its a Windows or Unknown system, it just outputs “Unknown”.

function uptime(){
 // See if it is a Unix Based OS
 if((PHP_OS == "Linux") || (PHP_OS == "FreeBSD") || (PHP_OS == "Unix") || (PHP_OS == "SunOS")){
  $uptime = file_get_contents( "/proc/uptime");
  $uptime = explode(" ",$uptime);
   $uptime_ = $uptime[0];
  $seconds = $uptime;
 
  $days = (($uptime % 31556926) / 86400);
  $days = explode(".",$days);
   $days = $days[0];
  $hours = ((($uptime % 31556926) % 86400) / 3600);
  $hours = explode(".",$hours);
   $hours = $hours[0];
  $minutes = (((($uptime % 31556926) % 86400) % 3600) / 60);
  $minutes = explode(".",$minutes);
   $minutes = $minutes[0];
  if($minutes > 0){
   $time = $minutes." mins.";
  }
  if($hours > 0){
   $time = $hours." hours, ".$time;
  }
  if($days > 0){
   $time = $days." days, ".$time;
  }
 
 }else{
  // Function Doesn't Support Windows
  $time = "Unknwown";
 }
return $time;
}

Hope someone else gets as much use of it as me.

Facebook & HTTPS


Written by

What is HTTPS?

HTTPS is the HTTP protocol that is secured, this means that all data sent via your web browser is encrypted and cant be read by some dodgy bugger somewhere trying to steal your passwords and sensitive information.

What on earth is Facebook?

Do you really have to ask?

So what are you going on about?

Well, if you visit https://www.facebook.com you can login securely but as soon as you click a link, it then takes you to the standard HTTP format.

So, if you think you’re being all secure and keeping your information all secure then you’ve thought wrong.

So why is this happening?

Well its all down to how you define a link in HTML. You have the a relative method:

<a href="webdesign/">

then you have the absolute method:

<a href="/webdesign">

now, both of these links take you to the same place but id different ways, if you moved the page that the first link is place in to another directory, it will break but the second will.

Now Facebook is using a third method usually used to link to external sites

<a href="http://www.aboutcher.co.uk">

this loads up an external address with the HTTP protocol, this method is used to tell your browser that the link is outside your domain.

So in short, Facebook is defining its links as though it is linking to an external site and not its own. This maybe because it runs several sub-domains for things such as groups and relative links would the not work but this is no excuse. Facebook runs on PHP and there is a function to check to see if the connection is secure or not.

All that is needed is the following code:

if($_SERVER['HTTPS']=="on"){
$PROTO = "HTTPS://";
}else{
$PROTO = "HTTP://";
}
echo $PROTO."www.aboutcher.co.uk";

This will create the link HTTP or HTTPS depending on what you are already using, and so keeping the connection secure on every click.

NS2 on Ubnutu


Written by

I had a lot of trouble installing Network Simulator 2 (NS2) onto my Virtual Ubnutu 9.04 Linux install. After much work, I have created a short set of terminal scripts that will allow NS to be compiled and installed.

apt-get install -y build-essential autoconf automake libx11-dev libxmu-dev libxmu-headers libxt-dev libtool g++-4.3
cd /
wget http://downloads.sourceforge.net/project/nsnam/allinone/ns-allinone-2.34/ns-allinone-2.34.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fnsnam%2F&ts=1295461023&use_mirror=sunet
tar -zxvf ns-allinone-2.34.tar.gz
wget http://www.aboutcher.co.uk/files/configure
cp configure ns-allinone-2.34/otcl-1.13/
mv ns-allinone-2.34/ ns/
cd ns
./install
export LD_LIBRARY_PATH=/ns/otcl-1.13/
export LD_LIBRARY_PATH=/ns/lib/
export TCL_LIBRARY=/ns/tcl8.4.18/library/
cd /usr/bin
ln /ns/ns-2.34/ns ns
ln /ns/nam-1.14/nam nam
cd ~
ln /ns/ns-2.34/tcl/ex/ NS\ TCL\ Examples
apt-get install -y xgraph

Just copy and paste the above code into a root terminal.
to get a root terminal type

sudo su

Hope this works for you too. This should work for newer versions of Ubuntu too.

New year, New site


Written by

Well, after much deliberation I have change my site once again.

My past website layout was nice and clean but a little too white for my likings and it wasn’t easy enough to update. I have thus opted for a more blog style site.

This should hopefully get me to update it more, I have chosen to turn comments off just to save me time moderating them for spam n stuff.

This change was not taken lightly as my previous site was completely hand written by myself and conformed to standards, which this new site currently does not; this will be rectified when I have time.

I hope you like the new style and find it just as easy to use as the previous.

Goodnight

PHP Log Function


Written by

Since developing several sites, I have found that the default error/notice output can be vague or not flag the error up where it is.
This is why I have developed a PHP function which logs what ever you want it to.

function logger($type,$action,$msg){
 // Three types of logging, debug(anything not User/Security/), user and Security (Sec)
 $ip = $_SERVER['REMOTE_ADDR'];
 $bool=TRUE;
 if(strtolower($type)=="user"){
 $buffer = "\r\n".date("d-m-y(H:i:s)")."; ".$action."; ".$msg."; ".$ip;
 }elseif(strtolower($type)=="sec"){
 $buffer = "\r\n".date("d-m-y(H:i:s)")."; ".$action."; ".$msg."; ".$ip;
 $type = "security";
 }else{
 $buffer = "\r\n".date("d-m-y(H:i:s)")."; ".$type."; ".$action."; ".$msg."; ".$ip;
 $type = "debug";
 }
 $logfile = "logs/".strtolower($type).".log";
 $handle = fopen($logfile, "a") or $bool = FALSE;
 if($bool!=FALSE)
 {
 $write = fwrite($handle, $buffer);
 if(!$write){
 $bool = FALSE;
 }
 fclose($handle);
 $bool = TRUE;
 }
return $bool;
}

Just call it up by doing

logger("MYSQL Error","MySQL_Connect","MySQL Error".mysql_error());

which for example will produce

01-01-11(22:31:27); MySQL Error; MySQL_Connect; Database Returned Error - You have an error in your SQL syntax; at line 1; 95.145.206.220

As you can see the logs are delimited with a semi-colon (;) which makes it easy to import that data into a spreadsheet.
The logs are saved in logs/ of the page that executes the function, but you can change this if required; there are 3 logs, ‘user‘,’security‘ and ‘debug‘.

The debug log catches any $type that is not ‘user‘ or ‘sec‘.

And finally it returns TRUE on log and FALSE on error.

Hope this code helps you as much as it’s helped me.