Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,940 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,869 people online right now. Registration is fast and FREE... Join Now!




PHP If statement to check vaiables in another PHP doc

 
Reply to this topicStart new topic

PHP If statement to check vaiables in another PHP doc

MattGooch
24 Mar, 2008 - 12:28 PM
Post #1

New D.I.C Head
*

Joined: 24 Mar, 2008
Posts: 3

Hi i have this really simple bit of code which i use to check to see if my home pc is online.

i want to use this on my website running wordpress to tell visitors I am online and then for them then to be able to click the text saying "yeah im online" or w/e and be able to IRC me.

The code im using to check if the host is alive is:

CODE

<?php
//Web Server Status v 1.4, Copyright 2002 By Ryan Schwiebert, visit http://www.schwebdesigns.com/
//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, visit http://www.NathanDickman.com/
//Location of the live or dead server images

//Please change to your server specifications
$live = "live.php";
$dead = "dead.php";

//The status checking script
//meddle at your own risk!
//check for port number, default is 80
$link = "62.31.82.214:42356";
$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;
}
?>


inside the 2 output files the code is:

CODE

<?php
echo "yes im online";(and in the other file the same just "no im offline")
?>


This script was originally set to output an image file which isnt really what i want, i want it to check and then come back on the webpage with the text from either one of the output files.

So ive tried a few things, I just dumped the code in the wordpress page which did not work and i tried PHP include which also did not work and ive tried refrencing to the php file in a <script> tag in the header of the site.

So the only thing I could think of the could work would be an if statement on the website to go to the php doc and see which file it was outputting. So i wrote this:

CODE

<?php
$adres = "http://mattgooch.com/onlinestatus/status.php";
$picch = "live";

if ($adres == $picch)
{
echo "You win matt im online";
}
else
{
echo "your offline";
}
?>


That works in the sense that it doesn't return an error but doesn't work because it doesn't display the correct status.

This has been bugging me for a day now, and im a bit of a novice at php but ive been trying hard so if anyone could help me out I would really really appreciate it ! thanks
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: PHP If Statement To Check Vaiables In Another PHP Doc
24 Mar, 2008 - 03:09 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,232



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first of all the code you wrote at the end of your post there comparing $adres and $picch isn't going to work because you are comparing the two strings (not resolving the address) and thus the string "http://..." etc is not going ever match "live" so it will always fail.

Now I have played with the top script up top and it works fine as long as you remember that the IP address must be willing to accept requests through that port of 42356. Just to give you an idea I pointed the script to check google.com which I know is up all the time and it was reporting just fine.

Now remember you can't have header calls in the middle of a page if data has already been sent (you would see an error saying headers already sent). So what you should do there instead of using header is put in your echo statements as to whether or not you are online.

So in short here is a recap...

1) Use the top script, throw out what you wrote there at the end of your post.
2) Make sure that your computer or whatever machine pointed to having the ip of 62.31.82.214 accepts requests on port 42356. You may need to adjust your router and firewall settings to allow it through.
3) Get rid of the header calls because they will do you no good in the middle of a page. Use your echo calls instead for online or offline status. That way they will print in the page where you want them.

If you want to test the script, point the ip at google.com and a port of 80 just to test the script and make sure you are getting the message that it is "live".

Then work it from there. If you are getting specific error messages, be sure to post them here and we can do what we can to help.

Good luck. smile.gif
User is online!Profile CardPM
+Quote Post

MattGooch
RE: PHP If Statement To Check Vaiables In Another PHP Doc
25 Mar, 2008 - 01:50 AM
Post #3

New D.I.C Head
*

Joined: 24 Mar, 2008
Posts: 3

QUOTE(Martyr2 @ 24 Mar, 2008 - 04:09 PM) *

Well first of all the code you wrote at the end of your post there comparing $adres and $picch isn't going to work because you are comparing the two strings (not resolving the address) and thus the string "http://..." etc is not going ever match "live" so it will always fail.

Now I have played with the top script up top and it works fine as long as you remember that the IP address must be willing to accept requests through that port of 42356. Just to give you an idea I pointed the script to check google.com which I know is up all the time and it was reporting just fine.

Now remember you can't have header calls in the middle of a page if data has already been sent (you would see an error saying headers already sent). So what you should do there instead of using header is put in your echo statements as to whether or not you are online.

So in short here is a recap...

1) Use the top script, throw out what you wrote there at the end of your post.
2) Make sure that your computer or whatever machine pointed to having the ip of 62.31.82.214 accepts requests on port 42356. You may need to adjust your router and firewall settings to allow it through.
3) Get rid of the header calls because they will do you no good in the middle of a page. Use your echo calls instead for online or offline status. That way they will print in the page where you want them.

If you want to test the script, point the ip at google.com and a port of 80 just to test the script and make sure you are getting the message that it is "live".

Then work it from there. If you are getting specific error messages, be sure to post them here and we can do what we can to help.

Good luck. smile.gif


Awesome works a treat thanks !

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:56PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month