Archive

Posts Tagged ‘tutorial’

Mmmm…Cookies

August 18th, 2009 Matthew Lodge No comments

Time for a tutorial on PHP Cookies now. Every time you log into a site chances are you’re kept logged in with the help of cookies. Here I will show you a quick guide to cookies.

Syntax and rules.

setcookie(name, value, expire, path, domain);

Cookies must be set before any information is displayed on the page. So set your cookies before the tag and before you echo() or print() anything.

Ok let’s do this…

$expire=time()+60*60*24*6;
setcookie(”name”, “Matthew Lodge”, $expire);

The first part of the code sets an expiry date for the cookie. Here it is set for a week from when the cookie is set. (60 seconds, 60 minutes, 24 hours and 6 days). This cookie is called name and will have a value of Matthew Lodge.

This code is fine for setting a cookie but it will reset the cookie every time the page is refreshed. so to keep the current cookie we can use an if statement.

if(!isset($_COOKIE["name"])){
$expire=time()+60*60*24*6;
setcookie(”name”, “Matthew Lodge”, $expire);
}

This if statement checks whether the cookie exists and if it doesn’t it sets it.

Showing the value of a cookie is easy as shown below

echo $_COOKIE["name"];
//This will show the cookie that is called name

print_r “$_COOKIE”;
//This will print all the cookies in an array

To delete cookies you must set them in the past.

setcookie(”name”, ” “, time()-3600;
//Sets the cookie an hour in the past and destroys it.

So that’s cookies. Remember that some people do not have cookies enabled so always have a contingency plan if you want your application to be used by all.

Categories: PHP Tags: , ,

PHP & mySQL: An intro

August 14th, 2009 Matthew Lodge No comments

The main benefit of PHP is its compatibility with mySQL. In this tutorial you will learn the basics of PHP and mySQL.

<?php

$connection = mysql_connect(’SEVER’, ‘USER’, ‘PASSWORD’);
$selectdb = mysql_select_db(’DB NAME’);

if(!$connection){
die(’Error connecting to database server’);
}
if(!$selectdb){
die(’Error connecting to selected database’);
}

//This code connects the page to the database ready to make a query. By holding //the connection functions in variables we can create a custom error message.

?>

Next I will teach you about query syntax. A basic query follows this rule:

SELECT name FROM table

The SELECT tells mySQL to return a value from the specified field (in this example the field is called “name”). Alternatively you can use the * wild card. This requests all the information from the table selected in the FROM parameter.
The FROM tells mySQL which table in the database the field can be found.

For example to retrieve a name from a table called users the code would look like this:

$query = mysql_query(”SELECT * FROM users”);
if(!$query){
die(’Error in query’);
}

while($row = mysql_fetch_array($query)){
echo $row['firstname'];
echo “<br />”;
}

First of all we query the database for a set of results. Then we echo out all the results using a WHILE loop. The $row variable contains all the information so we specify what information we want from the query using ['']. This returns the information from that field.

The full code

<?php$connection = mysql_connect(’SEVER’, ‘USER’, ‘PASSWORD’);
$selectdb = mysql_select_db(’DB NAME’);

if(!$connection){
die(’Error connecting to database server’);
}
if(!$selectdb){
die(’Error connecting to selected database’);
}

$query = mysql_query(”SELECT * FROM users”);
if(!$query){
die(’Error in query’);
}

while($row = mysql_fetch_array($query)){
echo $row['firstname'];
echo “<br />”; //Add a line break for formatting
}

mysql_close($connection);

?>

The mysql_close() function will close the connection to the database

So as you can see PHP and mySQL can be a formidable combination. By integrating them into your site you can give users an enhanced experience.

Categories: PHP Tags: , ,