Oct
30
Posted on 30-10-2007
Filed Under (Tutorial) by chintan

Setting and reading cookies in PHP is a part of–dare we say it?–cake. We don’t want to get into all the misinformation about cookies, but they’re important and useful. Sometimes they’re the right tool for the work.

create and modify a cookie In PHP

To create and modify a cookie, use the PHP function setcookie(). setcookie() takes up to six arguments, depending upon how much control you want over the cookie and who can read its value.

The best way of setting a cookie is like this:

setcookie(‘name’, ‘xxx’);

Then, for every further page on your site viewed by this browser (without the user quitting) you’ll have the value of ‘xxx’ stored in the variable $name for easy right to use in PHP. This type of cookie is known as a session cookie, since it lasts for the length of a user’s session.

If you want the cookie to persevere after the person exits his or her browser, you must pass setcookie() through a third parameter, the date you want the cookie to expire. Since PHP’s background springs fully formed from the head of Unix, you correspond to this time as the number of seconds since March 23, 1985. If you’re a Unix programmer, this makes total sense. But, if you’re from a Windows or a Macintosh background, you’re just trembling your head wondering if you’ll ever understand those wacky Unix folk.

PHP has a very nice function, mktime(). You pass mktime() (in this order) the hour, minute, second, month, day, and year that you want to represent, and mktime() returns to you the number of seconds since March 1, 1985. So, if you want to simulate a ZZZ meltdown:

<?php

$ZZZ = mktime(0,0,0,1,1,2000);

setcookie(‘name’, ‘xxx’, $ZZZ);

?>

your cookie will end with the millennium.

If you want to update a cookie to store a newer value, you can simply overwrite its value. So, even if you’ve previously sent the cookie above on an earlier page, it’s perfectly legal to go ahead and change your name to “akki.”

<?php

$ZZZ = mktime(0,0,0,1,1,2000);

setcookie(‘name’, ‘akki’, $ZZZ);

?>

Note that doing this doesn’t alter the value of the variable $name. It’s set when the page is loaded. If you want to make sure these two are always in sync, you can code like this:

<?php

$name = ‘Akki’;

$ZZZ = mktime(0,0,0,1,1,2000);

setcookie(‘name’, $name, $ZZZ);

?>

The next two parameters for setcookie() let you control the path and the domain of who can read your cookie. By default, only pages equivalent to or lower down in the ladder on the same server that sends the cookie can read its value. That’s for security’s sake. However, if you had an account that’s sometimes “www.any.com” but also “other.any.com,” and your account lets you serve pages from ~/myhome, you should modify setcookie() as such:

<?php

setcookie(‘name’, ‘akki’, $ZZZ, ‘~/myhome’, ‘.any.com’);

?>

The last parameter to setcookie(), which we’ve never used, teach that the cookie be sent only to a Web server that’s running a secure connection such as SSL. For this to occur, set the sixth value to 1.

Delete Cookie

Deleting a cookie is simple, simply pass setcookie() the name of your cookie and PHP will arrange for it to be deleted.

<?php setcookie(‘name’); ?>

There’s one last important item to cite about using cookies. Because of the way cookies work within HTTP, it’s important that you send all cookies before you print any text. If you don’t, PHP will give you a warning and your cookies will not be sent. So, this is OK:

<?php

setcookie(‘name’, ‘akki’);

echo “Good Morning!”;

?>

But this is not:

<?php

echo “Good Morning!”;

setcookie(‘name’, ‘akki’);

?>

(0) Comments    Read More   
Oct
30
Posted on 30-10-2007
Filed Under (Tutorial) by chintan

In PHP session variable is used to store up information about, or modify settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.

Session Variables in PHP

While you are working with an application, you open it, do some modify and then you close it. This is much like a Session. Computer knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn’t continue state.

A PHP session solves this problem by allowing you to store user information on the server for later use. However, session information is temporary and will be removing after the user has left the website. If you need a enduring storage you may want to store the data in a database.

Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is spread in the URL.

Create a PHP Session

Before you can store user information in your PHP session, you must first start up the session.

Message: The session_start() function must appear ahead of the <html> tag:

<?php session_start(); ?>

<html>

<body>

</body>

</html>

The code above will register the user’s session with the server, allow you to start saving user information, and allot a UID for that user’s session.

Store up a Session Variable

The accurate way to store and retrieve session variables is to use the PHP $_SESSION variable:

<?php

session_start();

// store session data

$_SESSION['xxx']=1;

?>

<html>

<body>

<?php

//retrieve session data

echo “Page=”. $_SESSION['xxx'];

?>

</body>

</html>

Output:

Page=1

In the example under, we make a simple page counter. The isset() function checks if the “xxx” variable has already been set. If “xxx” has been set, we can addition our counter. If “xxx” doesn’t exist, we create a “xxx” variable, and set it to 1:

<?php

session_start();

if(isset($_SESSION['xxx']))

$_SESSION['xxx']=$_SESSION['xxx']+1;

else

$_SESSION['xxx']=1;

echo “Page=”. $_SESSION['xxx'];

?>

Destroying a Session

If you wish to remove some session data, you can use the unset() or the session_destroy() function.

The unset() function is used to free the specified session variable:

<?php

unset($_SESSION['xxx']);

?>

You can also totally destroy the session by calling the session_destroy() function:

<?php

session_destroy();

?>

message: session_destroy() will reset your session and you will lose all your stored session data.

(0) Comments    Read More   
Oct
25
Posted on 25-10-2007
Filed Under (Tutorial) by chintan

SQL – What’s a Database?

A database is nothing more than an empty shell, like a empty warehouse. It offers no real functionality what so ever, other than holding a name. Tables are the next row of our tree offering a wide scope of functionality. If you follow our warehouse example, a SQL table would be the physical shelving inside our vacant warehouse. Each SQL table is capable of housing 1024 columns (shelves). Depending on the situation, your goods may require reorganization, reselling, or removal. SQL tables can be manipulated in this same way or in any fashion the situation calls for.

SQL – Four Principles of Database Design

When designing and implementing a database, keep in mind these four guidelines.

Atomicity

Your coded statements flow without the constant need to update or “fix” your data.

Consistency

Your statements are either executed 100% or fail 100%, do not implement code that partially works.

Isolation

Keep data files and logs away from public eyes, and limit the number of users with administration access to your database.

Durability

Maintain reliable servers with plenty of storage space and back-up systems that save transactions immediately.

A well thought out database will continue to serve and meet your needs for ages. It is important to plan ahead and really put some thought into what you intend to record in your databases. Keep in mind that tables and databases should maintain some relationship. In many instances it is far more desirable to have several small related tables and databases than one giant one. “Never place your eggs all in one basket.”

The Database Model

Dr. Edgar F. Codd was the founding father of what is known as the relational model of databases. In 1970, he published a groundbreaking article “A Relational Model of Data for Large Shared Data Banks.” Included within the article were 11 rules of relational databases. These rules are as follows (paraphrased).

1. Information Rule

All data in the database should be represented as values in tables.

2. Treatment of NULL Values

Null values must be treated as incomplete data pieces. Nulls are not to be confused with zeros.

3. Guaranteed Access

Every piece of data should be accesible by using the table name, primary key, and a column name.

4. Sublanguage

Having one supported language with a well-defined syntax.

5. Active-Online Relational Catalog

A database is represented at a logical level of tables.

6. Set-level Insertion, Update, and Deletion

System must support set-time insert, update, and deletion operations.

7. Data Independence (Physical)

Alterations to the way data is stored must not alter the application itself.

8. Data Independence (Logical)

Altering tables, columns, and/or rows must not alter the application itself.

9 Updating Views

All views should be updated through the system.

10. Integrity Independence

The language of the database must define integrity rules.

11. No subversion

If the system uses a low level interface to record data, then there must be a higher level interface used when administrating.

The largest of corporation follow these rules of cataloging information to this very day.

Database Driven Web Sites

Today we experience the power of databases throughout the web. Many sites are completely dynamic, meaning the content that is being display is held inside of a table with columns, and columns of information to display. This is quickly becoming the ideal way to host websites. It allows for dynamic content on the fly, user interaction, and webmasters can store information about returning users making way for the site to recognize returning users. Any chance at building rapport with your audience is a great opportunity indeed.

SQL – Platforms

A SQL platform acts as the stage for structure and developing your databases. Several different platforms exist including:

* IBM’s DB2

* MySQL

* Oracle

* Microsoft’s SQL Server

* PostgreSQL

SQL – MySQL and PostgreSQL

MySQL and PostgreSQL are open source database programs rich in functionality and flexibility. They are often the choice of web developers and small businesses simply because they get the job done for a very sensible price. Also they will go anywhere and can operate on nearly every operating system available.

Microsoft’s SQL Server is steadily on the rise in the commercial world gaining popularity slowly. This platform has a GUI “Windows” type interface and is also rich with functionality. A free trial version can be downloaded at the Microsoft web site, however it is only available to Windows users.

SQL – DB2 and Oracle

By far the selection of choice for large corporations is either Oracle or DB2. Companies that have large ties to IBM stick to their DB2 software whereas others have made the switch to Oracle. These systems run on personal computers as well as large company mainframes.

PHP / MySQL Tutorial

MySQL is at present the most popular open source database server in existence. On top of that, it is very commonly used in combination with PHP scripts to create powerful and dynamic server-side applications.

MySQL has been criticized in the past for not supporting all the features of other popular and more expensive Database Management Systems. However, MySQL continues to get better with each release (currently version 5), and it has become widely popular with individuals and businesses of many different sizes.

Why Use a Database?

Databases are most helpful when it comes to storing information that fits into logical categories. For example, say that you wanted to store information of all the employees in a company. With a database you can group different parts of your business into separate tables to help store your information logically. Example tables might be: Employees, Supervisors, and Customers. Each table would then contain columns specific to these three areas. To help store information connected to each employee, the Employees table might have the following columns: Hire, Date, Position, Age, and Salary.

Learn MySQL

Before you begin this tutorial you should have a basic knowledge of the information covered in our PHP and HTML tutorials.

This tutorial focuses heavily on using MySQL in a PHP environment. It is aimed at teaching those who have web hosts with PHP and MySQL already installed. If you are unsure, please contact your web host.

MySQL Setup Guide

The easiest way to research with MySQL and PHP is to purchase some space on a shared web host.

even if you can set up MySQL physically on your home PC, it can be rather difficult for a beginner to do, and would require more than a few lessons! If you think you’ve got what it takes, or you’re just mentally unstable, head on over to MySQL.com for more information on installing MySQL yourself.

Setting Up MySQL in Control Panel

There are many different types of control panels that your shared hosting provider may have. This tutorial assumes that you are using the most popular, ControlPanel.

First, find the link that allows you to administer MySQL. Within Control Panel the icon is labeled MySQL Databases. Once there, you will need to do the following before you can start using MySQL.

* Create a new database

* Create a new user with password

* Assign the user to the database

Helpful Tool – phpMyAdmin!

Also supplied by most hosting services is phpMyAdmin (you can also install it anywhere you want, as it’s open source and free). This tool will allow you to view all the MySQL database, tables, and entries, as well as perform SQL queries remotely through a web browser.

Although we will be teaching how to create databases, tables and all other MySQL tasks through PHP, we encourage you to learn about phpMyAdmin. It’s easy-to-use interface will allow you to do many common MySQL tasks quickly and easily, saving you many beginner headaches and helping you understand what’s going on in a more visual manner.

more Database information on www.databasejournal.com

(0) Comments    Read More   
Oct
22
Posted on 22-10-2007
Filed Under (Tutorial) by joseph

PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code.

The PHP syntax is very similar to Perl and C. PHP is often used together with Apache (web server) on various operating systems. It also supports ISAPI and can be used with Microsoft’s IIS on Windows.

  • PHP stands for PHP: Hypertext Preprocessor
  • PHP is a server-side scripting language, like ASP
  • PHP scripts are executed on the server
  • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
  • PHP is an open source software (OSS)
  • PHP is free to download and use
  • PHP files may contain text, HTML tags and scripts
  • PHP files are returned to the browser as plain HTML
  • PHP files have a file extension of “.php”, “.php3″, or “.phtml”
  • PHP combined with MySQL are cross-platform (means that you can develop in Windows and serve on a Unix platform)

(0) Comments    Read More