Loading...


bookmark - Simple User System php, mysql driven

Simple User System - php, mysql driven

 
 Discussion by friiks with 23 Replies.
 Last Update: December 6, 2011, 4:27 am
 
bookmark - Simple User System php, mysql driven  
Quickly Post to Simple User System php, mysql driven w/o signup Share Info about Simple User System php, mysql driven using Facebook, Twitter etc. email your friend about Simple User System php, mysql driven Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

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 :P

Ok, so we start by creating a config.php file.

CODE

<?php

$dbhost = 'database host';
$dbname = 'database name';
$dbusername = 'database username';
$dbuserpass = 'database password';

mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
?>

Fill in the values of your host and upload it.
Here are querie to run in PHPmyadmin

CODE

CREATE TABLE `users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 255 ) NOT NULL ,
`password` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM ;


Next, lets create index.php.

CODE

<?php
//start the session so you would stay logged in
//always must be on top
session_start();
//include config.php file
include('config.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>The site</title>
</head>
<body>
<center><a href="?p=idx">Home</a> - <a href="?p=page">Protected page</a>
<?php
$p=$_GET['p'];
//see my ?id= browsing tutorial
switch($p){
default:
//if user isn't logged in lets show him the log in form
if(!isset($_SESSION['username'])){
?>
<form action='login.php' method='POST'>
Username: <input type='text' name='username'><br>
Password: <input type='password' name='password'><br>
<input name="login" type="submit" value="Submit"><br>
Not <a href="register.php">registered</a>?
</form>
<?}
else{
//$_SESSION['username'] = the current users
//username. It will be echoed like "Hi, user!"
echo "<br><br>Hi, ".$_SESSION['username']."!";
echo "<a href='logout.php'>Log out</a>";}
break;
case 'page':
//you can use it like this or use include()
if(!isset($_SESSION['username'])){
echo '<br><br>Log in to see this page!';}else{
echo '<br><br>Only user who is logged in can see this!..and you see this so this means you are logged in;]';
}
}
?>
</center>
</body>
</html>

You see the explanations in the code.

Now we need a file that will log the user in, right? Right!
Create a file called login.php

CODE

<?php
//start session and include conf...
session_start();
include'config.php';
//get the variables from form and adding some little security
$submit=$_POST['login'];
$username = mysql_real_escape_string(strip_tags(htmlspecialchars($_POST['username'])));
$password = md5($_POST['password']);
//if submit button is pressed
if ($submit){
if((!$username) || (!$password) || ($username=='') || ($password=='')){
header("Refresh: 2;".$_SERVER['HTTP_REFERER']);
echo'<center>Please enter both - username and password!</center>';
}
//lets see if the user exists by making a query which selects
//submitted username and password from the database
//and the we use mysql_num_rows() to count the results returned
//if there is a user with a username and password like that $c will be 1
//as it will be counted otherwise it'll stay 0.
$sql=mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password`= '".$password."'") OR die(mysql_error());
$c=mysql_num_rows($sql);
if($c>0){
$r=mysql_fetch_array($sql);
//set $_SESSION['username'] to the username from database
$_SESSION['username'] = $r['username'];
header("Refresh: 2; url=index.php?i=idx");
echo'<center>Login successfull!</center>';
//else, if there werent any records found show an error and
//return the user to index.
}else{
header("Refresh: 2; url=index.php?i=idx");
echo "<center>You couldn't be logged in!</center>";
}}
?>

Ok, so the user is logged in, everything's fine ...
but uh-oh...how can user log in if he's not registered ? :(

make a file called register.php

CODE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Register!</title>
</head>
<body>
<form action='<?=$_SERVER['PHP_SELF']?>' method='POST'>
Username: <input type='text' name='username'><br>
Password: <input type='password' name='password'><br>
<input name="register" type="submit" value="Submit">
</form>
<?php
include('config.php');
///variables...
$submit=$_POST['register'];
$username = mysql_real_escape_string(strip_tags(htmlspecialchars($_POST['username'])));
$password = md5($_POST['password']);
//if button is pressed
if($submit){
//if username is not blank..same for pass
if(($username) and ($password) and ($username!==NULL) and ($password!==NULL)){
$sql="INSERT INTO `users` (`id`,`username`,`password`) VALUES ('NULL','".$username."','".$password."')";
mysql_query($sql) or die(mysql_error());
echo "Congratulations! You are registered!<br><a href='index.php'>Log in</a>";
}
}
?>
</body>
</html>

and the logout.php

CODE

<?
session_start();
$_SESSION = array();
header("Location: index.php");
?>


I'll add some more explanations later...don't have time now sorry.
But you have the code...and if you have questions - ask.
Preview

   Sun Mar 18, 2007    Reply         

nice tutorial
allot of comments i like it

   Thu Mar 22, 2007    Reply         

nice tut indeed, really good, maybee i can use it. One question, is it possible, when you already have a forum, that u can make a quick login on your main page so you can login for the forum.. ? <-- bit vague I think? :lol: when u dont get it say it, dont really know how to say it in english :lol: :(










   Thu Mar 22, 2007    Reply         


Well, I can show you how to make so you can have a little login form in your site that will log you in to forums, but it wont log you into the site...
If it's MYbb maybe I could make you a site you can but...yeah.. :lol:

   Thu Mar 22, 2007    Reply         

Not a bad code. Could be very useful to the users that are new to PHP.

   Sun Apr 1, 2007    Reply         

In register.php-

QUOTE

<form action='<?=$_SERVER['PHP_SELF']?>' method='POST'>


Could i change the form action to register.php, and make the php code register.php and the HTML code to index.php?id=register? I think that would work... But just checking first.

   Fri Apr 13, 2007    Reply         


QUOTE

<form action='<?=$_SERVER['PHP_SELF']?>' method='POST'>

Php replaces the $_SERVER['PHP_SELF'] with register.php, so that would not be required. And I think keeping the register script separate from the index.php is a good thing. It modularizes the code so that the Index page is cleaner and the register script is separate, so any changes to it are easier to figure. Just my take on it. You would need to adjust the index page to cause the register script to be called if you make the changes you are considering.

   Fri Apr 13, 2007    Reply         

Okay thank you Jim. I was just wondering...

   Sat Apr 14, 2007    Reply         

Thanks, jlh, for the answer, maybe you've noticed I'm here almost never. I told why once already so yeah. I just terminated my account and will be looking at the posts and probably making some new tutorials.

   Tue Apr 17, 2007    Reply         

Ah, nice mate. Comments are great, and the code works awesome. I will be looking forward to seeing more of your tutorials very soon. Can't wait to see one on custom skins or something. XD

   Tue Apr 17, 2007    Reply         

I get a warning when I go to my login page...

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/enrit/public_html/projectblack/index.php:2) in /home/enrit/public_html/projectblack/index.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/enrit/public_html/projectblack/index.php:2) in /home/enrit/public_html/projectblack/index.php on line 5

edit: fixed this error, go figure, i just had to move the code up one line LMAO, now i just have that other error..

Is this right? also, when I try registering I get

Unknown column 'id' in 'field list'

How could I fix this? Does anybody know?

   Sat Jun 2, 2007    Reply         

Very helpful tutorial, I'm sure I'll use this with my coding in the future! Thanks for the heads up. Anyone have any good suggested uses for this?

   Sat Jun 2, 2007    Reply         

Greetings,

nice tut man, nice code too play with,
man is it possible to add some more things to this code,
like avatars and private message system things like that,


i would love to see things like that in this code,


hope to see some more in the future


have a nice day :lol:

   Sat Jun 2, 2007    Reply         

nice tut.
I might use this later when I get a site that can use PHP and all. I'm trying to learn PHP so this will help out a bit with my learning.

   Thu Aug 16, 2007    Reply         

o.o sweet! :) I'm So gonna use that! Thanks alot! ^-^

[note=rvalkass]
Please try to contribute to the thread. Short posts are considered spam here at Trap17.
[/note]

   Sun Sep 23, 2007    Reply         

That's a pretty good tutorial. I've been trying to do something like that for a while, but I always give up after a couple of days, because I keep getting too many errors. At the very beginning, I always had problems getting the username and stuff for the database right, then I ended up re-doing my computer, so I had to reset easyPHP and stuff, and I accidentally deleted all my files I used to have.

It's a really good tutorial to have for a beginner at php, though.

   Fri Sep 28, 2007    Reply         

I am not really certain but I think that you should close connections to the database each time you use it. I repeat that this might not be neccessary I am not certain but you should do it.

The reasons why you should do it is simple to lowers the server load as it leaves connection open unitl it times out. At least I think so.

   Sat Sep 29, 2007    Reply         

Display First Name of user instead of username
Simple User System

Okay guys what I did was add a couple fields in the register.Php page that asks the user for their first name and last name and stores them in the database as firstname and lastname. What I am trying to do is instead of displaying their username when logged in, is to display their first name they entered when they first registered. I got everything up to the point where it stores their firstname into the database. Could one of you guys tell me how this is possible? It has to be something easy and I just can't get it for the life of me!

-question by Matt

   Thu Mar 20, 2008    Reply         

This is an awesome tutorial. It is very helpful. I really enjoyed all of the comments on the code. It makes it very easy to understand for me, and I'm a n00b to PhP. :lol:

   Fri Mar 21, 2008    Reply         

Great Job!!!
Simple User System

Thank you so much for this tutorial! Not only is it great with many comments, but it actually works!! I went through 2 others, both of which weren't compatible with PHP 4... Which is the only thing my outdated host will allow lol. It seems in perfect working condition! Now all I have to do is add my comment and add a hashing algorithm for security (which I'd advise everyone else do too). Thanks again, this was very helpful, as the comments also helped me with some of the PHP commands I didn't know before :D

-Chris

-reply by Chris

   Sat Mar 22, 2008    Reply         

How in the world would I add a photo upload to this?

   Tue Apr 28, 2009    Reply         

nice tut dude it really help me alot!! :P

   Fri May 1, 2009    Reply         

Hi thank you for the great tut,
I am just having one error every thing is working well except that am getting the password as a huge number in my database sorry am newby I never used datbase b4.

Pls help me

   Fri Nov 26, 2010    Reply         

@Guest_Jos_*
its down as long numbers in the database cause the passwords are encrypted for security

   Tue Dec 6, 2011    Reply         

Quickly Post to Simple User System php, mysql driven w/o signup Share Info about Simple User System php, mysql driven using Facebook, Twitter etc. email your friend about Simple User System php, mysql driven Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Mysql Php Apache Downloads And Se...

Mysql, PHP, Apache downloads were easy enough. Then came the setup process. This seemed to be an endless mess of going back and forth trying to get the programs to work together properly. Finaly I got them working after about 48 hours. There are great tutorials on the setup process, but only one or ...more

   20-Apr-2008    Reply         

Php And Mysql Programming

hi everyone! I am making a program using php and mysql...I am a noob on this so i need your help guys...I want to make a simple program that will some values and then store them on a database and then retrieve them...uhmm let me give an example out put of what i need. T ...more

   05-May-2008    Reply         

Best Php And Mysql Editor For Noobs

hi there guys, from my previous posting, i am a noob in php and mysql programming. I want to know if there are any php and mysql editors which are best for me as a noob. i appreciate your kindness ...more

   06-May-2008    Reply         

Transfering Hosting Credits Or Awarding Them On Trap17   Transfering Hosting Credits Or Awarding Them On Trap17 (3) (3) [GIMP] Simple Stroke Tutorial Better than Edit > Stroke...  [GIMP] Simple Stroke Tutorial Better than Edit > Stroke...