October 19, 2007

Reading a file's contents with PHP

PHP provides three built-in functions which allow you to easily read the contents of a file on your webserver. This is useful when, for example, another program may write information to the file and you could access that information through your script.

In this example, we're going to use the following example 5 line file, saved as file.txt:
line 1
line 2
line 3
line 4
line 5

In the same directory as file.txt, we're going to work with the PHP file test.php. We'll outline the file reading functions below.

Continue reading "Reading a file's contents with PHP" »

Tags:

September 27, 2006

Temperature Conversion Program in Java

Here's a simple program to convert temperatures between degrees Celcius, Fahrenheit and Kelvin written in Java. There's no GUI used here - all output is done via the command line.
The forumulae for the conversions were obtained from Temperature conversion forumlas on Wikipedia

Continue reading "Temperature Conversion Program in Java" »

Tags:

December 21, 2005

Image Roation with PHP

This tutorial will show you how to rotate an image in your PHP scripts. You'll need to have the GD library installed to be able to use some of the functions we use in this guide, though this is included with most PHP installations so there's a chance you'll have it.

Image rotation in PHP is handled by a function called imagerotate(), which takes three parameters (and optionally a fourth, which deals with transparency and is beyond the scope of this guide). The first is the resource identifier which you obtain when you load the original image within your script, the second is the angle which you want to rotate the image and the third parameter is used when the image isn't going to be rectangular, and specifies the colour with which to fill the uncovered areas of the rotated image to make it into a rectangle (since all image formats require rectangular images).

Continue reading "Image Roation with PHP" »

Tags:

How To Add A New Line in a C# or Visual Basic TextBox

If you're building a Windows Form Application, for example with C# or Visual Basic, then you may need to add a new line character, for example in a multiline TextBox. You do this using Enviroment.NewLine to insert the line break as follows:

textBox1.Text="First Line" + Environment.NewLine + "Second Line";

My preferred use is with the += operator in C#:

textBox1.Text = "Line One";
textBox1.Text += Environment.NewLine;
textBox1.Text += "Line Two";

This way, you can add extra lines of output to the TextBox as you require it.

References: Environment.NewLine on MSDN

Tags:

Finding Items in an Array with PHP

The problem: we have an array of items in PHP and we want to find out if a specific item is in the array. In code we can define the array as:

<?
// create an array of strings called $fruitBasket:
$fruitBasket = array( "Apple", "Orange", "Mango", "Lemon", "Pear" );
?>

IE we have an array called $fruitBasket which contains 5 strings, each of which is the name of a fruit. We want to find out if there is an Apple in the $fruitBasket - is there an element "Apple" in the array?
We do this with the following code:

<?
// create an array of strings called $fruitBasket:
$fruitBasket = array( "Apple", "Orange", "Mango", "Lemon", "Pear" );
// use the in_array() function to check if "Apple" is in the array:
if( in_array("Apple", $fruitBasket) )
{
  echo "Apple is in the array";
}
else
{
  echo "Apple is not in the array";
}
?>

This code uses the in_array() method to check if the element "Apple" exists in the array $fruitBasket:

in_array("Apple", $fruitBasket)

in_array() takes two parameters here: firstly the object we're looking for, in this case "Apple", and secondly the array which we're looking in, $fruitBasket. It then returns a boolean value: true if "Apple" is in the array, or false if it isn't.

References: php.net manual: in_array() function

Tags:

The Difference Between require() and include()

The key difference between require() and include() is that if you require() a file that can't be loaded (eg if it isn't there) then it generates a fatal error which will halt the execution of the page completely, and no more output will be generated. On the other hand, if you include() a file that can't be loaded, then this will merely generate a warning and continue building the page.

What one you should use depends on the situation; require() is best suited for loading files that are essential to the rest of the page - for example if you have a database driven website then using require() to include a file containing the database login and password is clearly preferred over using include(). If you used include() in this situation, then you may end up generating more warnings and errors than you had intended.

include() should be used when it isn't essential for that file to be loaded to execute the page. This is best used in situations where the file isn't essential to the processing of the page (for example a footer file), so if the file isn't present then the user can still view the site. You should, of course, make sure that all files you include() and require() are going to be available.

Continue reading "The Difference Between require() and include()" »

Tags:

December 15, 2005

Yahoo! Webhosting Integrates Movable Type

Yahoo! Web Hosting now includes Movable Type by default, as Six Apart (the folks behind Movable Type) revealed today. This includes automatic installation and upgrades to the latest Movable Type versions. However, it's not too clear which license of Movable Type applies (there are several, ranging from free for personal use to commercial ones) and what degree of technical support is available.

Tags:

December 08, 2005

Web Hosting Terminology

This article will define what various terms used by web hosting companies mean. It's not possible (and sometimes insulting to the reader) to cover everything so this will just cover the most commonly used and important terms.

Bandwidth (or Transfer or Data Transfer)
Bandwidth is the amount of traffic that is sent (and recieved) by your website. It is usually measured in gigabytes (GB), where 1 GB = 1024 megabytes (MB), and it is usually allocated on a monthly basis, for example you could have 5GB/month of bandwidth, which means that there is a limit of 5GB of files that can be downloaded from your site within a month. Bandwidth is only a concern if you host a lot of files, for example images or movies, as webpages are usually small file sizes.

Colocation Server (or Colocation Hosting)
This is similar to Dedicated Hosting (see below), except the server is owned by a customer and they pay the colocation hosting company to host the server for them - that involves paying a charge for bandwitch which the hosting company provide, the physical space which the server takes up and the power that the server uses. With this set up the customer is responsible for their hardware.

Control Panel
This is a web page with a usable interface which the customer can connect to when they want to tweak their hosting settings, view the transfer statistics, or utilise other features provided with their hosting.

Dedicated Server (or Dedicated Hosting)
This is a server which is owned by the hosting company which they will rent out to you for a monthly fee along with an allotment of bandwidth which you can use with it. This means that you get a whole server to yourself to use for what you desire, this is needed for scripts which use a lot of server resources (for example large message boards, for one of which I pay for a dedicated server). As a customer, you would have full control over the server and could change the configuration as required, a facility not available when you have to share a server with others. With a dedicated server, the web hosting company is responsible for the hardware as they own it, not the customer.

Disk Space
This is the amount of space on the server that you have to upload your files on, it's usually measured in MB or GB.

MySQL
This is a type of database (as is PostgreSQL) which allows the customer to store and retrieve information on the server. A lot of scripts require that a database is present to function.

Reseller Hosting
This is similar to Virtual Hosting (see below), but the customer is given the ability to create small hosting accounts of their own, which they can then give or sell to people if they desire. It usually allows the customer more features, bandwidth and diskspace than with normal virtual hosting.

Scripts
(eg PHP, Perl, ASP)
These are server side scripting languages which allow the customer to use already written scripts, or write their own, which provide additional features for the website and increased interactivity (for example message boards and content management systems).

Virtual Hosting (or Shared Hosting)
This is where a webhosting company puts many customers' webpages on the same server - so the server resources are shared amongst all the customers using it. This means that applications which cause a high server load are not suitable, and if one customer uses one then other customers will suffer.

Tags:

Dedicated Servers: A Summary

What is dedicated hosting?
This is a server which is owned by the hosting company which they will rent out to you for a monthly fee along with an allotment of bandwidth which you can use with it. This means that you get a whole server to yourself to use for what you desire, this is needed for scripts which use a lot of server resources (for example large message boards, for one of which I pay for a dedicated server). As a customer, you would have full control over the server and could change the configuration as required, a facility not available when you have to share a server with others. With a dedicated server, the web hosting company is responsible for the hardware as they own it, not the customer.

What are the advantages of it?
You can do anything you like on it that you like (although most hosts have acceptable use policies which state that illegal content is not allowed, and in some cases they prohibit connecting to IRC). This means that you can login to the server, set up programs as you desire, then run them without having to worry about the effects on server resources for other people. You can run games servers (for games such as Half-Life, etc), IRC servers or bots, or just run a web server which uses a lot of resources (and with a special setup which isn't found in shared hosting).

What are the disadvantages?
The main disadvantage is the cost, which will put many people off getting one. The cheapest seem to come at about $50/month, and the most expensive cost thousands of dollars each month. The price can be reflective of the quality (such as the cost of running - or your host renting a space in - the datacentre, the hardware in the server, the technical support provided by your host and so on), alternatively it can just be an over inflated price for what is a poor quality product. It's important to compare the prices and packages offered from several companies before coming to a decision, also you should ask your friends if they have any opinions on the matter, and research information about the hosting companies and datacentres - are the constantly offline due to DDOS attacks? do they have a poor record of customer service?
Another disadvantage is the added responsibility involved in keeping the software on your server secure, for example you may have to update builds of apache, control panel software, etc as security holes are found.

Is it for me?
That depends, if you want to a game server then you'll need one - but it may be easier for you to buy one from a dedicated games server comapny, they know their stuff and would probably be able to install and configure the servers for you. However they would place restrictions on what you could do with the server and maybe the setup.
If you want to run a small IRC server, bot or bouncer then you would probably be easiest paying a smaller fee to hire one of those from an IRC or shell provider company, also you don't have to worry about restrictive datacentre AUPs which prohibit IRC use.
If you are starting a small web forum or site then you probably won't need one initially, wait until it grows and you can determine if you need the server to ensure further growth is ok.
Otherwise you could possibly look into getting one, you could even email hosting providers asking what package they think your site would require (although take their replies with a pinch of salt, they could try to sell you stuff you don't actually need).

Tags:

November 02, 2005

How to Find the Current URL with PHP

The full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the query string. For example, take the URL http://www.example.com/example/page.php?name=Bob. The three parts of this are:

1. The domain name: www.example.com
2. The path to the page: /example/page.php
3. The query string: name=Bob

So how do you find it all out with your own PHP scripts?

Continue reading "How to Find the Current URL with PHP" »

Tags:

November 01, 2005

Find a visitor's IP Address with PHP

Do you want to know the IP address of a visitor? This can be useful for many reasons, such as tracking site usage or blocking access to specific people. Here's how you can find it using PHP.

Continue reading "Find a visitor's IP Address with PHP" »

Tags:

October 24, 2005

How to use PHP in pages with a .html extension (or any other)

To be able to use php code on a page with an extension other than .php, you need a server which supports .htaccess files.
To add an extension to be parsed for php, create or edit a file called .htaccess (with the dot first) containing the following line:
AddType application/x-httpd-php .html .moo .htm
This tells the server to check for php code and execute it in files with extensions .html, .moo or .htm.

Tags:

October 19, 2005

Random Colors in Flash

Want to set something to a random colour in Macromedia Flash? Once upon a time I did, and I found this code which did the trick.

Continue reading "Random Colors in Flash" »

Tags:

How to find the SHA1 hash of a string in Ruby

To find the SHA1 hash of a string, first include the library 'digest/sha1' then use the code below, where 'password' is the string we're computing the hash of.

require 'digest/sha1'
# ...
password = "blah"
sha1_pass = Digest::SHA1.hexdigest(password)

Tags:

How to use the QueryString in ASP

This article tells you how to pass variables to an ASP page using the QueryString, and how to access them from that page.

Have you ever seen a URL which looked like "www.example.com/page.asp?mode=1&style=red"? Well, this page is being passed variables and their values through the QueryString, here the variables "mode" and "style" are being passed, with values "1" and "red" respectively. The question mark indicates the start of the query string and the ampersand, &, symbol seperates variable=value assignments.

Continue reading "How to use the QueryString in ASP" »

Tags:

Categories

Adlinks

Creative Commons License
This website is licensed under a Creative Commons License.
Powered by
Movable Type 3.2