PDA

View Full Version : User System Tutorial Part One



Eliterate
04-03-2007, 01:30 AM
Hey Everyone! This is the first part of the user system tutorial. This tutorial will include the following:
Mysql Tables
config.php
functions.php
register.php
login.php
logout.php
usercp.php
members.php
Alright Let's Start with making our table so far. Copy and paste this in to the sql query for phpMyAdmin.
But to learn more, I'd rather you re-write it so you get the hang of it better
CREATE TABLE `users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` TEXT NOT NULL ,
`password` TEXT NOT NULL ,
`email` TEXT NOT NULL ,
`joindate` TEXT NOT NULL ,
`ip` TEXT NOT NULL ,
`level` INT( 11 ) NOT NULL DEFAULT '1'
) ENGINE = MYISAM ;
Lets Get right to the config file, name this: config.php


<?php
ob_start();
//Tells Server we are using cookies
$dbhost = "localhost";
$dbname = "db_name";
$dbuser = "db_username";
$dbpass = "db_pass";
$connect = mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db($dbname) or die (mysql_error());
//Connect to the database, self explanatory
$loggedU = MYSQL_QUERY("SELECT * FROM users WHERE id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
$loggedU = mysql_fetch_array($loggedU);
//Used for shorter variables.
$check = mysql_query("SELECT * FROM users WHERE id='$_COOKIE[id]' AND password='$_COOKIE[pass]'");
//This will check against the users cookies
if(!$_COOKIE[id] || !$_COOKIE[pass]){
//If the cookies arnt the same
$loggedIn = 0;
//There logged in status is 0
$Uname = "Guest";
$Uid = "Guest";
//So everything is considered a guest
}else{
//if it is them
$loggedIn = 1;
//set logged in to 1
$Ulog = mysql_fetch_array($check);
//fetch the query
$Uname = $Ulog['username'];
$Uid = $Ulog['id'];
//Logged Info - shorter variables to use
}
//end else
//Global Settings
$date = date("l, F j");
$time = date("h:i:s A"); ;
$Uip = getenv('REMOTE_ADDR');
?>

Get it? Good! :P
Lets's move on to making a functions page, Name this: functions.php


<?php
function safe($string,$no_white = 1){
//Name the safe function to use in our forms
$string = htmlspecialchars(stripslashes(addslashes($string)) ,ENT_QUOTES);
//Set our string, no html, strip then add slashes,
if($no_white <= 1){
//if no white, do nothing
}else{
//else, add some
$string .= " ";
//THe string adds some
}
//end else
return $string;
//Return the string
}
//End function
//The safe function will turn out like - $variable = safe($_POST['field_name']);
function getname($userid){
//name a new function to grab names, because in this system, we wont be calling stuff by names, we will use numbers.
$getuser = mysql_query("SELECT * FROM users WHERE id='$userid'");
//get the user fro mthe database
$check = mysql_num_rows($getuser);
//Check the rows
while($r=mysql_fetch_array($getuser)){
$name = $r['username'];
$id = $r['id'];
}
//Fetches data and that
if($check == 0){
$lev = "Guest";
//If no user, level is guest and that
}else{
$lev = "<a href='?user=member&uid=$id'>$name</a>";
//If not, get the user and link to their profile
}
return $lev;
//Return the Username
}
//The getname function will turn out like - $user = getname($uid);
function switchlevel($Ulevel){
//name new function switchlevel, this will turn 1 - member or 5 - Administrator
$level = array(
"1" => "Member",
"5" => "Administrator",
);
$new_level = str_replace(array_keys($level),array_values($level ),$Ulevel);
return $new_level;
}
//End function
//The switchlevel function will go like $Ulevel = switchlevel($userlevel);
?>

Remember to make that functions page as it is crucial to have, and we will constantly be updating it.
Next lets make the registration page, name this: register.php


<?php
ob_start();
//Start Cookies
include "config.php";
//Include our database connection
include "functions.php";
//Include our functions
if (!$_POST['submit']){
//If the submit isnt pressed
echo "<form method='POST'>
Username: <input type='text' name='username'><br>
Email: <input type='text' name='email'><br>
Password: <input type='password' name='pass'><br>
Confirm Password: <input type='password' name='cpass'><br><br>
<input type='submit' name='submit' value='Process'> <input type='reset' name='reset' value='Clear Data'>
</form>";
//That is register form for the user to fill in.
}else{
//if the submit button is pressed (Else 1)
$username = safe($_POST['username']);
$email = safe($_POST['email']);
$pass = safe($_POST['pass']);
$cpass = safe($_POST['cpass']);
//get the data from our form
if ($username == NULL || $email == NULL || $pass == NULL || $cpass == NULL){
//If any fields were left blank
echo "Sorry, we cannot complete your registration because one or more fields was left blank!";
//Echo the error
}else{
//Else 2
$getname = mysql_query("SELECT `username` FROM `users` WHERE `username`='$username'") or die(mysql_error());
$checkname = mysql_num_rows($getname);
//Check for the user in the db
$getemail = mysql_query("SELECT `email` FROM `users` WHERE `email`='$email'") or die(mysql_error());
$checkemail = mysql_num_rows($getemail);
//Check for the email in the db
$getip = mysql_query("SELECT `ip` FROM `users` WHERE `ip`='$Uip'") or die(mysql_error());
$checkip = mysql_num_rows($getip);
//Check for the ip address in the db
if ($checkname != 0){
echo "Sorry, but the name you have chosen is already in use in our database, please go back and choose another!";
//If their name is taken, echo the error
}elseif ($pass != $cpass){
echo "Sorry, but the passwords you have entered do not match! Please go back and re enter them.";
//If the passwords do not match, echo the error
}elseif ($checkemail != 0){
echo "Sorry, but the email you have entered is already in use in our database, please go back and enter a new one!";
//If the email is taken, echo the error
}elseif ($checkip != 0){
echo "Sorry, but this computer has already registered, to keep it fair, one user per computer!
If you are sure no one has registered on this computer, please contact Administration.";
//If their computer is already registered, echo the error
}else{
//Else 3, if all else is good, then we submit to database
$password = md5($pass);
//md5 the password
$insert = mysql_query("INSERT INTO `users` (`username`,`password`,`email`,`ip`,`joindate`)
VALUES ('$username','$password','$email','$Uip','$date')")
or die(mysql_error());
echo "You have successfully registered, you may now login to use some of our wonderful features.";
//insert the user i nto the database, and echo the message!
}
//End else 3
}
//End Else 2
}
//End else 1
?>

Wow, that was a mouthful! XD, Let's continue on to login.php


<?php
ob_start();
//Start Cookies
include "config.php";
//Include our database connection
include "functions.php";
//Include our functions
if (!$loggedU['username']){
//If they arnt logged in
if (!$_POST['submit']){
//If they havent submitted the form

echo "<form method='POST'>
<b>Username</b>
<input type='text' name='username'>
<b>Password</b>
<input type='password' name='password'>
<input type='submit' name='submit' value='Login'>
<input type='reset' name='reset' value='Reset'>
- <a href='register.php'>Register</a>
</form>";
//Echo our login form

}else{
//if they hit the submit button
$username = safe($_POST['username']);
$password = safe($_POST['password']);
$password = md5($password);

$getuser = mysql_query("SELECT * FROM `users` WHERE `username`='$username'") or die(mysql_error());
$checkuser = mysql_num_rows($getuser);
$r = mysql_fetch_array($getuser);
//Get all the data from our form?

if ($checkuser == 0){
echo "This username does not exist in our database, please register!";
//If the user doesn;t exist, echo error
}elseif ($password != $r[password]){
echo "The password you have entered does not match the password in the database for this user!";
//If the password fro mthe db doesnt match the submitted password, echo error
}else{
setcookie("id", $r[id],time()+(60*60*24*5), "/", "");
setcookie("pass", $r[password],time()+(60*60*24*5), "/", "");
header ("Location: http://YOURSITE.com");
//Set cookies and redirect them
}
}
}else{
//Else 1
echo "Welcome $Uname, What would you like to do today?<br>
<a href='usercp.php'>User CP</a><br>
<a href='members.php'>Members</a><br>
<a href='logout.php'>Logout</a>";
}
//End Else 1
?>

Now that we can login, lets logout :P logout.php


<?php
ob_start();
setcookie("id", 2132421,time()+(60*60*24*5), "/", "");
setcookie("pass", loggedout,time()+(60*60*24*5), "/", "");
header ("Location: http://yoursite.com");
//set new cookies, then redirect the user.
?>

Want to edit your profile? usercp.php


<?php
ob_start();
//Start Cookies
include "config.php";
//Include our database connection
include "functions.php";
//Include our functions
if ($loggedU['username']){
//if they are logged in
switch ($_GET['control']){
//Makes url usercp.php?control=actionhere
default:
//set default page
echo "<a href='usercp.php?control=edit_profile'>Edit Profile</a>";
break;
//end default page
case "edit_profile":
//start edit profile page
if (!$_POST['submit']){
//If the submit button hasnt been pressed
echo "<form method='POST'>
Email: <input type='text' name='email' value='$loggedU[email]'><br>
<input type='submit' name='submit' value='Edit'>
</form>";
}else{
$email = safe($_POST['email']);
//grabs the data with the safe function
$update = mysql_query("UPDATE `users` SET `email`='$email' WHERE `id`='$loggedU[id]'");
//Update the profile, if adding more fields,
//make sure to seperate with a comma, `field`='$value', `field2`='$value2'
echo "You have updated your profile!";
//echo the success
}
//end else
break;
//end edit profile
}
//end switch function
}else{
//if they arent logged in
echo "Please register or login!";
//echo error
}
//end else
?>

We are almost done part one of this tutorial! Last page for now, members.php


<?php
ob_start();
//Start Cookies
include "config.php";
//Include our database connection
include "functions.php";
//Include our functions
switch ($_GET['control']){
//start switch functions, urls are now, members.php?control=actionhere
default:
//set default page
$getusers = mysql_query("SELECT * FROM `users` ORDER BY username ASC");
//get the users and order them alphabetically
while ($x = mysql_fetch_array($getusers)){
//make a while loop for all users
echo "<a href='members.php?control=user&uid=$x[id]'>$x[username]</a>";
//echo an url to view the users profile.
}
//end while
break;
//end default page
case "user":
$uid = safe($_GET['uid']);
//get the uid from the url
$x = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id`='$uid'"));
//get the user and already fetch the data.
$level = switchlevel($x['level']);
//get their level in words with the switchlevel function
echo "
<b>Username:</b> $x[username]<br>
<b>Email:</b> $x[email]<br>
<b>Level:</b> $level<br>
<b>Joindate:</b> $x[joindate]<br>";
//echo the info
break;
//end view profile
}
//end switch fucntion
?>

Thanks, that is part one of the user system tutorial,
keep it locked for the next parts which include, users online, message system, admin cp,
change password, forgot password.
You can also suggest tuts you want to see for this system!

Edited by Catzsy (Forum Super Moderator): Thread closed due to bumping.

wilky15
04-03-2007, 03:34 AM
*Cough Edited Version Of Techuts usersystem*

Eliterate
04-03-2007, 03:39 AM
*Cough* An actual user system that wont be so easily hacked or sql injected.

Florx
04-03-2007, 03:39 AM
yup theres a nice demo on www.habblio.com with some added stuff i coded e.g. alert, ban, badges. the rest is C&P with tweeks lol

wilky15
04-03-2007, 06:59 AM
LOOL agreed for once eliterate, i might just go along and use that usersystem. techtuts is hopeless :) ill just edit urs like crazy, i dont like that one account per ip thing. And u need last time they logged in. And and and, sessions. urs u have to login twice

Tomm
04-03-2007, 08:50 AM
Warning:

This system is not secure!

Your function safe will NOT protect from SQL injections.

Also cookies are far from secure. I could use malicious scripts to steal user's cookies.

Eliterate
04-03-2007, 03:02 PM
others said the same thing about it, theyve even tried to get on to my site, but failed...miserably.

F32
04-03-2007, 03:07 PM
This is just Techtuts?

Homosexual
04-03-2007, 03:08 PM
Correct, Xeoro!

Mentor
04-03-2007, 06:00 PM
Warning:

This system is not secure!

Your function safe will NOT protect from SQL injections.

Also cookies are far from secure. I could use malicious scripts to steal user's cookies.

tbh i think the bigger problem with the safe function is that its not actualy used on most of the sql inputs anyway o.0

Eliterate
04-03-2007, 07:01 PM
It doesnt go in a sql query. It cleans all form input.

Mentor
04-03-2007, 07:33 PM
It doesnt go in a sql query. It cleans all form input.

oh? So the SQL the main risk point is completely unprotected, you just clean inputs that are pretty much safe anyway due to the magic quotes on most php setup's o.0

Homosexual
04-03-2007, 07:45 PM
You need addslashes/cleanslashes.

Eliterate
04-03-2007, 08:35 PM
this is only set as guidelines. Dont like it, dont use it. This is why it is called tutorials.

Homosexual
04-03-2007, 08:52 PM
Thats why you ripped it :)

Eliterate
04-03-2007, 09:26 PM
Of course its going to look like its ripped because any basic register/login/config will look the same. My addons are a lot different.

wilky15
04-03-2007, 09:37 PM
Your login page is like the same as the techtuts one, the funny thing is everything ive said to you that youve ripped your responce is "I know they the guy" LOL,

Eliterate
05-03-2007, 01:10 AM
Would you like Naresh's contact details? Id be happy to give them to you in a private message.

Mentor
05-03-2007, 02:57 AM
Just wondering, How the Hell can things like this be called tutorals, There bloody copys of the Code, with NO explination or anything that comes close to teaching. These things are ment for idiots to copy and paste, not for people to learn from. They are NOT tutorals <_<.

wilky15
05-03-2007, 05:07 AM
eliterate, so because you know him that gives you the right to rip it and call it urs?

Eliterate
05-03-2007, 08:18 PM
Who said I ripped it? Naresh and I have been friends for a couple of years now. He has seen my user system tutorial and he doesn't beleive its his.

wilky15
06-03-2007, 07:01 AM
Sure dude Sure, Your **! The logout is almost identical, your login is almost identical.

Your full of yourself.

bad-dj
06-03-2007, 07:31 AM
well it dose not mater

Mentor
06-03-2007, 05:49 PM
Sure dude Sure, Your **! The logout is almost identical, your login is almost identical.

Your full of yourself.

Who cares? there both crap.

wilky15
07-03-2007, 02:17 AM
because this noob rips stuff and says he knows the owner. he ripped this hotel thing right off of haborator.org and said i know the owner. WAT A NOOB THAT GUY IS.

nets
08-03-2007, 03:44 AM
Insecure. I wouldn't recommend anyone uses that script, and quite frankly it has a pretty much identical structure to something I've seen before anyway.

sever104
11-05-2007, 03:52 PM
Says.

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 106

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 107

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 108

Anyone help me?

Edited by Catzsy (Forum Super Moderator) : Please do not bump old threads.

Blob
11-05-2007, 03:57 PM
Says.

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 106

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 107

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 108

Anyone help me?

Stupidist Bumper Award of the Week!

Matt.
11-05-2007, 03:59 PM
Says.

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 106

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 107

Warning: Cannot modify header information - headers already sent by (output started at /home/habbotex/public_html/index.php:11) in /home/habbotex/public_html/index.php on line 108

Anyone help me?

Have you created the database, user and pass? and edited the config.php file?

Want to hide these adverts? Register an account for free!