Archive for the ‘Code’ Category

by @ 12:36   Code   Permalink

PHP code to check server status

Tuesday, October 3rd, 2006

Borntwisted came up with a wonderful idea to check that his free host was running without having to visit the a page or check the ftp.

He wrote a short piece of HTML that would pull an image off his website, if the server failed to respond the ALT tage would display OFFLINE.

I like to code things in php so I decided to take this a step further and not actually use any images, thus saving on bandwidth on my server everytime the page loads.

The full code is at the bottom of this post but I am going to break it down and try to explain how each section works.

First of all we need to connect to the server and check to see if a file exists. This file can be anything you like, I chose to use an image called online.gif.

I declared a variable call $url which holds the data needed to connect to the server. @fopen is the php command to connect to the server and open the chosen file. The reason the @ symbol is used is to supress the error that will be returned when the file cannot be found.

Using "r" tells the command we want to open the file in read only mode.

PHP:
  1. <?
  2. $url = @fopen("http://path_to_server/online.gif", "r");

Now we use a simple ifelse statment to output the correct part of the formula.
The first part of the formula as below asks if the condition is true (if file exists) and will output the command below it.

PHP:
  1. if ($url) {

Before the command is outputted we have to close the connection to the file for security purposes. fclose closes the connection to the server which is defined again by our variable $url.

PHP:
  1. fclose($url);

Now we can output the following command if the file is found on the server. Basically this is the TRUE part of the conditional statement. We now ECHO a line of code to the browser which will be converted into HTML and displayed as what you will see. Do not worry about the div's and classes, that is just CSS styling for the page I have this code on.

PHP:
  1. echo("<div class='holder'><div class='name'>SG Status:</div> <divclass='online'></div></div>");
  2. }

The following code is the FALSE part of the staement, which is outputted if the above connection cannot find the file. It has similar styling to the above command but this time you will notice that the div class is set to "offline". This is so a different colour is displayed.

PHP:
  1. else {
  2. echo("<div class='holder'><div class='name'>SG Status:</div> <div class='offline'></div></div>");
  3. }
  4. ?>

That's all there is to it. The code is simple and works well. If you use this code you may notice that when the file is removed by yourself the script doe snot update, this is simply because of the server it is on. Sometimes servers take a few minutes to update.

The code as a whole is listed below and I have also added the CSS that I have used. Modify to your needs.

PHP:
  1. <?
  2. $url = @fopen("http://path_to_server/online.gif", "r");
  3. if ($url) {
  4. fclose($url);
  5. echo("<div class='holder'><div class='name'>SG Status:</div> <div class='online'></div></div>");
  6. } else {
  7. echo("<div class='holder'><div class='name'>SG Status:</div> <div class='offline'></div></div>");
  8. }
  9. ?>

CSS:
  1. .holder{
  2. width:80px;
  3. margin-left:5px;
  4. }
  5.  
  6. .name{
  7. float:left;
  8. }
  9.  
  10. .online{
  11. text-align:center;
  12. width:8px;
  13. height:8px;
  14. padding:2px;
  15. background:#75f475;
  16. float:right;
  17. border:1px solid #000;
  18. margin-bottom:2px;
  19. }
  20.  
  21. .offline{
  22. text-align:center;
  23. width:8px;
  24. height:8px;
  25. padding:2px;
  26. background:#ef2424;
  27. float:right;
  28. border:1px solid #000;
  29. margin-bottom:2px;
  30. }

Dynamic Menus for WordPress

Thursday, April 27th, 2006

The standard WordPress navigation menu is limited in that it does not show sub menus. it will list every single page on your website in one long list in the navigation section.

For larger sites this can become troublsome. Who wants 50 links running down the side of the page just for the content. It can also be very confusing to site visitors.

I decided I needed a simple script to display the sub menu only when the parent page was clicked, - a working example of this is on the right hand side of this page - so I wrote a little script to do this, and in the spirit of open source, here is the turorial so that you can make it yourself.

Styling individual comments for WordPress

Thursday, April 20th, 2006

I wanted a little script that would allow me to have specific styles for individual comments depending on the authour.

I had a look around for WordPress plugins and other info, but could not find anything that actually worked.

So I wrote my own little switch statment to sort it all out.

PHP:
  1. <?php
  2. switch (comment_author_email){
  3. case ($comment->comment_author_email == 'friend@friend.com'):
  4.        $style = '<div class="friendcomment">';
  5.   break;
  6.   case ($comment->comment_author_email == 'you@you.com'):
  7.        $style = '<div class="authorcomment">';
  8.   break;
  9.   default:
  10.       $style = '<div class="defaultcomment">';
  11.   break;}?>
  12.  
  13. <?php echo $style; ?>

This involves modifying the comments.php file in your theme folder.

Find:

PHP:
  1. <cite><?php comment_author_link() ?></cite>

And add this code above that line: Make sure you put the correct email addresses in.

PHP:
  1. <?php
  2. switch (comment_author_email){
  3. case ($comment->comment_author_email == 'friend@friend.com'):
  4.        $style = '<div class="friendcomment">';
  5.   break;
  6.   case ($comment->comment_author_email == 'you@you.com'):
  7.        $style = '<div class="authorcomment">';
  8.   break;
  9.   default:
  10.       $style = '<div class="defaultcomment">';
  11.   break;}?>
  12.  
  13. <?php echo $style; ?>

Now find:

PHP:
  1. <?php comment_text() ?>

Add the following on the line below:

CSS:
  1. </div>

That's it for the comments.php file.
Now open your style sheet, usually called style.css and add the following rules to the end of your style sheet, modify to yoru own needs.

CSS:
  1. .authorcomment {
  2. background: #ffffcc;
  3. padding:10px;
  4. border:2px solid #000;
  5. color:#333;
  6. min-height:90px;
  7. }
  8. .friendcomment {
  9. background: #fff;
  10. padding:10px;
  11. border:2px solid #000;
  12. color:#333;
  13. min-height:90px;
  14. }
  15. .defaultcomment {
  16. background: #eee;
  17. padding:10px;
  18. border:2px solid #000;
  19. color:#333;
  20. min-height:90px;
  21. }

Thats everything. Save and upload your files and see for yourself.

New Wordpress Theme and Mod available

Monday, April 10th, 2006
Morning Theme

Morning Theme

Quite aptly named, since it was very early in the morning that I created this :-)

Wordpress has hundreds of themes out there for you to choose from. I know there to be at least 700 as I search through a huge list looking for something that would suit my needs.

Each to there own though, because I couldnt find one remotely close to what I wanted. So, after 2 hours I had my first layout up and running. Since then I have been tinkering with the style and finally made this.

It is by no means the last style I will create, but for now it is here to stay.

You can find this Theme in the downloads section under Wordpress Themes and Mods. But, if your too lazy to click through to there, you can just click here

Festive Images

On that same page you will find the Festive Images mod. You can also see it running at the top of this site. Just wait till Easter. It allows you to have a certain image show up on a certain day. Ideal for having your Cristmas logo displayed over Cristmas, and you will not have to remember to upload it and edit the file.

Once you have added your dates to the script, and told it where to find each new image. just save it, upload it and your done.

Automation at its best.

Good Shades, Bad Colours

Wednesday, April 5th, 2006

In a chat with SoddenGecko i thought up this method for changing a sites colours when you have the shades right, but not quite the right colours. I'm too lazy to type it into a colour tutorial at the moment so heres the pasted chat message:

"if your thinking yea it blends well but i didn't want a tan site, then take a screenshot, take that into photoshop, then open the hue and saturation window and change the colour till you like it, then sample the colours and replace them in css, voila"

Contact