Need a script idea

Teren

One of Freddy's beloved
Joined
Dec 22, 2003
Messages
585
Hi
Can anyone help me to create a script/proggie/idea on how to do something?

The thing is:
I have my own box with Apache, PHP and MySQL... I set up my portal on it. But there is athing I don't like.
As the portal has no domain name, I use http://123.45.67.89 as the addres (won't give the real IP :p). My idea is making a .tk redirection (they give out free www.yourname.tk redirects), but I don't want it to directly to my box. I have a 50mb free host. I want the .tk to go there. But that ain't the prob. I want my page on the free domain to redirect to my box if it's on (it ain't on 24/7), or say something like "The portal is unavailible atm, try again later".
Can someone help me to solve the redirect thing?

p.s afair, the free domain supports php (but no DB's)

Thanks in advance,

Teren
 

wyrd_fish

Fledgling Freddie
Joined
Dec 27, 2003
Messages
537
try this

Code:
$host = ""; // your server
$un = ""; // your username
$pw = ""; // your password
$db = ""; // the name of your DB
$connection = mysql_connect($host, $un, $pw) or die("Host Offline");
header("Location: [url="http://your.server"]http://your.server[/url]");

there's another connecion funcion that i can't remember, you can use that one to just ping the server, rather than a full connection
 

Whipped

Part of the furniture
Joined
Dec 22, 2003
Messages
2,155
If it supports php then just use this script. Call it index.php so that it is run as soon as someone hits the free web host.

Code:
<?  header("Location: http://123.45.67.89/"); ?>
 

wyrd_fish

Fledgling Freddie
Joined
Dec 27, 2003
Messages
537
thats all well nad good but he wants a message saying offline, not a 404
 

Teren

One of Freddy's beloved
Joined
Dec 22, 2003
Messages
585
Heh, thanks for the ideas, but atm I think I'm gonna use my own "idea": making 2 diff index.html (one that redirects to the portal, and one saying offline) and making a script for rc5.d and rc0(6).d that will rename the indexes on the free host accordingly ;)
 

Whipped

Part of the furniture
Joined
Dec 22, 2003
Messages
2,155
He's meaning that when the server goes down it will connect to his free host. Rename index.htm to index.redir and rename index.off to index.htm thereby automating the redirect.

At least that seems like what he's meaning.

Although I did just think of this, btu it may not work.

Code:
<?
function ping($host) 
{ 
  $ip = gethostbyname($host); 
  $ping = `ping -c 1 -q $ip`; 
  $res = '1 packets transmitted, 1 packets recieved'; 
  if(strpos($ping, $res)) 
    return true; 
  else 
    return false; 
}

$redirect = 'http://123.456.789.123';

if( ping($redirect )
  header("Location: $redirect");
else
  echo "<center><h1>Server is down at the moment. Please try again later</h1></center>"
?>
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
that will fail as you're going to ping http://an.ip.num.ber and not the number iirc. you'll have to strip "http://" off $redirect. I'm p00 at php though, so I may be wrong.
 

EvilMonkeh

Fledgling Freddie
Joined
Dec 22, 2003
Messages
120
whipped
should your $ping = bit be
Code:
$ping = system("ping -c 1 -q $ip");
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
No, he's used backticks, they are effectively system(). But yes, you would need to strip off the http:// iirc .. or just not put it on in the first place:

Code:
<?
function ping($host) 
{ 
  $ip = gethostbyname($host); 
  $ping = `ping -c 1 -q $ip`; 
  $res = '1 packets transmitted, 1 packets recieved'; 
  if(strpos($ping, $res)) 
    return true; 
  else 
    return false; 
}

$redirect = '123.456.789.123';

if( ping($redirect )
  header("Location: http://$redirect");
else
  echo "<center><h1>Server is down at the moment. Please try again later</h1></center>"
?>

btw, chances of a host allowing system() or backticks is quite small surely?
 

Teren

One of Freddy's beloved
Joined
Dec 22, 2003
Messages
585
Thanks for the script, Whipped and SheepCow!

But... there is a problem, I'm using iptables to block outside pings... but I'm sure there is a way to allow my free host to ping me.

btw, chances of a host allowing system() or backticks is quite small surely?

Hmm... I'll ask.



Ohh, btw:
He's meaning that when the server goes down it will connect to his free host. Rename index.htm to index.redir and rename index.off to index.htm thereby automating the redirect.

Yes, you are completely right ;)

Teren
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
You could try fsockopen()'ing a connection on port 80 to the server, if it works then redirect or something
 

Whipped

Part of the furniture
Joined
Dec 22, 2003
Messages
2,155
Just thought of another way to do this.

Put your phone number on the page and ask people to phone you to see if the server is up before clicking the link ;)
 

wyrd_fish

Fledgling Freddie
Joined
Dec 27, 2003
Messages
537
He's meaning that when the server goes down it will connect to his free host. Rename index.htm to index.redir and rename index.off to index.htm thereby automating the redirect.
what happens ifthere's a powercit, it crashes or the connection goes down?
 

Whipped

Part of the furniture
Joined
Dec 22, 2003
Messages
2,155
I don't think this is going to be that mission critial ;)
 

EvilMonkeh

Fledgling Freddie
Joined
Dec 22, 2003
Messages
120
ah ha
better idea
put an image, maybe "goat.jpg" on your computer
then on the page use:
PHP:
<?php
if (fopen("http://23.235.32.424/goat.jpg"))
{
header("location: http://lah");
}
else
{
echo "moo not open";
}
?>
 

phlash

Fledgling Freddie
Joined
Dec 24, 2003
Messages
195
Yet Another Thought (YAT?):

Do you need to cope with dynamic IPs? If so then writing the redirection script (or part of it - like a config file!) from the dynamic host itself every time it comes up (or indeed regularly by hooking the DHCP refresh scripts) should help since it can put it's present IP address in somewhere...
 

Teren

One of Freddy's beloved
Joined
Dec 22, 2003
Messages
585
Thanks for all the help!
I combined all the info I got from you and made this:
Code:
<?
if (fopen("<PATH TO SOME FILE ON THE SERVER>","r"))
{
header("location: http://serverpath/index.php");
}
else
{
echo "<center><h1>Sorry, the server is offline, try again later!</h1></center>";
}
?>

And also added this: "php_flag display_errors off" to the .htaccess file on the free host (it's allowed) so it won't show the fopen() file not found error ;)

Teren
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
Have you tested this? How long does it take to timeout with the connection before saying server is dead?

You could but an @ infront of fopen to suppress errors
 

Users who are viewing this thread

Top Bottom