PHP Scripting

Posted by Ben Harris

How to show PHP source. Sometimes you may want to show your lovely PHP script source to the world (like in my above entry). To do this, you get your script, and rename it from .php/.php3/.php4/.php5 etc.. to .phps Then, In the above examples (and the one below as a demo) I am just using an iFrame

Code:

The iFrame code I used is as follows:

<iframe frameborder=”0″ width=”100%” height=”120″ scrolling=”No” src=”http://harrisuk.org/demo/fedora-demo.phps” mce_src=”../demo/fedora-demo.phps”>You can add something here for if the users browser doesnt support iframes! (this wont be displayed unless they cant) </iframe>

Feel free to take a look at the source for my mini-script for showing the servers OS here!

This is a secure way of showing syntax hilighted source, there are some other php functions such as highlight_file() which can also do the same thing. Using the syntax highlight_file($filename, true).

Regards

Ben Harris

PHP Scripting

Posted by Ben Harris

Recently I started using a script that required open_basedir similar to Server Status 2k script on one of my websites. Due to the restrictions of open_basedir I was unable to run it properly, so I set out to achieve a method of using the script withouth using open_basedir. Open_basedir is a restriction that stops users ‘getting out’ of their own files (e.g. When a script tries to open a file with, for example, fopen() or gzopen(), the location of the file is checked. When the file is outside the specified directory-tree, PHP will refuse to open it).

The method I used for the script was of a few parts. The script previously required access to /proc/cpuinfo and /proc/meminfo and aditionally, access to the OS release/version file (found at /etc/fedora-release in my case)

Firstly, to get around the requirement of open_basedir for the Operating System version/release file, we simply use the following lines of PHP

Code:

The output of the above in my case is: Fedora Core release 6 (Zod)
That is the same output that would be produced by accessing /etc/fedora-release. Depending on your operating system, your release/version file will be in a diferent place.

The following are a list of the major OSes file locations:
/etc/debian_release
/etc/debian_version
/etc/SuSE-release
/etc/UnitedLinux-release
/etc/mandrake-release
/etc/gentoo-release
/etc/redhat_version
/etc/redhat-release
/etc/fedora-release
/etc/slackware-release
/etc/slackware-version
/etc/trustix-release
/etc/trustix-version
/etc/eos-version
/etc/gentoo-release
/etc/cobalt-release
/etc/lfs-release
/etc/rubix-version

Secondly to get around the requirement of open_basedir for /proc/cpuinfo. (there are two ways)

Firstly,
we simply use the following lines of PHP. They can show as much or as little information as you want, I have just chosen the main info.

code:

This outputs the following:
vendor_id : GenuineIntel

model name : Intel(R) Core(TM)2 CPU T5500 @ 1.66GHz

cpu cores : 2

cpu MHz : 1000.000

cache size : 2048 KB

bogomips : 3319.95

A limitation of this is that it only shows the info on the first CPU. Can be a problem if you have more than one CPU.

My next method is better if your CPU’s never change (e.g. by changing server). If you have SSH access, run the following command

cat /proc/cpuinfo

and save the contence into a file named cpuinfo then you can use the file() command on it. So say, before you find file(/proc/cpuinfo) you could then change it to file(/home/user/public_html/cpuinfo) to get the same outcome. If you dont have SSH access, ask your host to run the above command and send you the output.

Finally, we get onto the /proc/meminfo bit. This one is a little harder than the others especially if you only want the figure (for example RAM used, which changes constantly).

code:

The script then outputs the following (however in real usage, you would be using the number not showing it in most cases)
2066096
95036
4192956
4192908
182628
0

Ok, The above code looks a little daunting. Lets work through it bit by bit.
1. $total_mem_full = exec(’cat /proc/meminfo | grep “MemTotal”‘);
2. $total_mem_min = split(’:', $total_mem_full);
3. $total_mem = substr($total_mem_min[1], 0, -3);

The first line gets the line that shows the Total memory (RAM) available at the time of script execution. If you echoed that. It would show the following: MemTotal: 2066096 kB. Ok, thats alright, but we cant use the number (for example for stats).

So next, line 2 says split the String $total_mem_full where there is a colon (:), then put it into an array.

Finally, line 3 says take $total_mem_min[1] (thats the bit after the colon, and looks like 2066096 kB), and then take the last 3 characters from the right hand side. This takes away the ‘ kB’ (thats a space, and kB). We are now left with 2066096 which we can generate stats and stuff from :D

I hope this was handy to someone!

Regards

Ben Harris

Programming / Scripting

Posted by Ben Harris

I currently Program in Borland Delphi 7, and a combination of PHP / MySQL. Here I will post some cool stuff that I have done and worked out how to do, Might even include some nice little scripts / applications.

Thanks for reading,

Ben Harris

Secured Loans

Compare secured loans to find the perfect deal for you!

www.accepted.co.uk

Matched.co.uk
 
PROTECT YOUR FAMILY GET QUOTE
Compare Online
Mortgage
Insurance Quotes
Ski Insurance