Quantcast

Class or ID?

(No Ratings Yet)
Loading ... Loading ...

If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!

Probably the most misused thing in CSS are classes and IDs. People just don’t know when to use which.

An ID is a unique identifier. Say if I want my navigation menu to have the ID nav, no other element will have the nav id. A class is, in my words, a definition for how something looks. Say you want everything in multiple IDs to have green text, well you wouldn’t assign all those elements the same ID, you would assign them the same class.

In a nutshell, ID = unique, class = multiple, formatting.

MySQL Databases and cPanel

(No Ratings Yet)
Loading ... Loading ...

MySQL is a popular SQL database platform. It’s commonly installed on servers and is a core PHP extension.

Software such as Invision Power Board, phpBB, vBulletin, and WordPress require MySQL (with support for other database platforms) in order to function. For the novice user, setting up the database and then entering that information in a config file may be a little confusing. I’m going to be using cPanel to set up the database, other control panel software may be different.

First we have to log into cPanel, then click the MySQL icon.

mysql-icon.png

Now, you will see a page with any MySQL databases you already have, then 4 forms, only 3 of which we’re going to worry about. First, we need to create our database, this is the field right under the ‘Add User to DB’ button. If we’re installing WordPress, we might want a database named wordpress, we’ll enter that in the box and press OK.

db-create.png

Now that we have our database, we need to create a user. This form is right below the add database one. The username we enter will be prefixed with your_cP_username_dbusername. Also, the length of usernames are limited, so although I’m entering wordpress, it will only be wordpres. This means my username would be mck9235_wordpres. Enter a password you will remember. Look at the image if you’re still confused.

db-user.png

Last, we need to add our user to the database and give it the proper permissions. This is the first form. All you need to do is find your user and database from the dropdowns and then click add.

db-user-add.png

Now, when you go to the main MySQL page, you will see something like:

db-final.png

At last we have our database set up, but we still need to be able to enter the details. A sample config file might look like this:

<?php
$host = ‘localhost’;
$user = ;
$db = ;
$pass = ;
?>

Now, the host will be localhost 99% of the time, so you don’t need to mess with that. We know our username is mck9235_wordpres, db is mck9235_wordpress, and the password is whatever you said. All you need to do is fill those values in, then you’re ready to go!

Database backups using cPanel

(No Ratings Yet)
Loading ... Loading ...

It sucks to visit your site and find it hacked or maybe a extremely rare, but deadly technical glitch happened on the server. This only sucks when you don’t have a backup of your database, but it’s actually very easy to do. We’re going to use cPanel’s database backup tools to make a CRON based backup script that will send us, as an attachment, a backup of our database via E-Mail.

This actually isn’t that hard to do, the hardest part was figuring out how to send attachments with PHP’s mail() function. Thankfully, I found an excellent tutorial on it.

First of all, never store CRON files in your public_html directory, otherwise the whole world can access it. Store the file in the directory above it, e.g. /home/username/backup.php instead of /home/username/public_html/backup.php. Now we can move on to the script. Right after the opening PHP tag, we must tell the server to ignore all headers (thanks, Tim!):

 

<?php
#!/usr/local/bin/php -q
Just to keep things organized, we’re going to create two functions, one to get the database backup, and the other to send. However, we’ll only be calling one function, as it will call the function to get the database backup. There’s going to be a lot of headers, which I really can’t explain, but I can assure you that they tell the E-mail client there’s going to be both a message and an attachment.

 

function get_database_file($database_name, $domain, $username, $password)
{
//Construct the URL out of all our little pieces
$url = ‘http://’ . $username . ‘:’ . $password .‘@’. $domain .‘/getsqlbackup/’. $database_name .‘.gz’;
//Return the file’s contents
return(file_get_contents($url));
}function send_database_file($to, $from, $subject, $message)
{
global $database_name, $domain, $username, $password;
//Thanks to http://www.theukwebdesigncompany.com/articles/php-file-attachments.php for making this easy
$unique_sep = md5(uniqid(time()));
//Tell our E-mail client that we’re sending both a message, and an attachment
$headers .= “From: $from\n.
“MIME-Version: 1.0\nContent-Type: multipart/mixed;boundary=\”$unique_sep\”;\n.
“charset=\”iso-8859-1\”\nContent-Transfer-Encoding: 7bit\n\n .
“–$unique_sep\n.
“Content-Type: text/plain; charset=\”iso-8859-1\”\n.
“Content-Transfer-Encoding: 7bit\n\n.
$message.\n\n.
“–$unique_sep\n.
“Content-Type: gz; name=\”wp.gz\”\n.
“Content-Transfer-Encoding: base64\n.
“Content-Disposition: attachment\n\n;
//Get the file from our function
$file = get_database_file($database_name, $domain, $username, $password);
//This is where the file is actually attached
$headers .= chunk_split(base64_encode($file)) . “–$unique_sep–\n;
//Finally we can mail
if(mail($to, $subject, $message, $headers))
{
return true;
}
else
{
return false;
}
}

We’ve still got to declare some variables for our database, URLs, and user details. Last, but certainly not least, we’re going to send the backup!

 

//If your database is user_wp, ONLY PUT wp!
$database_name = “wp”;
//Include :2082, it’s cPanel’s port
//No www or http
$domain = “site.com:2082″;
//cPanel username and password
$username = “username”;
$password = “random_password”;
//Who to send the backup to
$email = “backups@programimi.com”;
//Appears in the from field
$from = “server@programimi.com”;//Send it off with the data in the E-mail message.
send_database_file($email, $from, $database_name .” Database Backup”, “The backup of “. $database_name .” was performed at “. date(“g:i A F j, Y”));
?>

Save the above (compiled into one) file as whatever you like, and remember, not in the public_html directory, the one above it! We still need to set up the CRON, hop into cPanel and click the CRON Jobs icon.

CronJob

Now click the standard button. I’ve made a little image that should get you through the next step:

CronMenu

Your database backup should arrive on whatver day, hour, and minute you told the server to run it! That’s all there is to it.

C++ Introduction

(No Ratings Yet)
Loading ... Loading ...

C++ is an extreme language and the one of the most difficult. C++ like PHP derives from C so C functions can be called when programming in C++. I will tell about the very basics to get started into the C++ programming world.
Below you see the very least for a C++ console program just to run and be empty. I have commented on the main points.

#include //contains basic input and output functions like cerr, clog, cout, and cin and basic operators
#include //contains other basic functions and operators
//more headers are available in Dev-CPP’s include directory. These include math.h (C header) for math functions sin, cos, tan, sqrt, fstream sometimes fstream.h (compiler dependent) that contains functions for reading and writing files in C++ (will explain later), and one that is very useful string.h sometimes string (again compiler dependent) which contains string manipulation functions such as erase and replace.using namespace std;//this means that you want to use the namespace std which contains basic input and output functions otherwise you would have to call functions, operators, etc in a manner of namespace::functionname like std::cout

int main(int argc, char *argv[])//main function in console program where everything goes that needs to be done. This is a custom function and can also be seen as taking no arguments, int main()
{
system(“PAUSE”);//the system function. Whatever you put between the quotes is what you want to be sent to the command line. In this case PAUSE tells the program to wait and let the user see the results
return 0;//return value of 0 because the function is an int
}

Now that the structure of a basic console program has been explained now variables. First, what is a variable? A variable is sort of like a variable in math. Variables hold values and can be outputted. These variables can consist of letters, numbers and some symbols. Variables can not have the same name as another variable in C++. For example the first set of code outputs an error on compilation and the second compiles with no errors. Note: The code takes into consideration that the variables are declared in the main functions in the above template.

int i;
i = 0;
char i;//variable already used and declared as variable type int
i = ‘a’;//variable is type int and cannot hold a character

int i;//perfect
i = 0;//perfect
char a;//perfect
a = ‘a’;//perfect

Now for the variable types. A variable type decides what that variable can hold. A type of int can only hold whole numbers for example -1, 0, 1, etc. A type of char can only hold a single character or symbol (unless character array, or pointer). A type of string can hold letters, numbers, and symbols. A type of float can hold a single precision value (decimal, fraction) accurate to 7 decimal places. A type of double can hold double precision values (decimals, fractions) accurate to 15 decimal places.

I have given enough information to write a very simple program. Good Luck.

Yahoo! I’m Feeling Lucky

(No Ratings Yet)
Loading ... Loading ...

Most know about Google’s I’m Feeling Lucky feature, you enter a search term, and it automatically takes you to the first result. Yahoo has no such feature, but were going to make one. Yahoo’s great set of API’s, and the JSON PHP class makes this simple. We’re going to use the Yahoo! API for search requests and then decode the response with JSON, and redirect from there.

To begin, make sure you have a copy of the JSON class (linked above). Now, making the API request is simple, we form a URL and then get the contents. The content will be sent to JSON to be decoded, then sent back to use as a large array (and sub-arrays) and we’ll get the URL from there.

The request URL will look like this:

$request = ‘http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=SC&query=’. $_POST[‘query’] .‘&results=1&output=json’;

This sends a request to the web search service API with our posted query. (we also say to output and JSON and limit to 1 response) Next, we have to get the contents of the request and then load up JSON and decode:

$response = file_get_contents($request);
require_once(‘./json.class.php’);
$json = new Services_JSON();
$data = $json->decode($response);

Still simple. We get the contents of the request (which is just a jumble right now :P ) then require, start, and use the JSON class to decode our response. The data variable now contains a huge array, it’s interesting to see what happens if your print_r() it. If you do that, you might want to try to find the URL variable yourself, but if not, keep reading and I’ll show you. :)

$redirect = $data->ResultSet->Result[0]->Url;
header(“Location: $redirect”);

Really, it’s that simple. :) The URL has three parents, the actual result which contains the URL and other data about that particular result, the result set which is all the results, and the array itself.

Using the Audioscrobbler API

(No Ratings Yet)
Loading ... Loading ...

The service last.fm offers is pretty cool, you register, and it automatically records data about your music, and puts that data on their website. From there, you can find others who have the same taste as you or find artists similar to the ones you like. Audioscrobbler, which powers last.fm, offers an API, which makes it easy to get the latest tracks, artists, albums, and more. This data is offered in a variety of formats, text, XML, and XSPF (rarely). We’re going to be using plain text, as it’s easiest to manipulate. For a listing of all the API services, see here. We’re going to be using the API to display our last listened to track.

We will be using the API for latest tracks in combination with the GD functions to create a dynamic image with our latest song. The API is simple, we have a url which will look like: http://ws.audioscrobbler.com/1.0/user/USERNAME/recenttracks.txt and that contains the latest tracks for that user. The first part of our script will handle the API part:

 

<?php
header(“Content-type: image/png”);
$user = “RJ”;
$url = “http://ws.audioscrobbler.com/1.0/user/$user/recenttracks.txt”;
$data = file($url);
$song = explode(“,”, $data[0]);
$song = explode(“-”, $song[1]);

The first part tells the browser that we will be sending out an image (png in our case), the next few lines make the request to the API, and then gets the latest song’s artist and title in an array ($song). The next part of the script will deal with creating, writing, and outputting our image. This is a little more confusing, just becuase the image functions often are.

 

$img = imagecreate(225, 30);
$bg = imagecolorallocate($img, 255, 255, 255);
$text = imagecolorallocate($img, 0, 0, 0);
imagestring($img, 2, 5, 5, trim($song[1]), $text);
imagepng($img);
imagedestroy($img);
?>

This creates a new image with the dimensions 225 by 30, makes white the background color, black the text color, writes our song and artist, then outputs it to the browser. The 2, 5, 5 are the font size, x position, and y position.

The final image is really simple, but it’s still cool. It’s easy to change the background color to fit the page it will be on, just get the RGB values and put them in for the $bg variable.
Enjoy!

Adding a Search Box to Your Custom 404 Error Page

(No Ratings Yet)
Loading ... Loading ...

Hopefully you’ve played around with some HTML when you were building your custom 404 error page, as explained in our tutorial on creating your custom 404 error page , in which case this will be a breeze.

If not, it’s time to roll up your sleeves and get ready to do some coding!

Step One: Identifying your 404 error page

The first step is to figure out which page is your custom 404 error page.

Most likely you’ve just built it, so that’s a very simple step, but if not, identify your httpd.conf file, then find the line therein that looks like this:

ErrorDocument 404 /errordoc-404.shtml

That’ll tell you what file is your 404 error page.

Step Two: Open up the file for editing

There are dozens of different Web page and HTML editors on the market, so your process will likely be different to what we use, but hopefully you have a tool like Homesite, BBEdit, or even vi or EMACS to edit the file and can do so directly. If not, you might need to download the HTML file from your server to your local computer then open it with a simple editor like NotePad (on Windows) or TextEdit (on the Macintosh) to proceed.

Step Three: Copy and paste this code

Now the fun part: copy the following lines of HTML and paste them directly into your custom 404 error page:

<form action=”http://www.google.com/search” name=”searchbox”
method=”get” style=”margin-left: 2em;” />
<input type=”hidden” name=”hl” value=”en” />
<input type=”hidden” name=”ie” value=”ISO-8859-1″ />
<input type=”hidden” name=”sitesearch” value=”programimi.com” />
<input maxlength=”256″ size=”40″ name=”q” value=”" />
<input type=”submit” value=”find it” name=”btnG”
style=”font-size:75%;” />
</form>

You need to change the domain name from programimi.com to your own domain, but that’s the only customization required. Save the changes and upload the new version of the file if needed.

Step Four: Testing

Once you’ve saved your new custom 404 error page, generate a page by hitting a link like http://www.example.com/badpage and looking for the form.

That’s all there is to it. You now have a lovely Google search engine that constraints itself automatically to a search of your pages only. Now you should ensure that Google knows about your site and you might also want to fix spelling problems too. :-)

Greetings And Welcome!

(2 votes, average: 5 out of 5)
Loading ... Loading ...

Greetings and Welcome!

This Portal is still under construction. Please come back later.

ProCom