Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2007, 01:09 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
The CMS (#2) PHP

Ok so in the second part of this tutorial we are going to start creating the admin area.

Things we are going to use for this:

Sessions
If statements
Functions
MySQL database (but not yet)

Great!


Right then, let's get started with our simple login form..


login.php >
Code:
<form action="check-user.php" method="post">
<b>Username</b>:<input type="text" name="username" size="20"><br>
<b>Password</b>:<input type="password" name="pw" size="20"><br>
<input type="submit" value="Login"></form>
So i am guessing you don't need me to explain HTML to you, but just quicky.

<form action="check-user.php" method="post"> - Tells the server once this script is posted take all the stuff inside the 2 <form> tags to the url check-user.php

<input type="text" name="username" size="20"> - Text area to type your username into

<input type="password" name="pw" size="20"> - Text area to type your password into

<input type="submit" value="Login"> - Button you click to log in



-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

Right then, so now we have that we need to build our check-user.php

We are going to use sessions for this admin area, so that when a user leaves the website, they are automaticly logged off.


PHP Code:
<?php
session_start
();

include 
"../includes/connect.php";

$check="SELECT * from users where username='$username' and pw='$pw'";
$resultmysql_query($check) or die
(
"Could not execute query : $check." mysql_error());

if (
mysql_num_rows($result) == 0)
{

echo 
"<div align=center><b>Your login is wrong. Please click back and try again.</b></div>";

}
else
{
$r=mysql_fetch_array($result);
$login_username=$r["username"];
session_register("login_username");
Header("Location: admin.php");
}
?>
So lets go through this line by line:


session_start(); - telling the server we are starting a session.

include "../includes/connect.php"; - Including our connect.php that we created last tutorial.


$check="SELECT * from users where username='$username' and pw='$pw'"; - Checking the username and password are correct

$result= mysql_query($check) or die - Checks if the user & pass are correct


if (mysql_num_rows($result) == 0) - Checks if it finds no combinations of the 2..


echo "<div align=center><b>Your login is wrong. Please click back and try again.</b></div>"; - Tells the user they entered in correct information

}
else - Otherwise:
{


$r=mysql_fetch_array($result); - Fetch information place into array


$login_username=$r["username"]; - Gets their user and puts it into the variable $login_username


session_register("login_username"); - Reigsters the username

Header("Location: admin.php") - Redirects to our admin.php

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

So now what we need to do, is build the database, that is the backend of the what we were just checking.

Open up PHP my admin or any other database tool and place this code into your SQL window

CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
username VARCHAR(12),
pw VARCHAR(32))

What we did:

Created a table in the database, called users

Row1: ID, Auto increment

Primary Key is ID

Username, max charicters 12

pw max charicters 32

Or alteranatively, if you don't have access too php my admin or an alternative, then create a file called install.php in your install directory and put this code in it:

PHP Code:
<?php
include("../includes/connect.php");

// Create a MySQL table in the selected database
mysql_query("CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
 username VARCHAR(12), 
 pw VARCHAR(32))"
)
 or die(
mysql_error());  

echo 
"Admin users table created!";

?>

That will do the same thing, just in PHP.

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-


Creating our admin.php

So we need to first make sure that the user is logged in, if they are not then we need to redirect them to the login page.

We do that with the following code:

session_start();
if($login_username=="") {
Header("Location: login.php");

So if $login_username dosen't have anything in, then it redirects them to login.php

PHP Code:
<?
session_start
();
if(
$login_username=="") {
Header("Location: login.php");
} else {

echo 
'<li><a href="add-news.php">Add news</a>';
echo 
'<li><a href="edit-news.php">Edit news</a>';
echo 
'<li><a href="add-user.php">Add a user></a>';
echo 
'<li><a href="edit-user.php">Edit a user</a>';

}
?>
Ok so thats our entire admin.php

Just contains some links to the different pages that we will create.

Note we will add more links as we add more features.

End of tutorial #2

Coming up next, we are going to do the news section.

Base

Attached Files
File Type: zip PixN.zip (2.8 KB, 14 views)
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.

Last edited by Base; 03-31-2007 at 09:32 PM. Reason: Took the variable $seperateconnect out, as it was not needed
Reply With Quote
  #2 (permalink)  
Old 03-30-2007, 03:35 PM
Junior Member
 
Join Date: Mar 2007
Posts: 5
sandprince is on a distinguished road
hm....this is nice tutorial and easy too
Reply With Quote
  #3 (permalink)  
Old 03-31-2007, 09:34 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
The variable $seperateconnect should be removed from the connect.php & from the check-user.php files.

This was used in error.

Many thanks

Thomas Gray
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #4 (permalink)  
Old 03-31-2007, 11:51 PM
Turbocharged_06's Avatar
VIP Member
 
Join Date: Mar 2007
Posts: 151
Turbocharged_06 is on a distinguished road
ok now i got a small problem i get an error saying
Code:
No database selected
in my file http://php.arabian-outlaw.com/sql/install.php
__________________
OUTLAW-WEB.NET
Reply With Quote
  #5 (permalink)  
Old 04-01-2007, 01:50 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
lol, you are my un-official bug tester!

Now this works perfectly for me, but again its you that has the problem.

So try putting these lines into your install.php **NOTE THESE LINES HAVE TO BE JUST AFTER THE <? OF THE INSTALL.PHP!!!! **
PHP Code:
// The MySQL info, edit these
$host "localhost"// the database location. if you dont know it, leave it as it is
$dbuser "USER"// the database username
$dbpass "PASS"// the databases user's pass
$dbname "DATABASE NAME"// the name of the database

// The actual connection. DO NOT EDIT THESE
mysql_connect("$host","$dbuser","$dbpass"); // Using the variables stated above 
mysql_select_db("$dbname"); 
and then try running it agian.

If it still failes, then get back to me via PM, rather than spamming this thread.

Cheers

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #6 (permalink)  
Old 04-01-2007, 04:44 AM
Junior Member
 
Join Date: Apr 2007
Posts: 3
Julio Cesar is on a distinguished road
Smile Very good!!!!!!!!!

Excellent tutorial!!!!!!!!
Reply With Quote
  #7 (permalink)  
Old 04-06-2007, 04:10 PM
Junior Member
 
Join Date: Apr 2007
Posts: 10
Zika is on a distinguished road
really nice tutorial
Reply With Quote
  #8 (permalink)  
Old 04-07-2007, 09:51 PM
Junior Member
 
Join Date: Apr 2007
Posts: 11
hromero is on a distinguished road
Great

Thanks for the Tutorial!!!
Reply With Quote
  #9 (permalink)  
Old 05-04-2007, 02:19 AM
Junior Member
 
Join Date: Dec 2006
Posts: 21
nose is on a distinguished road
I'm you fan
Reply With Quote
  #10 (permalink)  
Old 05-05-2007, 08:10 AM
FallenSage2007's Avatar
Member
 
Join Date: Mar 2007
Location: USA
Posts: 43
FallenSage2007 is on a distinguished road
Quote:
Originally Posted by Base View Post

echo "<div align=center><b>Your login is wrong. Please click back and try again.</b></div>"; - Tells the user they entered in correct information
I don't know php because I wouldn't be following this tut I did. but based on the message that is sent by the this line. Shouldn't it be saying it tells you this if you entered your login info wrong? Because if you enter it right it should take you to the admin panel and not tell you that "Your login is wrong."

I don't know I thought I would point this out to know if it is typo or that's what it means. And if that's what it means could you explain it?

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 11:23 PM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com