Archive for the ‘Programming’ Category

This post will outline the best hosting companies on the web. It take into consideration uptime, features, bandwidth, simplicity and of course their prices.

Rank Top Web Hosts Features Highlights (All have PHP & MySQL & root access)

1 HostMonster.com
Best cPanel hosting
¤ Unlimited webspace
¤ Unlimited bandwidth
¤ $5.95 / month
¤ FREE domain
¤ Ruby On Rails
¤ e-Commerce tools
¤ $50 Google Credit
¤ cPanel
¤ 99.9% uptime!
¤ ASP, Curl, Cron
2 HostGator.com
Unlimited domain hosting
¤ Unlimited space
¤ Unlimited traffic
¤ $9.95 / month
¤ cPanel/WHM
¤ Fantastico
¤ ASP, Curl, Cron
3 BlueHost.com
Best multi-domain host
¤ Unlimited space
¤ Unlimited transfer
¤ $6.95 / month
¤ FREE domain
¤ SSH access
¤ $50 Yahoo! credit
4 Godaddy.com
Free SSL and UNL Domains
¤ Unlimited space
¤ Unlimited traffic
¤ $14.99 / month
¤ Unlimited domains
¤ Free SSL
¤ Dedicated IP
¤ eCommerce ready

Lately I was wanting to create a scheduled tweet to post to youtube. I then researched into twitter’s api and found that it needed cURL fields, so I produced the following script.

<?
$twitterUsername = Username;
$twitterPassword = password;
$status = "What you want the tweet to say here. This can include links.";
	if ($status) {
		$tweetUrl = 'http://www.twitter.com/statuses/update.xml';
 
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
		curl_setopt($curl, CURLOPT_USERPWD, "$twitterUsername:$twitterPassword");
 
		$result = curl_exec($curl);
		$resultArray = curl_getinfo($curl);
 
		if ($resultArray['http_code'] == 200) {
			return true;
		} else {
			return false;
		}
 
 
		curl_close($curl);
	}
?>

You are free to use and modify this script or set it up as a cron. If you use it as a cron make sure to add a time variable. Thank you for using bright-tutorials.

In order to generate a random string of letters and numbers we will be merging together two php functions. If you want to limit it’s size or limit it to just letters we will also be going over such topics.
Please look over the following functions before we begin:
1. The mt_rand function which generates a random number similarly to rand but 4x faster.
2. The sha1 function which generates a random 32character string of letters and numbers.
3. The substr function which cuts a word by a select number of letters.
4. The str_replace function which replaces a select letter/word/symbol.
5. Arrays and functions as they will be used to commit an str_replace.

Begin Tutorial————————–

1. Start with adding a number 0-999 or higher if you choose:

<?
$rand = mt_rand(0,999);
?>

2. Then create a random sha1 hash code:

<?
$rand = mt_rand(0,999);
$hash = sha1($rand);
echo"".$hash."";
?>

3. You now have a 32character string but what if you wanted a 10 character string? Well here’s how using substr:

<?
$rand = mt_rand(0,999);
$hash = sha1($rand);
$first10 = substr($hash, 0, 9);
echo"".$first10."";
?>

4. But what if you wanted just the first 10 letters? Use str_replace in a function:

<?
function remove_numbers($string) {
$numbers = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " ");
$string = str_replace($numbers, '', $string);
$string = substr($string, 0, 9);
return $string;
}
$rand = mt_rand(0,999);
$hash = sha1($rand);
echo remove_numbers($hash);
?>

5. What if you wanted it to be a very long string? Of course you could merge hashes together, but then that is just extra work. Let’s take another approach using strlen we will loop through a list of numbers and letters 10 times as defined by $length. To limit it to letters just delete the numbers in $characters.

<?
function RandomString() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}
echo RandomString();
?>

6. We can even pass the amount of characters through as a function like so (this will create a random 20 character string):

<?
function RandomString($length) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}
echo RandomString(5);
?>



After getting XAMPP setup, and having Apache and MySQL running open your IDE. Notepad will suffice for now and that is what I will use to demonstrate, but in the future Notepad++ is recommended. Open Notepad and enter in the following.

Save it as a test.php to your desktop and make sure you Save as Type: All Files or it will not work.

Then locate your htdocs folder which is at C:\xampp\htdocs supposing you saved it to the default location on the installer. Drag and drop your test.php file from your desktop into this folder.

Then check once more that MySQL and Apache are running if they are, navigate to http://localhost/xampp/htdocs/test.php in your web browser.

You have created your first program! Now we want to make some dynamic content, so reopen the test.php file by right clicking it and selecting Open With->Notepad to edit it with Notepad. Then enter in the following new code.

What “$” denotes is that it is a variable. Unlike JavaScript, Java, or C++ you do not have to declare what variable type it is. So what happened is the variable $randomvariable was given a random value of zero up to ten. In order to print this variable or echo it we had to cancel the quotation marks and put it in the middle. The periods are just there to make it safe and you do not have to worry about them, just know to put them there. Unlike many other languages php has one of the easiest randomization functions, the “rand” function is built in with php and the first number is the minimum, while the second is the maximum. It is separated by a comma as is required by PHP.

Now save it and refresh http://localhost/xampp/htdocs/test.php you should get the following.

You can refresh the page many times and will get different numbers all within the boundaries of 0-10. I know what you are thinking this is all good and fun but what does this all have to do with regular websites they are not just numbers! Oh, but in a way they are! As I said in the first post php is what makes up html. Once again open the file with Notepad and change the file with the following.

This will create an image with a dynamic height.

This ends the second part of the tutorial, the next tutorial will deal with strings and how to make a random string/image show up.

<-[PREVIOUS] [NEXT]->


So you know HTML and are now wanting to expand your horizons? You are wondering what else there could be ahead and are ready to attempt some more sophisticated programming languages. Maybe you have heard of a few, possibly C++, Flash, Java, or JavaScript? The answer is not a simple one, if you would like to start creating basic one player games you would likely look first at Java for console based or JavaScript for web based, but even with these you will come across barriers if at some point you want to add multiplayer connectivity or you want to allow the player to save their game progress. You would then likely have to start back from scratch in order to incorporate these changes.

Now, you may be concerned with the mere mention of multiplayer connectivity and may fear you are not ready for such a thing but it is actually much simpler than you think. The vast majority of web pages consist of different content that is dynamically generated. Dynamically meaning it comes from a database and/or it differs depending upon preset conditions. Now more likely than not if you have only worked with html you do not have any idea of what a database consists of, but it is nothing to fear and it is no way related to the command prompt/(cmd) or terminal at least not with how you will be using it in this tutorial. This is a common misconception and should be removed from your brain immediately in order to avoid intimidation.

So you now know that web pages in the same way as multiplayer games get their information from a database, and you are wondering what this has to do with programming. This is were PHP and MySQL come in. When it comes to server side programming PHP and MySQL are the peanut butter & jelly of the website world. MySQL is the language you will use when dealing with your database. It stands for “My Structured Query Language” and it is easily incorporated when programming in php.

PHP stands for “Hypertext Preprocessor”, which put simply means it is what directs html to act in a dynamic fashion. This can be a difficult concept to grasp but by the end of this tutorial you will understand how the majority of web pages are created in order to display different content depending upon conditions that you as a programmer set.

In order to proceed you will need to install XAMPP. In order to get help with installing XAMPP go to here and find an instillation tutorial that fits your operating system. To download XAMPP directly go to for Windows or for Mac or for Linux. Once you have XAMPP installed correctly on your system move on to the next section.

NEXT->