I take no credit for this script, i got it off techtuts.com, but i put it together into 1 easy tutorial.. This will allow you to make a pm system and admin system within the log in, it will also have a profile hingy aswell ''/ This is really easy ;P you can see an example of it by clicking the link below
Please bere in mind that even though i didnt write this it took me a long time. I put 4 basic tutorials together into 1 huge tutorial..
www.crisphosting.net/~habbopro/Login.php
Username: Test
Pass: test
If on the first time you log in it only displays the word members, press back and relogin..
This tutorial is in 4 parts:
Part 1: User System
Part 2: Who is online?
Part 3: Private Messaging System
Part 4: Admin system
Right here we go:
Part 1
Create a database, this can be anything you like. Then make a Username and pass for the database and add it to the database.. This is pretty easy..
Then go into phpmyadmin, and add this mySQL Query:
Now we need a file to connect to the database with.PHP Code:CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`email` varchar(40) NOT NULL default '',
`msn` varchar(250) NOT NULL default 'Not Specified',
`aim` varchar(250) NOT NULL default 'Not Specified',
`location` varchar(36) NOT NULL default 'Not Specified',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
Make a file called config.php and add this php code. Make sure you mod it to YOUR database details:
Now, on EVERY page that you make in the rest of this tutorial, add this code to the top of every page:PHP Code:<?
ob_start(); // allows you to use cookies
$conn = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD");
mysql_select_db(DATABASE NAME) or die(mysql_error());
//fill in the above lines where there are capital letters.
$logged = MYSQL_QUERY("SELECT * from users WHERE id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
$logged = mysql_fetch_array($logged);
//the above lines get the user's information from the database.
?>
This starts the database(I think you may need to only add it to the pages that you want to login on.. But just to be sure..PHP Code:<?
ob_start();
include("config.php");
?>
Coding the Pages
Create a file named Register.php, and add this code..
Now that people can register, we need to make it so that they can login...PHP Code:<?php
ob_start();
// allows you to use cookies
include("config.php");
//gets the config page
if ($_POST[register]) {
// the above line checks to see if the html form has been submitted
$username = $_POST[username];
$password = $_POST[pass];
$cpassword = $_POST[cpass];
$email = $_POST[emai1];
//the above lines set variables with the user submitted information
if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL) {
//checks to make sure no fields were left blank
echo "A field was left blank.";
}else{
//none were left blank! We continue...
if($password != $cpassword) {
// the passwords are not the same!
echo "Passwords do not match";
}else{
// the passwords are the same! we continue...
$password = md5($password);
// encrypts the password
$checkname = mysql_query("SELECT username FROM users WHERE username='$username'");
$checkname= mysql_num_rows($checkname);
$checkemail = mysql_query("SELECT email FROM users WHERE email='$email'");
$checkemail = mysql_num_rows($checkemail);
if ($checkemail>0|$checkname>0) {
// oops...someone has already registered with that username or email!
echo "The username or email is already in use";
}else{
// noone is using that email or username! We continue...
$username = htmlspecialchars($username);
$password = htmlspecialchars($password);
$email = htmlspecialchars($email);
// the above lines make it so that there is no html in the user submitted information.
//Everything seems good, lets insert.
$query = mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')");
// inserts the information into the database.
echo "You have successfully registered!";
}
}
}
}
else
{
// the form has not been submitted...so now we display it.
echo ("
<center>
<form method=\"POST\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\"><br />
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"pass\"><br />
Confirm Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"cpass\"><br />
Email: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"emai1\"><br />
<input name=\"register\" type=\"submit\" value=\"Register\">
</form>
</center>
");
}
?>
Create a file named Login.php and add this code to it.. Remember to mod the bit at the bottom to YOUR site.
This makes it so that when people login, they can edit their profile, view all the members and logout. Please be aware that these wont work yet, as we havent made the files ''/PHP Code:<?
oB_start();
// allows you to use cookies.
include("config.php");
if (!$logged[username])
{
if (!$_POST[login])
{
echo("
<center><form method=\"POST\">
<table>
<tr>
<td align=\"right\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\">
</td>
</tr>
<tr>
<td align=\"right\">
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\">
</td></tr><tr>
<td align=\"center\">
<input type=\"submit\" name=\"login\" value=\"Login\">
</td></tr><tr>
<td align=\"center\">
<a href=\"register.php\">Register Here</a>
</td></tr></table></form></center>");
}
if ($_POST[login]) {
// the form has been submitted. We continue...
$username=$_POST['username'];
$password = md5($_POST[password]);
// the above lines set variables with the submitted information.
$info = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if($data[password] != $password) {
// the password was not the user's password!
echo "Incorrect username or password!";
}else{
// the password was right!
$query = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$user = mysql_fetch_array($query);
// gets the user's information
setcookie("id", $user[id],time()+(60*60*24*5), "/", "");
setcookie("pass", $user[password],time()+(60*60*24*5), "/", "");
// the above lines set 2 cookies. 1 with the user's id and another with his/her password.
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://yoursite.com\"/>Thank You! You will be redirected");
// modify the above line...add in your site url instead of yoursite.com
}
}
}
else
{
// we now display the user controls.
echo ("<center>Welcome <b>$logged[username]</b><br /></center>
- <a href=\"editprofile.php\">Edit Profile</a><br />
- <a href=\"members.php\">Member List</a><br />
- <a href=\"logout.php\">Logout</a>");
}
?>
THIS is where we make the edit profile page, members can write a little profile on themselves, if you want to add more options to the profile page, simple message me and i'll help you out..
Create a file named editprofile.php and add this code:
Viewing members and their profilesPHP Code:<?
ob_start();
include("config.php");
if ($logged[username])
{
// the user is logged in! We continue...
if (!$_POST[update])
{
// the form hasn't been submitted. We continue...
$profile = mysql_query("SELECT * from users where username = '$logged[username]'");
$profile = mysql_fetch_array($profile);
// the above lines get the information so that it can be displayed in the html form.
echo("
<center><form method=\"POST\">
<table width=\"100%\">
<tr>
<td align=\"right\" width=\"25%\">
Location
</td>
<td align=\"left\">
<input type=\"text\" size=\"25\" maxlength=\"25\" name=\"locate\" value=\"$profile[location]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
MSN Messenger
</td>
<td align=\"left\">
<input size=\"25\" name=\"msn\" value=\"$profile[msn]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
AOL Messenger</td>
<td align=\"left\">
<input size=\"25\" name=\"aim\" value=\"$profile[aim]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
Email Address</td>
<td align=\"left\">
<input size=\"25\" name=\"email\" value=\"$profile[email]\"></td>
</tr>
<tr>
<td align=\"center\">
</td>
<td align=\"left\">
<input type=\"submit\" name=\"update\" value=\"Update\"></td>
</tr>
</table>
</form>
</center>");
}
else
{
$email = htmlspecialchars($_POST[email]);
$aim = htmlspecialchars($_POST[aim]);
$msn = htmlspecialchars($_POST[msn]);
$locate = htmlspecialchars($_POST[locate]);
// the above lines get rid of all html.
echo ("Your profile has been updated!");
$update = mysql_query("Update users set email = '$email',
msn = '$msn', aim = '$aim', location = '$locate' where username = '$logged[username]'");
// updates the information in the database.
}
}
else
{
// They aren't logged in!
echo ("<a href=\"login.php\">You must login</a>");
}
?>
Create a file named members.php and add this code:
This will show every member that has registered, and their profile.PHP Code:<?
ob_start();
include("config.php");
if (!$_GET[user])
{
$getuser = mysql_query("SELECT * from users order by id asc");
while ($user = mysql_fetch_array($getuser))
{
// gets all the users information.
echo ("<a href=\"members.php?user=$user[username]\">$user[username]</a><br />\n");
// links to a page to view the user's profile.
}
}
ELSE
{
$getuser = mysql_query("SELECT * from users where username = '$_GET[user]'");
$usernum = mysql_num_rows($getuser);
if ($usernum == 0)
{
echo ("User Not Found");
}
else
{
$profile = mysql_fetch_array($getuser);
echo ("<center><b>$profile[username]'s Profile:</b><br /></center>
MSN Messenger: $profile[msn]<br />
AIM Messebger: $profile[aim]<br />
Location: $profile[location]<br />
Email: $profile[email]");
// in the above code, we display the user's information.
}
}
?>
Now we need to make where members Logout, create a file named Logout.php and add this code:
On pages that you want to be members only ( Only people who are logged in will be able to view the page ) Add this code to the top of the page:PHP Code:<?
ob_start();
setcookie("id", 2132421,time()+(60*60*24*5), "/", "");
setcookie("pass", loggedout,time()+(60*60*24*5), "/", "");
echo ("You are now logged out!");
?>
If you want to put the login system into a content box on a main website, simple add this bit of simple code:PHP Code:<?
ob_start();
include("config.php");
if ($logged[username])
{
echo("You can put html and stuff in here.</font>");
}
else
{
echo("You are not logged in");
}
?>
Part 2: Showing who is online..PHP Code:<?
include("login.php");
?>
We will have to add a new field to our "users" database table. Go into phpmyadmin and execute this query. (To do this navigate to the tab that says "SQL" in your database.)
Add this code:
Now create a file called online.php and add this code:PHP Code:alter table `users` add online varchar(12)
This will show who is online etc.. 300 means 5 minsPHP Code:<?
include("config.php");
$offline = 300; //How long is considered online?
$current = time(); //gets the time on the server (unformatted)
$offline = ($current-$offline); //
if ($logged[username])
{
$update = mysql_query("UPDATE users set online = '$current' where username = '$logged[username]'");
//the above line sets the time the user was online to the current time
}
?>
You will have to add the following code to every page you want to log online users on:
In the area that you want to show who is online, add this code (the other code merely adds the previous online.php code to the page:PHP Code:<?
include("online.php");
?>
Part 3: Private Messaging SystemPHP Code:<?
include("config.php");
$offline = 300; // 5 minutes
$current = time(); //gets the current time
$offline = ($current-$offline); //the cutoff time for being considered online
$getusers = mysql_query("SELECT * from users where online >= '$offline'");
//gets the users that are online
while ($users = mysql_fetch_array($getusers))
{
echo ("$users[username], ");
//displays the online users
}
?>
Ok, we need to go back into phpmyadim and run this query:
Now, create a file called messages.php and add this code:PHP Code:CREATE TABLE `pmessages` (
`title` varchar(255) NOT NULL default 'Untitled Message',
`message` text NOT NULL,
`touser` varchar(255) NOT NULL default '',
`from` varchar(255) NOT NULL default '',
`unread` varchar(255) NOT NULL default 'unread',
`date` date NOT NULL default '0000-00-00',
`id` int(15) NOT NULL auto_increment,
`reply` varchar(15) NOT NULL default 'no',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
This is what will be the private messaging system which will allow you to send a private message to any member on the member list.PHP Code:<?
ob_start();
//the above line needs to be above ALL HTML and PHP (except for <?).
include("config.php");
//gets the config page, which connects to the database and gets the user's information
if ($logged[username])
{
//checks to see if they are logged in
switch($_GET[page])
{
//this allows us to use one page for the entire thing
default:
echo ("- <a href=\"messages.php?page=inbox\">Inbox</a><br />
- <a href=\"messages.php?page=write\">New Message</a>");
break;
case 'write':
if (!$_POST[send])
{
//the form hasnt been submitted yet....
echo ("<form method=\"POST\" style=\"margin: 0px;\">
<dl style=\"margin: 0px;\">
<dt>recipient</dt>
<dd>
<select name=\"to\">
");
$getusers = mysql_query("SELECT * FROM users ORDER BY 'username' ASC");
while ($users = MySQL_Fetch_Array($getusers)) {
echo ("<option value=\"$users[username]\">$users[username]</option>");
}
//the above line gets all the members names and puts them in a drop down box
echo ("
</select>
</dd>
<dt>Message Subject</dt>
<dd><input type=\"text\" name=\"subject\" size=\"20\"></dd>
<dt>Message</dt>
<dd><textarea rows=\"7\" name=\"message\" cols=\"35\"></textarea>
</dd><dt> </dt>
<dd><input type=\"submit\" value=\"Submit\" name=\"send\"></dd>
</dl>
</form>
");
}
if ($_POST[to])
{
//the form has been submitted. Now we have to make it secure and insert it into the database
$subject = htmlspecialchars(addslashes("$_POST[subject]"));
$message = htmlspecialchars(addslashes("$_POST[message]"));
$to = htmlspecialchars(addslashes("$_POST[to]"));
//the above lines remove html and add \ before all "
$send = mysql_query("INSERT INTO `pmessages` ( `title` , `message` ,
`touser` , `from` , `unread` ,
`date` ) VALUES ('$subject', '$message', '$to',
'$logged[username]', 'unread', NOW())");
echo ("Your message has been sent.");
}
break;
case 'delete':
if (!$_GET[msgid])
{
echo ("Sorry, but this is an invalid message!");
}
else
{
$getmsg = mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'");
$msg = mysql_fetch_array($getmsg);
//hmm..someones trying to delete someone elses messages! This keeps them from doing it
if ($msg[touser] != $logged[username])
{
echo ("This message was not sent to you!");
}
else
{
$delete = mysql_query("delete from pmessages where id = '$_GET[msgid]'");
echo ("Message Deleted");
}
}
break;
case 'inbox':
$get = mysql_query("SELECT * from pmessages where touser = '$logged[username]' order by id desc");
echo("
<table bgcolor=\"#dddddd\" border=\"0\" width=\"100%\" cellspacing=\"0\">
<tr>
<td align=\"center\">Subject</td>
<td align=\"center\" width=\"125\">From</td>
<td align=\"center\" width=\"97\">Date</td>
<td width=\"25\">Delete</td>
</tr>
</table>
");
$nummessages = mysql_num_rows($get);
if ($nummessages == 0)
{
echo ("You have 0 messages!");
}
else
{
echo("<table border=\"0\" width=\"100%\" cellspacing=\"1\">");
while ($messages = mysql_fetch_array($get))
{
//the above lines gets all the messages sent to you, and displays them with the newest ones on top
echo ("
<tr>
<td><a href=\"messages.php?page=view&msgid=$messages[id]\">");
if ($messages[reply] == yes)
{
echo ("Reply to: ");
}
echo ("$messages[title]</a></td>
<td width=\"125\">$messages[from]</td>
<td width=\"97\">$messages[date]</td>
<td width=\"25\"><a href=\"messages.php?page=delete&msgid=$messages[id]\">Delete</a></td>
</tr>");
}
echo ("</table>");
}
break;
case 'view':
//the url now should look like ?page=view&msgid=#
if (!$_GET[msgid])
{
//there isnt a &msgid=# in the url
echo ("Invalid message!");
}
else
{
//the url is fine..so we continue...
$getmsg= mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'");
$msg = mysql_fetch_array($getmsg);
//the above lines get the message, and put the details into an array.
if ($msg[touser] == $logged[username])
{
//makes sure that this message was sent to the logged in member
if (!$_POST[message])
{
//the form has not been submitted, so we display the message and the form
$markread = mysql_query("Update pmessages set unread = 'read' where id = '$_GET[msgid]'");
//this line marks the message as read.
$msg[message] = nl2br(stripslashes("$msg[message]"));
//removes slashes and converts new lines into line breaks.
echo ("
<form method=\"POST\" style=\"margin: 0px;\">
<dl style=\"margin: 0px;\">
<dt><b>$msg[title] -- From $msg[from]</b></dt>
<dd>$msg[message]</dd>
<dt><b>Reply</b></dt>
<dd><textarea rows=\"6\" name=\"message\" cols=\"45\"></textarea></dd>
<dt> </dt>
<dd><input type=\"submit\" value=\"Submit\" name=\"send\"></dd>
</dl></form>");
}
if ($_POST[message])
{
//the form HAS been submitted, now we insert it into the database
$message = htmlspecialchars(addslashes("$_POST[message]"));
$do = mysql_query("INSERT INTO `pmessages` ( `title` , `message` , `touser` , `from` , `unread` ,
`date`, `reply`) VALUES
('$msg[title]', '$message', '$msg[from]', '$logged[username]',
'unread', NOW(), 'yes')");
echo ("Your message has been sent");
}
}
else
{
//hmm..this message was NOT sent to the logged in user...so we won't display it.
echo("<b>Error</b><br />");
echo ("This message was not sent to you!");
}}
break;
}
echo("<br /><br /><div align=\"center\"><b><a href=\"?page=inbox\">Inbox</a> · <a href=\"?page=write\">New Message</a></b>");
}
?>
Now we need to modify login.php so that we can add the link to the private messaging system. Find this code:
And change it to:PHP Code:echo ("<center>Welcome <b>$logged[username]</b><br /></center>
- <a href=\"editprofile.php\">Edit Profile</a><br />
- <a href=\"members.php\">Member List</a><br />
- <a href=\"logout.php\">Logout</a>");
This will add the link..PHP Code:$new = mysql_query("select * from pmessages where unread = 'unread' and touser = '$logged[username]'");
$new = mysql_num_rows($new);
echo ("<center>Welcome <b>$logged[username]</b><br /></center>
- <a href=\"editprofile.php\">Edit Profile</a><br />
- <a href=\"members.php\">Member List</a><br />
- <a href=\"messages.php\">Private Message Center ($new new)</a><br />
- <a href=\"logout.php\">Logout</a>");
You now have a Private Messaging system..
Part 4: Admin system
This is rather complicated..
Information
This system has 2 user levels, but it is easy to add new ones. The levels are:
1 -- This is for members.
5 -- This is for administrators.
You can use levels 2-4 for moderators, tutorial staff--whatever you want.
We need to add a new field to the users table. Go to phpmyadmin and run this query by pasting it into the sql tab.
Change your 'level' to 5 using phpmyadmin, to do this, All u have to do is go into php my admin go to the users table and press browse then click the pencil looking thing then look forPHP Code:ALTER TABLE `users` ADD level int( 1 ) default '1'
Level it will say 1, just change it to 5.
Now we need to add the code which allow the user admin to edit other peoples profiles..
Create a file named admin.php and add this code:
Extension...PHP Code:<?
ob_start();
include("config.php");
if($logged[username] && $logged[level] ==5)
{
//checks to see if the user is logged in, and if their user level
//is 5 (this is administrator)
if($_GET[user])
{
//checks to see if there is a ?user=username variable in the url.
if (!$_POST[update])
{
// the form hasn't been submitted. We continue...
$user = mysql_query("SELECT * from users where username = '$_GET[user]'");
$user = mysql_fetch_array($user);
//these lines get the user's information and put it in an array.
//we will display the information in the html form
echo("
<div align=\"center\"><form method=\"POST\">
<table width=\"100%\">
<tr>
<td align=\"right\" width=\"25%\">
User Level
</td>
<td align=\"left\">
<input type=\"text\" size=\"25\" maxlength=\"25\" name=\"level\"
value=\"$user[level]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
Location
</td>
<td align=\"left\">
<input type=\"text\" size=\"25\" maxlength=\"25\" name=\"locate\"
value=\"$user[location]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
MSN Messenger
</td>
<td align=\"left\">
<input size=\"25\" name=\"msn\" value=\"$user[msn]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
AOL Messenger</td>
<td align=\"left\">
<input size=\"25\" name=\"aim\" value=\"$user[aim]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
Email Address</td>
<td align=\"left\">
<input size=\"25\" name=\"email\" value=\"$user[email]\"></td>
</tr>
<tr>
<td align=\"center\">
</td>
<td align=\"left\">
<input type=\"submit\" name=\"update\" value=\"Update\"></td>
</tr>
</table>
</form>
</div>");
//displays the html form
}
else
{
$email = htmlspecialchars($_POST[email]);
$aim = htmlspecialchars($_POST[aim]);
$msn = htmlspecialchars($_POST[msn]);
$locate = htmlspecialchars($_POST[locate]);
$level = htmlspecialchars($_POST[level]);
// the above lines get rid of all html.
echo ("$_GET[user]'s profile has been updated.");
$update = mysql_query("Update users set email = '$email',
msn = '$msn', aim = '$aim',
location = '$locate', level = '$level' where username = '$_GET[user]'");
// updates the information in the database.
}
}
else
{
$getusers = mysql_query("Select * from users order by username asc");
while($users = mysql_fetch_array($getusers))
{
//makes a list of all the users
echo("<a href=\"admin.php?user=$users[username]\">$users[username]</a><br />");
//displays the user's names
}
}
}
else
{
//the user's level is not 5! They cannot view this page
echo("Sorry, but you are not allowed to view this page!");
}
?>
You can make other pages using this code.
That concludes the tutorial.. Please note that this is for advanced members only... Yet is really easy to make.. You must know how to use phpmyadminPHP Code:<?
ob_start();
include("config.php");
if($logged[username] && $logged[level] == 5)
{
echo("Welcome $logged[username]. Your level is 5");
}
else
{
echo("You are not logged in, or your level is not 5!");
}
?>
this tutorial was from www.techtuts.com it was in four seperate parts which i put together and made thisI started it at 18:01 so i guess it didnt take em that long to put together
Unless you have a good knowledge of php DONT mod anything.. If you need aa hand pm me and i will be happy to help
![]()






I started it at 18:01 so i guess it didnt take em that long to put together 
Reply With Quote




