[Tutorial] Creating a basic user control panel
#1

Introduction

Hi all,
After one-two week if finshed my UCP, most of it learn from phpAcademy (On *******, visit the!)
and I decide to make tutorial on this forum because I see a lot of people need it! It's pretty easy, just a few steps...
Let's start!


Features
  • Login form
  • Showing user stats
  • Logout form
Starting our user control panel

First of all, be sure that you have XAMPP (Or WAMP, I use XAMPP) and create in htdocs folder called eq. ucp
In that folder we will creat our files!



Page Name: config.php
Use: Connecting to MySQL server and selecting the database
Language: PHP

PHP код:
<?php
    $connect 
mysql_connect("localhost""root""") or die("I can't connect to the server!"); //connecting to mysql, change setting if you have to
    
mysql_select_db("sa:mp") or die ("I can't find the database!"); //selecting database, change name to your database name!
    
    
function sanitize($string//function for sanitize from xss and mysql and csrf... Thanks to XFlawless
    

        
$string strip_tags($string); 
        
$string mysql_real_escape_string($string); 
        return 
$string
    }
?>
Page Name: login.php you can also call it index.php if you want just login and showing stats in your ucp...
Use: Creating form for loging user
Language: PHP

PHP код:
<?php
include("config.php"); //including our config.php where is connecting to mysql...
session_start(); //starting session for profile.php (Dunno how to explain better) look little down
error_reporting(0); //without this we will always get some stupid notice that variable isn't defined....
$submit $_POST['submit']; //variable for submit button, in this variable we save button that player press in <input type='submit' name="submit" value='Login' />....
$username sanitize($_POST['username']); //variable for username, in this variable we save text that user type in <input type="text" name="username"....
$password sanitize($_POST['password']); //variable for password, in this variable we save text that user type in <input type="password" name="password"....
if($submit//if he press submit button
{    
    if(
$username && $password//if he type both of username and password not just one of them
    
{
        
$query mysql_query("SELECT username, password FROM users WHERE username = '$username'"); //selecting user name and password, change it to your field names,  chage users to your table name, $username means username that he type...
        
if(mysql_num_rows($query) == 1//if user exists
        
{
            while(
$row mysql_fetch_assoc($query)) //loop thought table that we select in mysql_query
            
{
                
$dbusername $row['username']; //setting dbusername as variable from table, change 'username' to your field!
                
$dbpassword $row['password']; //setting dbpassword as variable from table, change 'password' to your field!
            
}
            if(
$username == $dbusername && $password == $dbpassword//if username is same as one from table and if password is the same as one from table...
            
{
                
$_SESSION['username'] = $dbusername//setting session username to one from table, this is useful if you login, that restart your browser and than you go in url where is your profile.php... Anyway this is useful :D
                
echo header('location: profile.php'); //redirecting user to his profile page (profile.php)
            
}
            else echo 
"Wrong password!"//else if user type wrong password he will get this...
        
}
        else echo 
"Username doesn't exist!"//if username doesn't exist in table user will get this
    
}
    else echo 
"Type name and password!"//else if user doesn't type all fields he will get this...
}
?>
Put this in bottom of login.php!
Код HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic UCP</title>
//here you can add your design (Remove this line)
<form action='login.php' method='POST'> 
<input type="text" name="username" value='<?php echo $username?>'/> 
<input type="password" name="password"/> 
<input type='submit' name="submit" value='Login' /> 
</form>
</head>
</html>
Page Name: logout.php
Use: Logout user
Language: PHP

PHP код:
<?php
include("config.php"); //including our config.php
session_start(); //starting session
session_destroy(); //destroying it
header('location: login.php'); //redirecting user to login.php
?>
Page Name: profile.php
Use: Showing users stats...
Language: PHP

PHP код:
<?php
include("config.php"); //including our config.php
session_start(); //starting session
error_reporting(0);
if(isset(
$_SESSION['username'])) //if session is set, so if user is logged in...
{
    
$username $_SESSION['username']; //setting variable username as one from session
    
$query mysql_query("SELECT * FROM users WHERE username = '$username'");  //selecting all from table users where username is name that your is loged in
    
echo "Welcome ".$_SESSION['username']; //saying welcome to user!
    
while($row mysql_fetch_assoc($query)) //looping thousgt table to get informations
    
{
        
$name $row['username']; //selecting user name, change 'username' to your field name
        
$money $row['money']; //selecting user money, change 'money' to your field name
        
$score $row['score']; //selecting user score, change 'score' to your field name
        
$kills $row['kills']; //selecting user kills, change 'kills' to your field name
        
$deaths $row['deaths']; //selecting user deaths, change 'deaths' to your field name
    
}
    echo 
"<br><br>Name: ".$name."<br> Money: ".$money."<br> Score: ".$score."<br> Kills: ".$kills."<br> Deaths: ".$deaths;
}
else 
header('location: login.php'); //if user isn't loged in it will redirect him on login.php
?>
Put this in bottom of profile.php!
Код HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic UCP</title>
</head>
</html>
Pictures

Ok, here are some pictures of my user control panel...

Login
Profile
Reply


Messages In This Thread
Creating a basic user control panel - by System64 - 17.10.2011, 09:00
Re: Creating a basic user control panel - by Lorenc_ - 17.10.2011, 09:25
Re: Creating a basic user control panel - by Sinner - 17.10.2011, 09:28
Re: Creating a basic user control panel - by System64 - 17.10.2011, 09:29
Re: Creating a basic user control panel - by Sinner - 17.10.2011, 09:31
Re: Creating a basic user control panel - by System64 - 17.10.2011, 09:39
Re: Creating a basic user control panel - by [HiC]TheKiller - 17.10.2011, 09:42
Re: Creating a basic user control panel - by System64 - 17.10.2011, 10:02
Re: Creating a basic user control panel - by Lorenc_ - 17.10.2011, 10:17
Re: Creating a basic user control panel - by System64 - 17.10.2011, 10:19
Re: Creating a basic user control panel - by GloomY - 17.10.2011, 10:20
Re: Creating a basic user control panel - by System64 - 17.10.2011, 10:33
Re: Creating a basic user control panel - by System64 - 17.10.2011, 18:21
Re: Creating a basic user control panel - by Baboon - 17.10.2011, 20:25
Re: Creating a basic user control panel - by System64 - 17.10.2011, 20:27
Re: Creating a basic user control panel - by XFlawless - 18.10.2011, 10:46
Re: Creating a basic user control panel - by System64 - 18.10.2011, 11:05
Re: Creating a basic user control panel - by XFlawless - 18.10.2011, 12:59
Re: Creating a basic user control panel - by System64 - 18.10.2011, 17:49
Re: Creating a basic user control panel - by DiNorscio - 05.04.2012, 09:23
Re: Creating a basic user control panel - by System64 - 05.04.2012, 09:34
Re: Creating a basic user control panel - by Krunsy - 21.04.2012, 11:32
Re: Creating a basic user control panel - by System64 - 21.04.2012, 13:08
Re: Creating a basic user control panel - by Krunsy - 23.04.2012, 12:46
Re: Creating a basic user control panel - by Ballu Miaa - 23.04.2012, 17:53
Re: Creating a basic user control panel - by System64 - 23.04.2012, 19:58
Re: Creating a basic user control panel - by tiernantheman - 23.04.2012, 20:00
Re: Creating a basic user control panel - by Elysian` - 23.04.2012, 21:51
Re: Creating a basic user control panel - by Ballu Miaa - 24.04.2012, 02:39
Re: Creating a basic user control panel - by System64 - 24.04.2012, 12:08
Re: Creating a basic user control panel - by Claude - 24.04.2012, 15:58
Re: Creating a basic user control panel - by System64 - 24.04.2012, 16:36
AW: Creating a basic user control panel - by xOFxK1LLER - 25.04.2012, 15:32
Re: Creating a basic user control panel - by 3ventic - 26.04.2012, 16:10
Re: Creating a basic user control panel - by Ballu Miaa - 26.04.2012, 16:21
Re: Creating a basic user control panel - by SwiftKidZ - 02.05.2012, 04:12
Re: Creating a basic user control panel - by Gertin - 02.05.2012, 11:11
Re: Creating a basic user control panel - by System64 - 02.05.2012, 11:20
Re: Creating a basic user control panel - by Sanady - 28.09.2012, 14:13
Re: Creating a basic user control panel - by marine - 29.09.2012, 18:06
Re: Creating a basic user control panel - by Mikkel - 08.10.2012, 11:18
Re: Creating a basic user control panel - by ZackBoolaro - 08.10.2012, 11:29
Re: Creating a basic user control panel - by Roel - 25.10.2012, 06:59
Re: Creating a basic user control panel - by gtakillerIV - 25.10.2012, 12:33
Re: Creating a basic user control panel - by Dugzor - 30.09.2013, 20:18
Re: Creating a basic user control panel - by DanishHaq - 30.09.2013, 20:24
Re: Creating a basic user control panel - by logoster - 01.12.2013, 18:04
Re: Creating a basic user control panel - by BizzyD - 01.12.2013, 18:14
Re: Creating a basic user control panel - by thefatshizms - 01.12.2013, 22:16
Re: Creating a basic user control panel - by System64 - 06.12.2013, 19:43
Re: Creating a basic user control panel - by FahadKing07 - 12.12.2013, 20:40
Re: Creating a basic user control panel - by DavidLuango - 13.12.2013, 05:42
Re: Creating a basic user control panel - by Spydah - 21.03.2014, 21:22
Re: Creating a basic user control panel - by Micko123 - 10.05.2016, 06:56
Re: Creating a basic user control panel - by Zorono - 02.06.2016, 18:37
Re: Creating a basic user control panel - by Zeus666 - 25.07.2016, 17:22

Forum Jump:


Users browsing this thread: 1 Guest(s)