Archive for 'Tutorials'

DB Connection Info in Seperate File

Posted on 03. Jun, 2009 by admin.

0

While PHP can be an exciting and intrigueing language, it can also bring with it much frustration in the form of erros. A character in the wrong place or even simple white space can cause an entire script to not function. Complex errors often lead us straight to our favorite programming forum where we post our code and beg for help. We often times see programers posting the contents of their code without first considering security risks. The most common mistake is forgetting to hide the database connection info. Sometimes this is a simple slip-up while other times it is caused by people simply not knowing the risks.

To prevent this from happening to you, we reccomend storing your database connection information in a seperate file and then using the built in “include” function to do just that, include it into your script.

This is a very simple 2 part process. We will start by creating a file called mysql_connect.php which consists of the following snippet.

<?php
DEFINE (‘DB_USER’, ‘username’); // Insert your database username into the quotes.
DEFINE (‘DB_PASSWORD’, ‘password’); // Insert your database password into the quotes.
DEFINE (‘DB_HOST’, ‘localhost’); // This will most likely stay the same.
DEFINE (‘DB_NAME’, ‘database_name’); // Insert your actual database name in the quotes.

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die (‘Could not connect to MySQL: ‘ . mysql_error());
@mysql_select_db (DB_NAME) OR die(‘Could not select the database: ‘ . mysql_error() );
?>

This defines the values of the variables used to connect to the database using the username, password, and database name you enter. If you receive an error it is most likely caused by an incorrect value. Double check each of the values you entered to make sure they are correct.

Once you have saved and uploaded the file to your server you simply need to add the following snippet to any page you wish to connect to the database.

<?php
include ‘mysql_connect.php’;
?>

So that’s about it for this basic script. Feel free to share any comments or questions.

Continue Reading

Email Contact Form w/ PHP

Posted on 03. Jun, 2009 by admin.

0

Welcome to our very first tutorial - a simple PHP driven contact form.

This tutorial will create two different files, a contact.php page that will contain the contact form, and send_contact.php which will process the information and send the email.

We will start with send_contact.php. Open a new file in your favorite text editor and name it send_contact.php. Copy and paste the following code into this file, and edit it as necessary. I have explained each segment in comments, and will give more details below the code.

<?php

//Checks to see if form was submitted
if(isset($_POST['submit'])) {

//This variable holds the email address the information will be sent to
$to = “contact@yoursite.com”;

//This will be the subject of the email
$subject = “Contact From Website”;

//This is the value of “name” taken from the contact form
$name = $_POST['name'];

//This is the value of “phone” taken from the contact form
$phone = $_POST['phone_num'];

//This is the value of “email” taken from the contact form
$customer_mail = $_POST['customer_mail'];

//This will be the main message of the email, taken from contact form
$message = $_POST['message'];

//This combines the collected information and displays it on separate lines
$body = “From: $name\n E-Mail: $customer_mail\n Phone: $phone\n Message - \n $message;

//This will inform the submitter if the email was sent or if it failed.
echo “Data has been submitted to $to!”;
mail($to, $subject, $body);
} else {
echo “Error!”;
}
?>

Each of the variables come together to form the email. The “$to” variable determines which email account the message will be sent to. The “$subject” variable declares the subject of the email, this will be the first thing you see when the email is received. The next four variables, $name, $phone, $customer_mail, and $message are the values collected from the contact form that is created in the next part. The “$body” variable joins the collected values together and seperates them on different lines with “\n”. Finally the script sends the information to the email account or reports an error message if unsucessfull.

The next part will be the actual form that a site visitor can fill out. Open a new file in your favorite text editor and name it contact.php, or anything you prefer (a PHP extension is not required). The script for this is the following:

<form name=”form1″ method=”post” action=”send_contact.php”>
<p style=”padding-left:10px; padding-top:10px”>
Name<br />
<input name=”name” type=”text” /><br />
Email<br />
<input name=”customer_mail” type=”text” /><br />
Phone<br />
<input name=”phone_num” type=”text” /><br />
Message<br /><textarea name=”detail” style=”height: 73px; width: 192px”>
<br />
<p style=”text-align:center” ><input name=”Reset1″ type=”reset” value=”reset” /><input name=”submit” type=”submit” value=”submit” /></p>
</form>

You will see that “action” calls the first file we created, send_contact.php. If you named the file anything else, before to change the value of action to the file name. If you wish to add or edit input fields the process is very simple. Give a name to each input field and match that name to a variable in the send_contact.php file. For example, if you created a new field for an address, you would name that field “address”, then add $address = $_POST[address]; to the send_contact.php file. To ensure that it is included in the email being sent, you would change the “$body” variable to $body = “From: $name\n E-Mail: $customer_mail\n Phone: $phone\n Address: $address\n Message - \n $message”; and it is as simple as that. I know that this guide does not include security features, they will be added in the near future.

Thanks for taking the time to read this tutorial. If you have any questions or comments feel free to leave them.

Continue Reading

5 random items w/ PHP

Posted on 03. Jun, 2009 by admin.

0

Today we are going to create a “5 Random Items” script that uses PHP and MySQL to display 5 random items from a database. This script can be useful for displaying things like random websites, affiliate banners, or anything else you can come up with.

For this tutorial, I am going to use “Random Links” as my theme.

To start we will create a table in a database. If you already have a populated table, you can skip this step, and edit the code to best suit your needs. Execute the following SQL query:

CREATE TABLE `links` (
`id` int(10) NOT NULL auto_increment,
`url` text NOT NULL,
`title` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;

This snippet of code creates a new table in our database and names it links.
In this table we insert 3 columns: id, url, and title.
id will be our PRIMARY KEY, which means there will always be unique id for every row in the table. We use auto_increment to let mysql automaticly give the id when no value has been entered.

Next we will run an SQL query to insert some rows into our table.

insert into links values (”,”http://www.davasso.com“, “Davasso Web Design“);
insert into links values (”,”http://www.pixel2life.com“, “Pixel 2 Life“);
insert into links values (”,”http://www.thepiratebay.org“, “The Pirate Bay“);
insert into links values (”,”http://www.blogspot.com“, “Blogspot“);
insert into links values (”,”http://www.wordpress.org“, “WordPress“);
insert into links values (”,”http://www.byteknights.blogspot.com“, “Byte Knights“);

Now create a new file, I will call mine “links.php”. If you are simply adding this feature to a pre-existing page, add the following code to that page.

First we will connect to the database using the following snippet.

<?php
$user =“username”;
$password = “password”;
$database = “database”;
$host = “localhost”;
mysql_connect($host,$user,$password); @mysql_select_db($database) or die ( “Unable to select database!”);

You must replace “username”, “password” and “database” with your own information. As for the host, “localhost” can almost always stay the same. Of your database is on a different server than your website, you will have to change this to path of your database.

Now we will add two more lines of code.

$number = 5 ;

$result = mysql_query (”SELECT * FROM links ORDER BY RAND() LIMIT $number“);

You can change the number to be displayed by changing the value of $number to anything you wish. After that we set the value of $result using the premade function “mysql_query” to get the data from the database.

Finally we will add the part that actually displays the items.

while ($row = mysql_fetch_array($result))
{
echo “<a href=” . $row["url"] . “><br>”
.$row["name"]. “</a>” .” “;
}
?>

This snippet basically just creates a new link using the data of each row. As long as there is data in the database, it will display it.

And here is the full code:

<?php
$user =“username”;
$password = “password”;
$database = “database”;
$host = “localhost”;
mysql_connect($host,$user,$password); @mysql_select_db($database) or die ( “Unable to select database!”);

$number = 5 ;

$result = mysql_query (”SELECT * FROM links ORDER BY RAND() LIMIT $number“);

while ($row = mysql_fetch_array($result))
{
echo “<a href=” . $row["url"] . “><br>”
.$row["name"]. “</a>” .” “;
}
?>

If you have any questions or comments feel free to share them.

Continue Reading