From: Vahis on
On 2010-06-12, houghi <houghi(a)houghi.org.invalid> wrote:
> Vahis wrote:
>>>> Instead of pasting the whole code here it can be seen at
>>>> http://waxborg.servepics.com/testsite/uptime.php
>>>
> If you want to show the code instead of the result of a php page, what I
> do is add the following lines to the top:
> // Add ?code to see the code
> if (isset($_GET['code'])) { die(highlight_file(__FILE__, 1)); }
>
> http://houghi.org/ssh/index.php will give the standard page
> http://houghi.org/ssh/index.php?code will give the code

Nice, thanks :)
>
> Because now all we see is a working page and we have no idea what went
> wrong if anything.

Yeah, sorry for that.
I was getting late for work and that's when you should post _nothing_,
especially in the tubes...

> And afterwards you can comment the lines out or remove them.

You'll see the code in its whole in my reply to David.

Vahis
--
http://waxborg.servepics.com
openSUSE 11.2 (x86_64) 2.6.31.12-0.2-default
19:01pm up 3:33, 8 users, load average: 0.04, 0.04, 0.05
From: David Bolt on
On Saturday 12 Jun 2010 16:09, while playing with a tin of spray paint,
houghi painted this mural:

> David Bolt wrote:

>> For extra security,
>> you could even take that script out of the normally accessible webroot.
>> As long as it's stored in a location that php can read, and you pass
>> the path to the file, it will be found and used.
>
> Using a hidden (starting with a dot) file for that is said to be handy
> as well. Not sure if that is true.

Well, a quick test here shows it works with apache2. A demo is here:

<http://www.davjam.org/~davjam/hidden_php_test.php>

This requires the php script with the name .hidden_php_test.php , as
can be seen from the page output when viewed.


Regards,
David Bolt

--
Team Acorn: www.distributed.net
openSUSE 11.0 32b | | | openSUSE 11.3M7 32b
| openSUSE 11.1 64b | openSUSE 11.2 64b |
TOS 4.02 | openSUSE 11.1 PPC | RISC OS 4.02 | RISC OS 3.11

From: David Bolt on
On Saturday 12 Jun 2010 17:00, while playing with a tin of spray paint,
Vahis painted this mural:

> On 2010-06-12, David Bolt <blacklist-me(a)davjam.org> wrote:
>
>> My guess is that before the line:
>>
>> if ($days > 0) {
>>
>> you're missing a line:
>>
>> $uptimeString="";
>
> Great guess :)
> I added that and it seems to have removed the notice.

:-)

>>> http://waxborg.servepics.com/testsite/uptime.php
>>
>> That works here.
>
> Does it stay static or does it advance every second?

If I allow the javascript, it advances.

> I'll put the whole thing here, there's javascript in it to make it
> dynamic in case the browser supports it.
>
> And if my php skill is zero, javascript is below that...

Your javascript skill probably matches mine, and maybe even surpasses
it as I know very little javascript.

<snip>

> if ($days > 0) {
> $uptimeString .= $days;
> $uptimeString .= (($days == 1) ? " day" : " days");
> }
> if ($hours > 0) {
> $uptimeString .= (($days > 0) ? ", " : "") . $hours;
> $uptimeString .= (($hours == 1) ? " hour" : " hours");
> }
> if ($mins > 0) {
> $uptimeString .= (($days > 0 || $hours > 0) ? ", " : "") . $mins;
> $uptimeString .= (($mins == 1) ? " minute" : " minutes");
> }
> if ($secs > 0) {
> $uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? ", " :
> "") . $secs;
> $uptimeString .= (($secs == 1) ? " second" : " seconds");
> }
> return $uptimeString;
> }

I used:
$uptime="";
if($days>0)
$uptime.=$days." day".(($days>1)?"s":"");
if($hours>0)
$uptime.=(($uptime!="")?", ":"").$hours." hour".(($hours>1)?"s":"");
if($mins>0)
$uptime.=(($uptime!="")?", ":"").$mins." minute".(($mins>1)?"s":"");
if($secs>0)
$uptime.=(($uptime!="")?", ":"").$secs." second".(($secs>1)?"s":"");


> // read in the uptime (using exec)
> $uptime = exec("cat /proc/uptime");

I prefer:

$uptime=(a)file("/proc/uptime",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);

It is longer to type, but doesn't rely on any external programs. Plus,
an error is easier to detect and handle than the format you've used. In
this instance, the "file" /proc/uptime is unlikely to be missing, but
it's possible to typo another name and without passing other variables
to exec(), you won't know for certain. Using file(), if a file can't be
read, $uptime is set to FALSE and so you can check to see if there was
an error without additional variables.

> $uptime = split(" ",$uptime);

I would use explode() as split() is depreciated and will likely
disappear in a future version. Starting to use it now means no sudden
breakage when you upgrade to a new version.

> $uptimeSecs = $uptime[0];
>
> // get the static uptime
> $staticUptime = "Server Uptime: ".format_uptime($uptimeSecs);

Just as a matter of curiosity, why are you assigning $uptime[0] to a
variable when you could skip the assignment and use $uptime[0] instead?

I haven't checked these modifications, but they should work, at least
in theory.


> if (days > 0) {
> uptimeString += days;
> uptimeString += ((days == 1) ? " day" : " days");

uptimeString += days + " day" + ((days > 1) ? "s" : "" );

> }
> if (hours > 0) {
> uptimeString += ((days > 0) ? ", " : "") + hours;
> uptimeString += ((hours == 1) ? " hour" : " hours");

uptimeString += ((uptimeString != "" ? ", " : "" )) + hours + " hour" + ((hours > 1) ? "s" : "" );

> }
> if (mins > 0) {
> uptimeString += ((days > 0 || hours > 0) ? ", " : "") + mins;
> uptimeString += ((mins == 1) ? " minute" : " minutes");

uptimeString += ((uptimeString != "" ? ", " : "" )) + mins + " minute" + ((mins > 1) ? "s" : "" );

> }
> if (secs > 0) {
> uptimeString += ((days > 0 || hours > 0 || mins > 0) ? ", " : "") +
> secs;
> uptimeString += ((secs == 1) ? " second" : " seconds");

uptimeString += ((uptimeString != "" ? ", " : "" )) + secs + " second" + ((secs > 1) ? "s" : "" );

> P.S. There had been a power outage this afternoon here.
> Boy, was I surprised to see the uptime page after coming home.
> Then the wife told me that the power had been down...

That could explain the couple of times where I tried and was unable to
make a connection.


Regards,
David Bolt

--
Team Acorn: www.distributed.net
openSUSE 11.0 32b | | | openSUSE 11.3M7 32b
| openSUSE 11.1 64b | openSUSE 11.2 64b |
TOS 4.02 | openSUSE 11.1 PPC | RISC OS 4.02 | RISC OS 3.11

From: Vahis on
On 2010-06-12, David Bolt <blacklist-me(a)davjam.org> wrote:
> On Saturday 12 Jun 2010 17:00, while playing with a tin of spray paint,
> Vahis painted this mural:
>
>> On 2010-06-12, David Bolt <blacklist-me(a)davjam.org> wrote:
<snip>
> Your javascript skill probably matches mine, and maybe even surpasses
> it as I know very little javascript.

No. I can't write code at all.
This is a script that I've found here:

http://www.xenocafe.com/tutorials/php/realtime_server_uptime/index.php
<snip code>

I'll try to scrutinize your changes later today, hoping to to learn
something. Right now I need to hit the streets again :(.
>
>> P.S. There had been a power outage this afternoon here.
>> Boy, was I surprised to see the uptime page after coming home.
>> Then the wife told me that the power had been down...
>
> That could explain the couple of times where I tried and was unable to
> make a connection.

I guess it does. Outages are really rare here. This was caused by a
storm which are also very rare at this time of year :)

http://www.yle.fi/uutiset/news/2010/06/high_winds_cut_power_and_delay_ships_1755660.html

Vahis
--
http://waxborg.servepics.com
openSUSE 11.2 (x86_64) 2.6.31.12-0.2-default
06:42am up 15:14, 8 users, load average: 0.04, 0.06, 0.10
From: David Bolt on
On Sunday 13 Jun 2010 05:12, while playing with a tin of spray paint,
Vahis painted this mural:

> On 2010-06-12, David Bolt <blacklist-me(a)davjam.org> wrote:
>> On Saturday 12 Jun 2010 17:00, while playing with a tin of spray paint,
>> Vahis painted this mural:
>>
>>> On 2010-06-12, David Bolt <blacklist-me(a)davjam.org> wrote:
> <snip>
>> Your javascript skill probably matches mine, and maybe even surpasses
>> it as I know very little javascript.
>
> No. I can't write code at all.
> This is a script that I've found here:
>
> http://www.xenocafe.com/tutorials/php/realtime_server_uptime/index.php
> <snip code>

I'm surprised that in the last 5 years that no one has pointed out the
missing initial assignment for $uptimeString.

> I'll try to scrutinize your changes later today, hoping to to learn
> something. Right now I need to hit the streets again :(.

Well, when you get back and have a chance to look at them, I'll have a
go at explaining them if needs be.

>> That could explain the couple of times where I tried and was unable to
>> make a connection.
>
> I guess it does. Outages are really rare here.

They're fairly infrequent here too, although there has been the
occasional brownout which has been of sufficient duration to cause some
of my systems to restart while others haven't noticed and stayed up
throughout.

> This was caused by a
> storm which are also very rare at this time of year :)

Blame Iceland for that. It's absolutely disgusting them letting their
volcanoes go off willy-nilly. They should have grounded it for at least
a week, and told it it was going to go without pocket money for a
couple of weeks as well. That would have at least made it think about
changing its behaviour. :-)

> http://www.yle.fi/uutiset/news/2010/06/high_winds_cut_power_and_delay_ships_1755660.html

I hope you had no problems apart from the short power disruption.


Regards,
David Bolt

--
Team Acorn: www.distributed.net
openSUSE 11.0 32b | | | openSUSE 11.3M7 32b
| openSUSE 11.1 64b | openSUSE 11.2 64b |
TOS 4.02 | openSUSE 11.1 PPC | RISC OS 4.02 | RISC OS 3.11