QuickSilva
Jan 16 2007, 07:35 PM
Hello and welcome to the tutorial of creating a simple user system! In this tutorial we will be using sessions in order to keep the user logged in. All passwords in the database will be encrypted with MD5. Ok first of all run this SQL on your database:CODE CREATE TABLE `users` ( `id` int(11) NOT NULL auto_increment, `username` varchar(250) NOT NULL default '', `password` varchar(250) NOT NULL default '', `email` varchar(250) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
As you see above we have 4 fields: ID: The ID will basically keep the ID of the user. This ID adds 1 for everytime a new row is created (basicly someone registering) Username: Pretty self explanatory, keeps the username of the user. Password: Again pretty self explanatory, this keeps the password of the user. For the passwords we will be encrypting them with MD5. This will keep them secure so that no-one can get in to there account by hacking the database or other means. Email: This is the email address for the user. ----------------- Ok now we move onto db.php. This file will allow us to connect to the database and will hold some other things in here aswell, such as the function for checking if the user is logged in. I have explained as well as I could in the code it's self. Please take time to read this, as it will make you understand the code more. CODE <?php session_start(); /*This will start our sessions. Without this the sessions will not run. This must be at the very top of the page.*/ $connection = mysql_connect("localhost","DB USER","DB PASS"); /* Basicly above this is what is happening. It is going to connect to the database using them credentials. Edit DB USER and DB PASS to what you have set it to be in cPanel. Localhost should only be changed if instructed by your host to do so, normaly if you have a dedicated mysql server. */
mysql_select_db("DB NAME") or die(mysql_error());
/* This selects the database and if it can't, it will display the error that mysql outputs. */
$username = $_SESSION['username']; /* This is assigning the variable $username to the session. */
$password = $_SESSION['password']; /* This is assigning the variable $password to the session */
$id = $_SESSION['id']; /* This is asigning the variable $id to the session */
function checklogin($error_str){ /* This sets the functions checklogin. We have one parimeter called $error_str. This is the error to be put if there not logged in */ /* This function will check if the user is logged in */ $checklogin = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password' AND id = '$id'")); /* This is the second check. It checks if there actualy in the database with the information by the sessions*/
if ((!isset($username)) || (!isset($password)) || (!isset($id))){ /* Checks if the sessions are set. This is the first login check. */ die($error_str); }elseif($checklogin == 0){ /* If the database said it can't find them anywhere */ die($error_str); } /* Closes the elseif */ } /*Closes the function */ unset($checklogin, $password, $id); ?>
----------------- Right next we are going to make register.php. This is the form where the user can register up to the user system. Again I will try and comment it as much as possible so you understand it more. CODE <?php session_start(); /*Start the session as always*/
include("db.php"); /*Here we are including the database file in order to connect to the database*/
if ($_POST['submit']){ /*If they have clicked the submit button*/ /* On the next few lines we will set a variable for what they have put. Remember variables always begin with a $. */
$post_username = $_POST['username'];
$post_password = $_POST['password'];
$md5_password = md5($post_password);
$post_email = $_POST['email'];
$usernamecheck = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username = '$post_username'")); /* Check if the username has already been taken */
if ((!$post_username) || (!$post_password) || (!$post_email)){ /* if they have missed a field */
echo "You have missed a field!";
}elseif($usernamecheck != 0){ /* If the username has already been taken */
echo "This username has already been taken!";
}else{ /* Username hasnt been taken and all fields filled in, let's carry on! */
mysql_query("INSERT INTO users (`id`, `username`, `password`, `email`) VALUES ('', '$post_username', '$md5_password', '$post_email')"); /* Adds them to the database */
echo "You have registered!";
}
}else{ /* If the form hasn't been submitted... */ ?> <html> <head> <title>Register</title> </head> <body> <!-- Starts the form --> <form style="margin: 0px; display: inline;" method="post"> <!-- Starts the layout table --> <table cellpadding="2" cellspacing="0" border="1" width="31%" align="center"> <tr> <td colspan="2" align="center"><strong>Register</strong></td> </tr> <tr> <td width="50%" align="right">Username:</td> <td width="50%"><input type="text" name="username"></td> </tr> <tr> <td align="right">Password:</td> <td><input type="password" name="password"></td> </tr> <tr> <td align="right">Email:</td> <td><input type="text" name="email"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Register!"></td> </tr> <!-- Ends the layout table --> </table> <!-- Ends the form --> </form> </body> </html> <? } ?>
----------------- The next page is login.php so the user can actualy login CODE <?php session_start(); /* Starts the session as always */
include("db.php"); /* Includes database file so we can connect */
if ($_POST['submit']){ /* If they have clicked submit */
$post_username = $_POST['username']; /* The username they filled in */
$post_password = md5($_POST['password']); /* The password they filled in. Notice the md5. This will encrypt our password so we can match it up to the database */
$numrows = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username = '$post_username' AND password = '$post_password'"));
if ((!$post_username) || (!$post_password)){
echo "You have missed a field!";
}elseif($numrows == 0){
echo "Incorrect username or password!";
}else{
$fetcharray = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE username = '$post_username' AND password = '$post_password'")); /* We are selecting the row from the database which is theres in order to get there id. This will put it all into arrays eg. $fetcharray[id] is what we are going to use. */
$_SESSION['username'] = $post_username; /* Notice theyre the other way round? This is because we are setting a session. */
$_SESSION['password'] = $post_password;
$_SESION['id'] = $fetcharray[id];
echo "You are now logged in!";
}
}else{ /* They didn't submit the form. */
?> <html> <head> <title>Login</title> </head> <body> <form method="post" style="margin: 0px; display: inline;"> <table cellpadding="2" cellspacing="0" border="1" width="31%" align="center"> <tr> <td colspan="2" align="center"><strong>Login</strong></td> </tr> <tr> <td width="50%" align="right">Username:</td> <td width="50%"><input type="text" name="username"></td> </tr> <tr> <td align="right">Password:</td> <td><input type="password" name="password"></td> </tr> <tr> <td align="center" colspan="2"><input type="submit" name="submit" value="Login"></td> </tr> </table> </form> </body> </html> <? } ?>
----------------- Ok finally I will show you how to make a page where they are required to be logged in. You can name this page what you want CODE <?php session_start();
include("db.php"); /* Includes the database */
checklogin("You need to be logged in to access this page!"); /*Error if they are not logged in and also this checks if they are.*/ ?> Your page here for logged in people!
----------------- Thank you very much for reading this and I hope you have learnt something. If you ever need help reply here or message me. Check back for part 2 coming soon!
Reply
delivi
Jan 24 2007, 08:02 PM
how do I display the login form when the user visits a page that need login.
Reply
detportal
Jan 25 2007, 04:56 AM
Is that one of those Central Authentication Systems, where you login once and get authenticated by all the applications you have installed that require logins? Because that's what I've been after for a LOONG time, and I've posted here about it too. So IS it one of those systems, and if it is, THANK YOU SO MUCH! but how do you get programs to intergrate with it?
Reply
alex1985
Mar 8 2008, 11:11 AM
You called the topic itself like ...Part 1, if there is a second part pf the titorial?!
Reply
Recent Queries:--
php creating a user system - 113.95 hr back. (1)
Similar Topics
Keywords : php user creating basics user- Create Dynamic Html/php Pages Using Simple Vb.net Code
- Taking your application data, and creating a webpage for others to vie (1)
- Getting Started With Amfphp And Rias
- first steps in creating RIAs (2)
AMFPHP in a short way is a library of php files that let u manage in JUST ONE FILE what u would do
in many files like for example queries to mysql. So u can have tons of queries to mysql and all of
them in just ONE FILE! so what is a RIA? a RIA is a Rich Internet Application commonly given
to flash applications, not the animations or presentations we see daily on the internet but very
useful and cool programs made in flash. the fisrt step u need to take is to download the AMFPHP
library directly from its site at www.amfphp.org, in some free hosted sites the latest...
How To Start Your First Game Project
- The first steps to creating your own game! (0)
Ok, I see alot of questions around the game development forums of people wanting to make their first
game, but they don't know where to start. So, I decided to write this tutorial, having been in
the same situation before. NOTE: This Tutorial: -Is meant to help you get past that initial bout of
developer's block -Does not teach you how to make a game, just help you start -Cannot help you
if you cannot answer the questions asked So, let's get to it, shall we? How to start
your first game project: --So, the first thing you need to make sure yo...
Simple Login In Visual Basic 6
- user interaction example trough login programm (6)
First of all, I am NOT a programmer, this is something my friend taught me. It describes basic
interaction with the user, while showing basic functionality of this simple programm. So, without
further ado, we're off to the tutorial: First of all, start your visual basic, when prompted
for new project, select Standard Exe . Next, we need to open code window, so we can start typing
the program. This can be done in two ways, one is double clicking on the form, or selecting Code
from View menu. If you double clicked on the form, you will see following text: CODE ...
Getting Started With Mysql
- creating tables and insert data into them. (2)
Hi in this tutorial you will learn how to create tables and insert items into them. First steps are
to create the database - go into your cpanel and mysql databases, from there make an account and a
database and then attach them together with all priviliges, call the database test and the account
admin, with the pw as pass - or any other password. We need to connect to the database so first in
your php file (probably named index.php) - this is how to do it. CODE
mysql_connect("localhost", "admin", "pass") or
die(mysql_error(&...
Delete Files And Directories Using Php
- following up from creating and writing (7)
How To Delete Files and Directories follow up from creating them Hello all and
welcome to my second tutorial involving file management. In my previous tutorial , I explained how
to create, write and read files. In this tutorial I'll explain how to remove the files and
directories you took so long to create. I did not explain last time how to create directories as I
did not know, now I do, you can use the mkdir() function. Now with this tutorial.... Removing
Files Removing files can easily be done with the unlink() function: CODE <? un...
Simple User System
- php, mysql driven (19)
Hey! Maybe you've seen my other tutorials...or my signature.. Anyways I'm going to show
you how to make a system so users of your site could register accounts and you could have protected
- user only - pages on your site /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
alt="smile.gif" /> Ok, so we start by creating a config.php file. CODE <?php
$dbhost = 'database host'; $dbname = 'database name';
$dbusername = 'database username'; $dbuserpass = 'database pas...
Creating A Timer Program
- Using Visual Basic 2005 (8)
This tutorial will explain how to create a basic timer using Visual Basic Express 2005. If you
don't have it, it's free and you can dowload it from Microsoft's website. All you need
is a few minutes to sit down and read this and a version of Visual Basic. OK, so what will this
timer actually do? Well, you are able to enter a number of minutes and a message, and then click a
button. Once the timer is up, your message pops up and you are reminded! So, basically it's
a little reminder system. I use it to remind me when TV programmes start, when I have to...
Creating Navigation For Html Websites
- Have a common navigation menu for the whole website! (12)
Pre-requisite: HTML, inline frame tags 1 Attachment(.zip) included. Updates : 29-12-07: Doctype
added in example files (Advised by jlhaslip) Designing a whole website takes a lot of planning
and organization. Designing a proper navigation system is a basic step in building your website. If
you are developing webpages in html you would have observed that as you go on creating pages it
becomes difficult to maintain the links to the pages. This article will guide you in developing a
common navigation menu for your website. It describes three ways, so if you don'...
User Permission Function [php]
- Determining User Permissions (3)
There have been several recent request for methods to restrict access to various pages on a web-site
based on User Permissions in sites that have a Login System in place. Here is some code showing one
method to restrict access by providing a sub-set of your site's links based on the User's
permissions. In this demonstration, I have defined a string for each 'level of user'
determining which set(s) of links they can view on your site. Normal MySql procedure would be to
read the User's permission level from a record in the database. For the purpose of ...
Css Basics No.2
- More basics of css (0)
Well, if you found the first one useful, here you go. CSS Basics No. 2 Description Welcome to
CSS Basics No. 2! In Basics No. 1 you learned how to write a simple CSS document. In this
tutorial you will learn to apply what is in your CSS document to your webpages! Try It Out
With CSS you have two methods of incorporating CSS into your webpages: an internal stylesheet, or an
external stylesheet. First, we will cover the internal style-sheet. The Internal Stylesheet The
internal stylesheet allows you to edit the entities of a single page. This is still q...
Want-to-start Html Tutorials
- An HTML tutorial that covers the basics (2)
NOTE: Don't use a rich text editor* for writing HTML code! Use Notepad in Windows,
SimpleText in Mac or TextEdit in OSX, but you must set the following preferences for the HTML code
to work! In the Format menu, select Plain Text. Open the preferences window from the Text
Edit menu, then select "Ignore rich text commands in HTML files." Start Creating Your Own HTML
File You can either use HTM or HTML file extensions. For more information, visit:
http://filext.com/file-extension/htm for HTM or http://filext.com/file-extension/html for HT...
Css Basics
- Just for those who know nothing about css yet. (6)
A tutorial on some of the basics of css.... I wrote if for my site, may as well post it here too
/tongue.gif" style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> CSS Basics
Description For those of you that don't know, CSS stands for Cascading Style Sheets. The whole
point of it is to easily edit all of the pages on your website, so you don't have to edit each
on individually. In this tutorial, you will learn some of the basics. Try It Out Css Syntax has
three parts to it. The object (where you want to edit), the property (what part ...
Php Variable Basics
- Where would PHP programmers be without the variable? Learn all about t (9)
CODE <?php $var= "This is a variable"; echo $var; ?> Outcome This is
a variable Using what we know about the echo function, we can see that we have created a
variable, and then used the echo command to display it. This is fairly simple, and can have many
uses, but there is much more too it! CODE <?php $var1 = "This is variable
one"; $var2 = "This is variable two"; $varall = "$var1. $var2.
You just combined three variables in one!"; echo $varall; ?> Outcome This is...
Creating A Resume
- 10 Tips For Making A Resume (1)
I've been working on my Resume for months now. Here is a summary of what I've learned: 1.
Avoid referring to yourself via 1st person or 3rd person terms. Rather than saying "I started this
job in" just say "Began job in"... Employers expect Resumes to be professional and avoid reference
to oneself; and instead speaking in an impersonal tone that presents
achievements/skills/experience/education without personalization. Avoid words like "I", "my", "he",
"she", etc. Leave out personal pronouns and only use the action words/verbs. This also includes
your Ob...
Programming In Glut (lesson 4)
- Creating 3D objects (0)
Lesson 4 of 6. I hope you are enjoying them /laugh.gif" style="vertical-align:middle"
emoid=":lol:" border="0" alt="laugh.gif" /> . QUOTE Hello, in this tutorial we will be creating
a 3D pyramid. We are building this tutorial from Lesson 3, but I took out the 2D objects and placed
a 3D pyramid in there instead. The 3rd axis for drawing can be a litle confusing, but after you get
the hand of it you'll do fine. Now when you are setting a 3D vertex just remember that the
camera is on the positive end of the z axis. So things that have a more positive z axis va...
Programming In Glut (lesson 1)
- Creating a windwo (0)
This is the first of six lessons I am transferring from Astahost for programming in GLUT, and after
the six I hope to make more, I hope you enjoy. QUOTE Hello, I'm starting a series on how to
program in OpenGL using the OpenGL Utility Toolkit, a.k.a. GLUT. I chose GLUT because it is quick
and easy to write, and very easy to learn. In this tutorial I am going to teach you how to create a
basic window which we will build off of in later tutorials. Throughout the tutorial I will leave
notes to let you know what each command does, and how you can modify it to fit...
Creating Your Own Icon
- (23)
It is easy to create your own icon, just pick a bitmap (.bmp) file and change its extension to .ico.
To do so, open the Windows Explorer, click on the View menu (or Tools in WinMe), click Folder
Options, click View tab, remove the check on the "Hide file extensions for known files types"
option, and then click OK. Select a bitmap file, press F2 key, and then change its extension to
.ico. Have fun learning:)...
How To Play The Guitar And Basics For Any Instrument
- introductory musical instruction focused on the guitar (0)
So, you've had your guitar for years and it just sits around collecting dust. But you don't
have the money to get lessons and you don't think you can play anyway. Well, most likely you
can play, all you need is practice and little tolerance for pain in the first two weeks. Pain? Why
pain? Pain because you must condition your fair fingertips for the constant bombardment of pressure
from metal strings. (Nylon strings are a bit easier and I like them, but it is best to practice on
the worst strings possible...accoustic, high action and thick gauge so that ...
A Guide To Css And Creating A Stylesheet
- (15)
Table of Contents: I. Introduction II. Starting your stylesheet --A. Starting syntax with
font-family --B. Defining classes --C. Using classes III. The STYLE tag IV. Comments in CSS V. The
"a" tag VI. A quick list of common attributes VII. Notes --A. Universal classes --B. Grouping --C.
Multiple instances VIII. Finding other attributes IX. Closing I. Introduction Firstly, to begin
using a stylesheet, you must have one. Open up your text editor and save as (something).css. I know
NotePad doesn't need quotes for a stylesheet, but I'm not sure about other progr...
Tutorial: Creating Custom Icons For Devices
- Give that device a great icon every time you plug it in! (0)
Ok for this tutorial I will use the PSP as an example of the device, this should work for every
device. (THIS WILL NOT HARM ANYTHING AT ALL!) First off, open notepad and type in:
ICON=ICONE.ICO Save that as AUTORUN.inf Now, find a picture you like.. make sure its dimensions
are 16X16 pixels. Put it in the main folder of the device, for example, my psp is located at
F:\, i would type in F:\ to the adress bar and put the picture in that folder, as it is
the folder your computer automatically reads, anyways, put your picture in there and name it: ICO...
Creating A Simple Image Viewer
- Using Visual Basic 2005 Express Edition (3)
I downloaded Microsoft's Visual Studio Express suite a few months ago, but only recently got
around to installing it. I have been practising with Visual Basic and making some rather basic
programs and utilities, but they contain most of the basic concepts. This tutorial will explain how
to create a basic image viewer, and I will try to explain each step from beginning to end as clear
as I can. To start you will need: Microsoft Visual Studio About 10-20 minutes free time OK,
first open up the Visual Basic part of the Studio. I am using the 2005 Express version, so...
Learning Vb
- Part 1 -- The Basics (5)
NOTE: This guide assumes you know how to operate your compiler, meaning create objects and forms.
This guild will only teach you the scripting. Almost all program users and game players alike want
to create their own software, whether it being a massive game, an easy-to-use calendar, or maybe
even a hack. However, they must master, or at least know much about a programming language. If
you've ever seen a Source Code , a text code written by a programmer and execute tasks by a
computer, you'll know how much time and effort it takes to learn these nevertheless ...
Php - The Basics
- Learn the basics of PHP (44)
I should point out that I originally put this tutorial together for another site, but decided
against it and to place it here instead (because I love you guys... ~sniffle, wipe a tear away~).
This tutorial should help get you started if you are just setting out into the big bad world of eb
development. I have been coding in PHP for a little over a year now, and have worked on several
large projects in the language - but whilst I certainly don't know everything about it, I would
like to share some of the basics that hopefully will give you a bit of a kick start. ---...
How To "lock Down" A Os X User Account
- Crude but effective way to maintain Macs (1)
Here's a quick summary of how one can configure OS X for use in public labs running Panther
(10.3). It should also work with Tiger (10.4) but I dunno. There may be better ways, but this is
quick and cheap: 1. Install OS X fresh, or boot up your new Mac, and set the username to
MacAdmin or the like. This is now the administrator account which users should never touch.
Share this password only with trusted admins authorized to muck with critical systems. 2.
Install all the software you expect anyone to need in the default folders (usually Applicat...
Hiding User Account On Xp
- (0)
Hopefully someone will find this useful. Ive done this on my machine as well. To a hide an account
to display on the welcome screen of xp here is a nifty way: 1. Open regedit 2. Browse to
Hkey_local_machine> software>microsoft>windows nt>winlogon>special Accounts>Userlist Here you will
see a list of all accounts on XP which are hidden and dont display on welcome screen. Yes XP does
create useless accounts as you see there. 3. Go on the right pane, right click, create a new dword
value with its name as the exact username you want to hide and its value as "0" without t...
How To: Change An Image When A User Clicks On It
- using both php and javascript (11)
How To: Change An Image When A User Clicks On It using both php and javascript - a powerful
combination I have seen quite a few how tos offering a method of doing this but none of which
resembled my method of making use of both php and javascript. This code is fairly repetitive and
most of the functions are easy to pick-up if you haven't heard of them before. Here it is...
Create your two images. Call them anything you like, you'd just need to change their filenames
in $imgano $imgayes. In fact with this script you can easily create more tha...
Creating Rollovers With Buttons
- Short & simple javascript tutorial that shows you how. (2)
Javascript in action can render some very neat visual effects, which can make your website more
appealing, and sometimes even easier to navigate. Among the most common effects are the
'button' effect, and the mouseover effect. The buttons are very common, of course; if
nowhere else, most of us have seen them at the end of forms (Click the 'submit' button to
proceed...). The basic idea is to have a 'depressible' object, which can give you the
illusion that you're 'pressing' it when you click it. The rollover effect causes
something to h...
Creating Personal Alarm
- To remind you something (2)
Creating Personal Alarm This personal alarm is very useful to remind you something or anything you
would like it to do. Use Task Scheduler application to create your personal alarm. First, click on
Start -> Settings -> Control Panel -> Scheduled Tasks. Now click on the Add Scheduled Task wizard
in the Task Scheduler folder. Click Next, click the Browse button, and then select your favorite
sound files (WAV/MP3/MIDI). Set the file to open as the task daily. Set it at the top of the hour
every day, open up its advanced properties. On the Schedule tab press the Advance...
Creating Your Own Php News System
- (23)
Hello, heres a simple tutorial from a script that I made to power my news system. It is written
withthe PHP coding system and consists of 8 files using a flatfile based system, without MySQL
databases. This should be usefull for those who want a simple little news manager and like to have
simplcity without the fancy date strings and sutff. You can see a demo of it at my site @
http://www.xeek.trap17.com . Let's Start! First let's start with the easy stuff,
making the directory first, first create a main directory to hold everything, call this folder "ne...
Looking for Php:, Simple, User, System, Part, 1
|
|
Searching Video's for Php:, Simple, User, System, Part, 1
|
advertisement
|
|