Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
LEARN HOW TO CREATE A CMS PHP SITE FOR BIGINERS.
06-23-2014, 12:18 AM (This post was last modified: 06-25-2014 03:21 AM by Tunzwap.)
Post: #1
LEARN HOW TO CREATE A CMS PHP SITE FOR BIGINERS.
Learn how to build a complete content
management system using PHP and MySQL in just
a few hours. Full code download included.
View Demo »
Download Code
Building a content management system can seem
like a daunting task to the novice PHP developer.
However, it needn't be that difficult. In this tutorial I'll
show you how to build a basic, but fully functional,
CMS from scratch in just a few hours. Yes, it can be
done!
Along the way, you'll learn how to create MySQL
databases and tables; how to work with PHP objects,
constants, includes, sessions, and other features; how
to separate business logic from presentation; how to
make your PHP code more secure, and much more!
Before you begin, check out the finished product by
clicking the
View Demo button above. (For security
reasons this demo is read-only, so you can't add,
change or delete articles.) You can also click the
Download Code button above to download the
complete PHP code for the CMS, so you can run it on
your own server.
For this tutorial, you'll need to have the Apache web
server with PHP installed, as well as the MySQL
database server running on your computer. Setting all
this up is beyond the scope of the tutorial, but a
really easy way to do it is simply to install
XAMPP on
your computer.
The feature list
Our first job is to work out exactly what we want
our CMS to do. The CMS will have the following
features:
The homepage, listing the 5 most recent articles
The article listing page, listing all articles
The "view article" page, letting visitors see a single
article
Front end:
Back end:
Admin login/logout
List all articles
Add a new article
Edit an existing article
Delete an existing article
Each article will have an associated headline,
summary, and publication date.
Planning it out
Here are the steps we'll need to follow to create our
CMS:
1. Create the database
2. Create the articles database table
3. Make a configuration file
4. Build the Article class
5. Write the front-end index.php script
6. Write the back-end admin.php script
7. Create the front-end templates
8. Create the back-end templates
9. Create the stylesheet and logo image
This page contains all the code for the CMS, ready for
you to copy and paste into your own files. If you
don't want to create the files yourself, simply
download the
finished zip file , which contains all the
code files and folders.
Ready? Grab a cup of tea, and let's get coding!

Step 1: Create the database
The first thing we need to do is create a MySQL
database to store our content. You can do this as
follows:
1. Run the mysql client program
Open a terminal window and enter the
following:
mysql -u username -p
Then enter your MySQL password when
prompted.
username should be a user that has permission
to create databases. If you're working on a
development server, such as your own
computer, then you can use the
root user for
this, to save having to create a new user.
2. Create the database
At the mysql> prompt, type:
create database cms;
Then press Enter.
3. Quit the mysql client program
At the mysql> prompt, type:
exit
Then press Enter.
That's it! You've now created a new, empty database,
into which you can put your database tables and
content.
Some web server setups let you create databases via
a web-based tool such as cPanel or Plesk (in fact
sometimes this is the only way to create MySQL
databases). If you're not sure what to do on your
server, ask your tech support team for help.
Step 2: Create the articles database table
Our simple CMS has just one database table: articles.
This, as you'd imagine, holds all of the articles in the
system.
Let's create the schema for the table. A table's
schema describes the types of data that the table
can hold, as well as other information about the
table.
Create a text file called tables.sql somewhere on your
hard drive. Add the following code to the file:
DROP TABLE IF EXISTS articles;
CREATE TABLE articles
(
id smallint unsigned NOT NULL auto_
increment,
publicationDate date NOT
NULL, # When the article was
published
title varchar(255) NOT NULL,
# Full title of the article
summary text NOT
NULL, # A short summary of
the article
content mediumtext NOT
NULL, # The HTML content of the
article
PRIMARY KEY (id)
);
The above code defines the schema for the articles
table. It's written in SQL, the language used to create
and manipulate databases in MySQL (and most other
database systems).
Let's break the above code down a little:
1. Create the articles table
DROP TABLE IF EXISTS articles removes any
existing articles table (and data — be careful!) if
it already exists. We do this because we can't
define a table with the same name as an
existing table.
CREATE TABLE articles ( ) creates the new
articles table. The stuff inside the parentheses
defines the structure of the data within the
table, explained below...
2. Give each article a unique ID
We're now ready to define our table structure.
A table consists of a number of
fields (also
called columns). Each field holds a specific type
of information about each article.
First, we create an
id field. This has a smallint
unsigned (unsigned small integer) data type,
which means it can hold whole numbers from 0
to 65,535. This lets our CMS hold up to 65,535
articles. We also specify the
NOT NULL attribute,
which means the field can't be empty (null) —
this makes life easier for us. We also add the
auto_increment attribute, which tells MySQL to
assign a new, unique value to an article's id
field when the article record is created. So the
first article will have an
id of 1, the second will
have an id of 2, and so on. We'll use this unique
value as a handle to refer to the article that we
want to display or edit in the CMS.
3. Add the publicationDate field
The next line creates the publicationDate field,
which stores the date that each article was
published. This field has a data type of
date,
which means it can store date values.
4. Add the title field
Next we create the title field to hold each
article's title. It has a data type of varchar(255),
which means it can store a string of up to 255
characters.
5. Add the summary and content fields
The last 2 fields, summary and content, hold a
short summary of the article and the article's
HTML content respectively. summary has a
text
data type (which can hold up to 65,535
characters) and
content has a mediumtext data
type (which can hold up to 16,777,215
characters).
6. Add the primary key
The last line inside the CREATE TABLE statement
defines a key for the table. A key is also called
an index, and in simple terms it makes it
quicker to find data in the table, at the expense
of some extra storage space.
We make the
id field a PRIMARY KEY. Each
table can only have a single PRIMARY KEY; this
is the key that uniquely identifies each record in
the table. In addition, by adding this key, MySQL
can retrieve an article based on its ID very
quickly.
Now that we've created our table schema, we need
to load it into MySQL to create the table itself. The
easiest way to do this is to open up a terminal
window and change to the folder containing your
tables.sql file, then run this command:
mysql -u username -p cms < tables.sql
...where username is your MySQL username. cms is
the name of the database that you created in Step 1.
Enter your password when prompted. MySQL then
loads and runs the code in your
tables.sql file,
creating the articles table inside the cms database.
You can also use a web-based admin tool such as
phpMyAdmin to run your tables.sql code and create
the table. phpMyAdmin comes pre-installed with most
web hosting accounts.
Step 3: Make a configuration file
Now that you've created your database, you're ready
to start writing your PHP code. Let's start by creating
a configuration file to store various useful settings for
our CMS. This file will be used by all the script files in
our CMS.
First, create a cms folder somewhere in the local
website on your computer, to hold all the files
relating to the CMS. If you're running XAMPP then the
local website will be in an
htdocs folder inside your
XAMPP folder. Or, if you prefer, you can create a
brand new website just for your CMS, and put all the
files in that new website's document root folder.
Inside the cms folder, create a file called config.php
with the following code:

[font color=red]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
ini_set( "display_errors", true );
date_default_timezone_set( "Australia/Sydney" ); //
http://www.php.net/manual/en/timezones.php
define( "DB_DSN",
"mysql:host=localhost;dbname=cms" );
define( "DB_USERNAME", "username" );
define( "DB_PASSWORD", "password" );
define( "CLASS_PATH", "classes" );
define( "TEMPLATE_PATH", "templates" );
define( "HOMEPAGE_NUM_ARTICLES", 5 );
define( "ADMIN_USERNAME", "admin" );
define( "ADMIN_PASSWORD", "mypass" );
require( CLASS_PATH . "/Article.php" );
function handleException( $exception ) {
echo "Sorry, a problem occurred. Please try later.";
error_log( $exception->getMessage() );
}
set_exception_handler(
'handleException' );
?>
[/font]

Let's break this file down:
[center]1. Display errors in the browser
The ini_set() line causes error messages to be
displayed in the browser. This is good for
debugging, but it should be set to
false on a live
site since it can be a security risk.
2. Set the timezone
As our CMS will use PHP's date() function, we
need to tell PHP our server's timezone
(otherwise PHP generates a warning message).
Mine is set to
"Australia/Sydney" — change this
value to your local timezone.
3. Set the database access details
Next we define a constant, DB_DSN, that tells
PHP where to find our MySQL database. Make
sure the
dbname parameter matches the name
of your CMS database (cms in this case). We
also store the MySQL username and password
that are used to access the CMS database in the
constants
DB_USERNAME and DB_PASSWORD.
Set these values to your MySQL username and
password.
4. Set the paths
We set 2 path names in our config file: CLASS_
PATH, which is the path to the class files, and
TEMPLATE_PATH, which is where our script
should look for the HTML template files. Both
these paths are relative to our top-level
cms
folder.
5. Set the number of articles to display on the
homepage
HOMEPAGE_NUM_ARTICLES controls the
maximum number of article headlines to display
on the site homepage. We've set this to 5
initially, but if you want more or less articles,
just change this value.
6. Set the admin username and password
The ADMIN_USERNAME and ADMIN_PASSWORD
constants contain the login details for the CMS
admin user. Again, you'll want to change these
to your own values.
7. Include the Article class
Since the Article class file — which we'll create
next — is needed by all scripts in our application,
we include it here.
8. Create an exception handler
Finally, we define handleException(), a simple
function to handle any PHP exceptions that
might be raised as our code runs. The function
displays a generic error message, and logs the
actual exception message to the web server's
error log. In particular, this function improves
security by handling any PDO exceptions that
might otherwise display the database username
and password in the page. Once we've defined
handleException(), we set it as the exception
handler by calling PHP's set_exception_handler()
function.
This exception handler is a bit of a quick and
dirty shortcut to keep the tutorial as simple as
possible. The "proper" way to handle exceptions
is to wrap all the PDO calls within
Article.php in
try ... catch blocks.[/center]
Visit this user's website Find all posts by this user
Like Post Quote this message in a reply
06-23-2014, 04:12 AM
Post: #2
RE: LEARN HOW TO CREATE A CMS PHP SITE
nice tutorial bro.
Find all posts by this user
Like Post Quote this message in a reply
06-23-2014, 09:46 AM
Post: #3
RE: LEARN HOW TO CREATE A CMS PHP SITE
Thank. more still on the way.
Please invite your friends to master~land
Visit this user's website Find all posts by this user
Like Post Quote this message in a reply
06-24-2014, 07:49 AM
Post: #4
RE: LEARN HOW TO CREATE A CMS PHP SITE
Can u send me this all tutorial .. Plz brother ... Post it at pagg2.x20.asia
Find all posts by this user
Like Post Quote this message in a reply
06-24-2014, 10:10 AM (This post was last modified: 06-24-2014 10:35 AM by Tunzwap.)
Post: #5
RE: LEARN HOW TO CREATE A CMS PHP SITE
copy it by yourself am nt going to do it for you

Code:
welcome guy
Visit this user's website Find all posts by this user
Like Post Quote this message in a reply
09-21-2014, 10:49 AM
Post: #6
RE: LEARN HOW TO CREATE A CMS PHP SITE FOR BIGINERS.
hi can you post a tutorial about creating own forum script
Visit this user's website Find all posts by this user
Like Post Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)