Can you test a web site's running, from a remote site?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you test a web site's running, from a remote site?

Post by simonmlewis »

I need to run a simple script that checks a web site is running.
If it does NOT load, it need to do something (which I can do - like an email).

It's basically a remote means of making sure a site is running. And if it's down for more than a minute or 3, it fires me an email.

It would rn thru a Cron file. But I've no idea what PHP script would check a site loads, and if it does NOT load, what output format that would come in.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you test a web site's running, from a remote site?

Post by requinix »

There are services out there that can do this for you. Like Pingdom, to name one off the top of my head. It has a free plan that might provide all you need.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you test a web site's running, from a remote site?

Post by simonmlewis »

Thanks. Is there no php script that can be run to test a url runs a page?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you test a web site's running, from a remote site?

Post by requinix »

You can, but why reinvent the wheel and manufacture them yourself?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you test a web site's running, from a remote site?

Post by simonmlewis »

Because I have complete control of it.
I can then make it send me an email when a site goes down, and if I choose, I can make the script check multiple sites.

So if you can - how ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you test a web site's running, from a remote site?

Post by requinix »

Are you also a mechanic? Farmer? Doctor? As one programmer who would much rather reinvent the wheel than accept one from a third party to another, sometimes it's worth letting someone else with more knowledge and experience take over.

cURL is the go-to for pretty much everything more complicated than downloading a page from a website. Set a connection timeout of a second or two (should be nearly instant), a timeout of a few seconds longer than your site should take, and check that the site returns a 200 response code. For the URL, pick a page you know will test your site, like one that uses PHP code and hits the database. If it fails you can mail() yourself.
Then set up a cronjob to run however often you want.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you test a web site's running, from a remote site?

Post by simonmlewis »

A mechanic???
I have looked up cURL and cannot see what you mean. I often find on here some helpers try to help without helping.

I just need a means to test a page works within a timeout. Curl sounds fine, but how?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can you test a web site's running, from a remote site?

Post by pickle »

Nagios is a pretty powerful system
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you test a web site's running, from a remote site?

Post by requinix »

And I often find on forums that people want to receive code rather than advice and suggestions. I'll tell you what to do but I won't give you the code to do it.

curl_setopt() is the most important function. Look at the manual for the big giant list of possible options you can pass, then check the examples and the user notes to see how it's used. Once you get a feel for it, the options you actually need are CURLOPT_TIMEOUT for the overall timeout and CURLOPT_CONNECTTIMEOUT for the connection timeout.
When you execute the request you've built up, cURL will return true or false if it worked. If it did then you also need to check the HTTP code returned: use curl_getinfo for that (it should always ==200).
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you test a web site's running, from a remote site?

Post by simonmlewis »

Well I don't know Curl at all, and those scripts are often very difficvult to follow, so it's often useful for someone to say: here's how to write it. And here's what each bit means.

I guess you dont' like tutorials that really help.

Thank you.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply