Forum Moderators: coopster

Message Too Old, No Replies

Php Redirect page

IIS 7 Server on my PC

         

greeny

8:45 pm on Dec 21, 2009 (gmt 0)

10+ Year Member



Hi i want to have PHP scripting page if SERVER A Goes down this PHP Page check the status & than Redirect user to SERVER B....

thing is that i made IIS 7 Server on my PC & some time i turn off my PC ill upload this PHP page on free hosting

i want this php page should work like this if some one open my website this php page should check if my server is online than it should open the page from my website if not than it should redirect the user the to this Page "Sorry The Server is Down"

i m using dot.tk Domain My website URL is <snip>

my website loads it from my Computer. So please help me & tell me how i can make it possible?

I have Script but its not working i dont know why i m gonna show it to you here

<?php
//Web Server Status v 1.4, Copyright 2002 By Ryan Schwiebert
//This script may be freely distributed providing all copyright headers are kept intact.

//Concept from:
//Abax Server Status v1.04, Copyright 2002 By Nathan Dickman
//Location of the live or dead server images

//Please change to your server specifications
$live = "ftp://greeny.example.com:13000/live.gif";
$dead = "http://example2.com/albums/aa149/greeeeny/dead.gif";

//The status checking script
//meddle at your own risk!
//check for port number, default is 80
$link = $_GET['link'].":";
$s_link = str_replace("::", ":", $link);
list($addr,$port)= explode (':',"$s_link");
if (empty($port)){
$port = 80;
}
//Test the server connection
$churl = @fsockopen(server($addr), $port, $errno, $errstr, 20);
if (!$churl){
//echo $errstr;
header("Location: $dead");
}
else {
header("Location: $live");
}
function server($addr){
if(strstr($addr,"/")){$addr = substr($addr, 0, strpos($addr, "/"));}
return $addr;
}
?>

-------------------------------------------------------------------------

$live = "ftp://greeny.example.com:13000/live.gif"; <<<< this picture loads from my PC

$dead = "http://example2.com/albums/aa149/greeeeny/dead.gif"; <<< its free image hosting website

problem is when i stop My IIS 7 & check it it does not show Dead.gif on page

now i want you guys to give me solution if it cannot loads image i mean if my server is stopped so it should show this page "The Server is Down" text on that page" so please me out ....

thanks in advance =)

[edited by: eelixduppy at 6:58 am (utc) on Dec. 22, 2009]
[edit reason] removed specifics [/edit]

Readie

12:26 am on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The PhP scripting runs on the server, so if your domain is directing users to your PC, and your PC is off, then even if you have a PhP redirect page they wouldn't get redirected because your PC is off

What might be possible is to have a PhP redirect script on your free hosting and have your domain pointed at that, and if your PC is on users get redirected to your PC, so in pseudo code terms:

<?php
if(hosting1 = available) {
header('Location: hosting1/index.php');
}
else {
echo 'content';
}
?>

Not sure what the argument for the if statement would actually be though, one of the pro's here will need to supply that.

[EDIT]

Also, you'll need to make sure that your free hosting supports PhP if this is to work. I've never used PhP on free hosting before, so I couldn't offer the names of any suitable providers I'm afraid.

Readie

12:58 am on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've actually thought of a way to do this without over complicating things.

If you have a file on your main hosting called something like redirect.php, with the following content:

<?php
header('Location: hosting1/index.php');
?>

and then on your free hosting, you have index.php with the following code:

<?php
include 'http://hosting1/redirect.php';
echo 'content';
?>

Then if your server is up, there will be a file for it to pull, which will automatically redirect users to your server; and if it is down, then there will be no redirect for it to run.

greeny

4:05 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Readie: Thank you so much =) i was trying to find this solution since one week but every buddy was talking about DNS & other stupid solution i tested this script u gave me its working but the problem is its taking 10-15 seconds to load but thank you so much =)

greeny

4:16 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Can we print "Please Wait Page is Loading" on page while it load the page ?

Readie

4:27 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm fairly certain the
header('Location: ...');
needs to be the very first line in the document to work, and so it would be the first thing to be run.

Nothing else would happen until the server had pulled the redirect.php or verified that it was unavailable; so a echo/print command would happen too late.

The first example I submitted might work more smoothly, but as I said, I'm not sure what the argument for the if statement might be. It might work as:

<?php 
if(http://hosting1/index.php) {
header('Location: http://example.com/index.php');
}
else {
echo 'content';
}
?>


But you would probably run into the same problem with load times without an argument that brings a little more finesse.

[edited by: eelixduppy at 8:36 pm (utc) on Feb 16, 2010]
[edit reason] switched to example.com [/edit]

greeny

4:46 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



ok let me test it =)

greeny

5:00 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



==================================
Script 2 index.php its working

==================================

FREE HOSTING (http://greeny.example.com/index.php)

<?php
include 'http://greeny.example.com/redirect.php';
echo 'Hi FRIENDS!
Greenyz Sharing Server is Down right Now, Please Wait for Greeny when he will be online this Website will Work. Because the contents of this website loads from his Computer He share Data Directly from his Computer You can make request aswel if you want Movies, Songs , Mobile Applications, Tutorials & much more. =) You Can add him on your facebook [facebook.com...] if you want or you can email him if you want to make request manually greeny@example.com Thank you.';
?>

============================================

redirect.php

================================================================

<?php
header('Location: [greeny.example.com...]
?>

[edited by: greeny at 5:05 pm (utc) on Dec. 22, 2009]

[edited by: eelixduppy at 5:16 pm (utc) on Dec. 22, 2009]
[edit reason] exemplified [/edit]

greeny

5:04 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



====================================================================
Script 1 index2.php ( NOR WORKING) Please Check where i m mistaking?
====================================================================

FREE HOSTING (http://greeny.example.com/index2.php)

its giving this error =(

Parse error: syntax error, unexpected ':' in /home/vhosts/greeny.example.com/index2.php on line 11

<?php
if(http://greeny.is-a-geek.net/wordpress) {
header('Location: [greeny.example.com...]
}
else {
echo 'Server is DowN';
}
?>

[edited by: eelixduppy at 5:16 pm (utc) on Dec. 22, 2009]

Readie

5:14 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a heads up, the mods don't like us using specific URLs unless they are to authorities (such as php.net)

I am thinking something along the lines of:

<?php
echo '<p>Please wait, page is loading</p>';
$page = 'http://hosting1/index.php';
if(file_exists($page)) {
echo '<meta http-equiv="refresh" content="0;url=http://hosting1/index.php">;
} else {
echo '<meta http-equiv="refresh" content="0;url=http://hosting2/serverdown.php">';
}
?>

Unfortunatley, my own testing (using just a plain text echo, rather than a meta to stop annoying local cache problems) is always echoing the "else" - and I'm not sure why, if one of the gurus could take a look, that'd be appreciated.

greeny

6:49 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Sir I tried it Edit it like this please check wotz the error cuz its giving this error

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/vhosts/greeny.example.com/index2.php on line 16

====================================================

<?php
echo '<p>Please wait, page is loading</p>';
$page = 'http://greeny.example.com/wordpress';
if(file_exists($page)) {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example.com/wordpress">;
} else {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example.com/serverdown.php">';
}
?>

===========================================

PLEASE CHECK why its giving this error? =(

[edited by: eelixduppy at 8:38 pm (utc) on Feb 16, 2010]

SuperDuck

7:48 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Why don't you ping the server ?

Make an if else statement and you are done!
Sorry if I misunderstood your demands!
change yourip with the ip of the server! don't use " or '
<?php
$str = exec("ping -n 1 -w 1 yourip",$input, $result);
if ($result == 0){
echo "ON";
}else{
echo "OFF";
}
?>

greeny

9:12 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Super Duck can u fix this plz

<?php
echo '<p>Please wait, page is loading</p>';
$page = 'http://greeny.example.com/wordpress';
if(file_exists($page)) {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example.com/wordpress">;
} else {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example.com/serverdown.php">';
}
?>

And put ur ping ip codes in this

<?php
$str = exec("ping -n 1 -w 1 yourip",$input, $result);
if ($result == 0){
echo "ON";
}else{
echo "OFF";
}
?>

==========================================
my Auto ip DNS is: greeny.is-a-geek.net
my free hosting URL is: greeny.freetzi.com

plz put the values & send me the code i m realy sorry i m kinda dump in php but i realy want this solution =( ill be highly thaNkful of you guys from my heart...

thanks

[edited by: eelixduppy at 5:31 am (utc) on Dec. 23, 2009]

Readie

9:17 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you are missing an apostrophee (') at the end of the first meta tag on the if statement. That will account for the reason you are getting "Unexpected T_STRING"

And so am I on my example, but I was in a bit of a rush when I posted that I'm afraid. My apologies for the confusion.

Regards,

Readie

greeny

9:31 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Readie: please you complete fIX & past it here plz ill be thankful i m getting confuse =S i m sorry =(

<?php
echo '<p>Please wait, page is loading</p>';
$page = 'http://greeny.example.com/wordpress';
if(file_exists($page)) {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example.com/wordpress">;
} else {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example.com/serverdown.php">';
}
?>

[edited by: eelixduppy at 5:31 am (utc) on Dec. 23, 2009]

SuperDuck

9:45 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



<?php
$str = exec("ping -n 1 -w 1 greeny.example.net ",$input, $result);
if ($result == 0){
// here you put the code if the server is up and running! Like header(Location:)
}else{
// here you put the code if the server is down, and you show the 'content' from where the file is hosted (i think greeny.example.com )
}

// still have questions ?, don't hesitate to asq!
?>

[edited by: eelixduppy at 5:32 am (utc) on Dec. 23, 2009]

Readie

9:52 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
echo '<p align="center">Please wait, page is loading.</p>';
$str = exec('ping -n 1 -w 1 greeny.is-a-geek.net ',$input, $result);
if ($result == 0) {
echo '<meta http-equiv="refresh" content="0;url=http://hosting1/index.php">';
} else {
echo '<meta http-equiv="refresh" content="0;url=http://hosting2/serverdown.php">';
}
?>

To borrow SuperDuck's (Cheers by the way Duck :)) code.

Regards,

Readie

greeny

10:29 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



ok let me test thanks

greeny

10:44 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Echo Function for serverdown is not working i TESTED this when i Stop my

host1 : [greeny.example.net...]

its still loading this page & IE Shows Page Cannot be displayed

Please you check it i upload it on my host2 :

[greeny.example2.com...]

=========================================================================

<?php
echo '<p align="center">Please wait, page is loading.</p>';
$str = exec('ping -n 1 -w 1 greeny.is-a-geek.net ',$input, $result);
if ($result == 0) {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example.net/wordpress">';
} else {
echo '<meta http-equiv="refresh" content="0;url=http://greeny.example2.com/serverdown.php">';
}
?>

=========================================================================

[edited by: eelixduppy at 5:33 am (utc) on Dec. 23, 2009]

greeny

10:50 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



I stopped HOST 1 [greeny.example.net...]

i want you guys to test it

[edited by: eelixduppy at 5:33 am (utc) on Dec. 23, 2009]

Readie

11:18 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So far as I can tell, it's working fine.

If you have issues with this though, there is another method (returning to my second idea I posted)

====
HOST 1 redirect.php
====

<?php
echo '<meta http-equiv="refresh" content="0,url=http://hosting1/index.php">';
?>

====
HOST 2 index.php
====

<?php
echo '<meta http-equiv="refresh" content="60,url=http://hosting2/serverdown.php">
<p align="center">Pleas wait, page is loading</p>';
include 'http://hosting1/index.php';
?>

Which will wait 60 seconds before giving up and sending you to your "server is down" page.

I advise only using this method if you experience problems with the method outlined by SuperDuck.

Regards,

Readie

Readie

5:09 am on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



include 'http://hosting1/redirect.php';

Typo fix :/

SuperDuck

6:00 am on Dec 23, 2009 (gmt 0)

10+ Year Member



i still wait to see what have you choosed , both methods are okay from my point of view, so you just have to test them well before choose one!

greeny

12:24 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



Readie YOUR CODE WORKED MAN! YOU ROCK ! LOVE YOU ! THANK YOU SO MUCH =) =)

greeny

12:27 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



but the problem is its Compitable only with FIREFOX Browser ... when i test it with FirefOX it works but when i test it in IE it doesnt work its keep refreshing the page lOl =D

greeny

12:28 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



i m talking about this code =)

====
HOST 1 redirect.php
====

<?php
echo '<meta http-equiv="refresh" content="0,url=http://hosting1/index.php">';
?>

====
HOST 2 index.php
====
<?php
echo '<meta http-equiv="refresh" content="60,url=http://hosting2/serverdown.php">
<p align="center">Pleas wait, page is loading</p>';
include 'http://hosting1/index.php';
?>

SuperDuck

6:39 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



than, why don't you try my code? As I said, test well before choose something, use different browsers with different versions, or ask your friends to test! It is one of the most important step when developing something!

Readie

6:52 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I made a mistake Greeny; change
include 'http://hosting1/index.php';

to
include 'http://hosting1/redirect.php';

... As I said to above.

Regards,

Readie