PDA

View Full Version : small hack if possible but not sure if can be done


Mr_P
05-07-2002, 04:34 PM
i run an ftp server and i wanted to know if is is possible to view on main pafe if server is up or down without having to call it in using iframe as you can view source and get ftp details.

Basically just want an icon to represent if on/off but i cant suzz it out so any help would be appreciated

ftp only holds maps etc for rtcw so aint used for piracy but its a boards ftp so would be neat if everyone can see if it on/off at a glance.

Issvar
05-08-2002, 02:30 AM
Add this where you want to show the icon:

It has a 5 second timeout for the connection, to make sure users don't have to wait too long for connection. Edit settings in ftp_connecct as required (host,port,timeout).

<?php
$ftp_conn = ftp_connect('ftp.mysite.com',21,5);
if ($ftp_conn) { echo '<img src="ftponline.gif">'; ftp_close($ftp_conn); }
else { echo '<img src="ftpoffline.gif">'; }
?>

Mr_P
05-08-2002, 07:23 AM
tried to input this code into a few different locations but having no luck

any ideas on where is best place to put it

also thanks for your help

Mr_P
05-08-2002, 07:43 AM
Warning: Wrong parameter count for ftp_connect() in \vbb\showthread.php on line 912

Issvar
05-08-2002, 01:07 PM
Hmm, looks like ftp functions require to be compiled into php which is not standard, and there are some errors with it on windows. Instead try:

<?php
$ftp_conn=fsockopen('ftp.mysite.com',21,$errno,$er rstr,5);
if ($ftp_conn) { echo 'Online'; fputs($ftp_conn,'BYE'); }
else { echo 'Offline'; }
?>

If connection fails you can use $errno and $errstr to view errormessages , if you know enough php to display them, but you don't have to use them.

Mr_P
05-08-2002, 01:35 PM
ok tried the above and nothing shows - i put the code in index.php

i used the code from above this morning and it worked on my local copy but when i went on web to test it thats when i kept getting the errors.
Basically im having probs knowing where is best place to put the code as php is not something i am good at.

Also on a final note do these commands have to be supported by your host and if so do that mean i will either have to contact my web provider and see if they can switch them on

Or am i flogging a dead horse - i know its a lot of hazzle just to see an on/offline gif but to me its deffo worth it so all help is appreciated.

Sparkz
05-08-2002, 01:52 PM
Personally, I would do this somewhere outside vBulletin (or whatever public page you were going to use it in) and put it in a cronjob on the server. That way everytime someone loads the page, they wouldn't steal a connection to the FTP-server.

Imagine running something like Issvar's script on a high traffic site, like this. Every pageload would generate 1 connection to the FTP-server. It would probably slow the server down, as well as forcing you to increase the max number of connections...

Mr_P
05-08-2002, 02:23 PM
ok Sparkz you raise some good points there that i was unaware of so how about if i go about it from a another angle.

A link on my main page to a page that shows if server is on/off
etc

so how would i incorporate this new code into that page ?
remembering that the new page would`nt have anything to do with vbull so would be a totally new page

Sparkz
05-08-2002, 02:45 PM
If you have access to crontab on your server, you can make Issvar's script run every x minutes, and write it's output to a file instead. Something along the lines of this:

<?php
$offbutton = "/images/offbutton.gif";
$onbutton = "/images/onbutton.gif";
$outfile = "/path/to/file.txt";
$fp = fopen ($outfile, 'w');

$ftp_conn=fsockopen('ftp.mysite.com',21,$errno,$er rstr,5);
if ($ftp_conn) {
fputs ($fp, $onbutton);
fputs ($ftp_conn,'BYE');
} else {
fputs ($fp, $offbutton);
}

fclose ($fp);
?>


Please not that the file referenced in $outfile has to be writable by the webserver (if calling this script with lynx or wget, etc)

In your crontab, you would place something like this:
1-59 * * * * /path/to/lynx --source http://your.server.here/updatescript.php

Then, in the page where you want to display this add something like this:

$myfile = "/path/to/file.txt";
$fp = fopen ($myfile, 'r');
$ftpbutton = fread ($fp, 4096);
fclose ($fp);


If I am not mistaken now, you should be able to use $ftpbutton in some template on that page. If it is a path to an image, like I did in my suggestion here, you would put something like <img src="$ftpbutton"> in your template.

This is yet another untested thingy from me, but in theory (hey, all my stuff works in theory, right ;)), it should work.

Mr_P
05-08-2002, 03:26 PM
lol that certainly is good help and more than i hoped for - but i dont have access to crontab so that counts that idea out.
But like you say all your ideas work and shame really as i would ov tested it as well.