It is currently Tue Jul 16, 2019 6:41 pm


All times are UTC


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: PhP - User Account Script
PostPosted: Thu Mar 22, 2007 5:08 am 
Newbie
Newbie

Joined: Fri Dec 01, 2006 11:25 pm
Posts: 4
I am fairly new to PhP and am trying to figure out how to create user accounts for my website. I haven't been able to find any examples of scripts online, so I was wondering if anyone already knows of a site I can visit with instructions and examples. What I am looking for is as follows:

In order to complete a survey I am administering online, I would like users to have to log in.

1) I need to be able to first have users create a user account and password (that I will store in a MySQL database).
2) Then, anytime a user visits the site again, I need to be able to check the information they enter against the information stored in my database when they log in.
3) Finally, I need to be able to automatically send users their password if they forget it.


If anyone knows of a place I can get example scripts from I would greatly appreciate the help!
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 8:18 am 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
Hmmm, the reason that there are not many example login scripts online is because everyone needs to be unique to the purpose it needs. The information you will have to lookup to make your own will be


1. MySQL Connect
2. MySQL INSERT
3. MySQL SELECT

And thats all that there is to a login script, and a registration. You will also need to find out about

$_POST

and problem

sessions

I will try my best to create a tutorial for you tonight.

Josh


Top
 Profile  
 
 Post subject: Login & Registration Tutorial
PostPosted: Fri Mar 23, 2007 8:57 am 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
Ok this is a very basic tutorial and might not all make sence but i will try my best.

1. Create a database
Login to your freehostia control panel
Select MySQL Databases
Fill in the details and press Add SQL Database.

2. Create table
Still in MySQL part of the control panel, you will see
# Database Name / Database Username Size Change Password Delete
Click the database name that you just created.
Once that has opened login with the details you just entered (if not already filled out)
Click the database name in the left side area.
You will now see:
Create new table on database #database name#:
Name:
Number of fields:
For a simple login i would recommend Name: Logintable Number of feilds 3
Click Go.
For the information enter the following
ID INT 12 (leave next 4 blank) Auto Increment (select circle under the key)
user varchar 15 (leave the rest the same)
pass varchar 30 (leave the rest the same)
Click Save

Ok so there the database and tables are created. You might have already known how to do this, but better safe than sorry. Next Bit is the code side:

3. Registration Form:

Well the registration form is going to be a HTML page simple as that, with 2 text boxes and a submit, i will put a .php extension on it, encase you want to add PHP at a later date.

registration.php
Code:
<html>
<body>
<form name='login' action='registration2.php' method='post'>
User name: <input type='text' name='username'><br>
Password: <input type='password' name='password'> <br>
<input type='submit' value='Register'>
</body>
</html>

(may i just note now that this is a very basic registration form.

'registration2.php
Code:
<?php
$Link = mysql_connect (#HOST# ,#USERNAME#,#PASSWORD#); //Fill in the details

$username = $_POST['username']; //Gets username
$password = $_POST['password']; //Gets password

$Query = "SELECT ID FROM #DATABASE TABLE# WHERE username = '$username'";
$Result = mysql_db_query(#DATABASE NAME#, $Query, $Link);
if ($Row = mysql_fetch_array($Result)){
         echo "Username already taken"; //Sends Error Message
         die(); //Ends Script
}

$Query2 = "INSERT INTO #DATABASE TABLE# VALUES ('', '$username', '$password')";
if ( mysql_db_query(#DATABASE NAME#, $Query2, $Link)){
         echo "Thank you for registering";
}
mysql_close();
?>


All that the code above does is
1.Connects to the database
2. Checks is the user name is taken
3. Enters the information into the database



4. Login script

So the users are now registered they have to be able to login

login.php
Code:
<html>
<body>
<form name='login' action='login2.php' method='post'>
User name: <input type='text' name='username'><br>
Password: <input type='password' name='password'> <br>
<input type='submit' value='Login'>
</body>
</html>


(pretty much the same as the registration script apart from it has action='login2.php' and the submit value is Login and not register.


I will use sessions for the login, so you will have to lookup using sessions so they can do something then logged in

login2.php

Code:
<?php
session_start();  //Must go top of every page that there login information is needed.
$Link = mysql_connect (#HOST# ,#USERNAME#,#PASSWORD#); //Fill in the details

$username = $_POST['username']; //Gets username
$password = $_POST['password']; //Gets password

$Query = "SELECT ID FROM #DATABASE TABLE# WHERE username = '$username' AND password = '$password'";    //Checks database for username and password combos
$Result = mysql_db_query(#DATABASE NAME#, $Query, $Link); //Gets the results

if ($Row = mysql_fetch_array($Result)){   //Checks if there is a username and password combo
        $_SESSION["user"] = $username; //Sets sessions
        $_SESSION['uID'] = $Row['ID'];
         header('location: #NEXTPAGE#.php'); //Sends the user to a different place
}else{
       header('location:login.php'); //Sends user back to login if they have the wrong details
}
mysql_close();
?>



--- End ---

Thats now them registered, and logged in. The lost password is not possible hear on the free accounts as there is no send_mail. If you have that option enabled on your account, let me know and i will write for that too.

I would like to also point out that this login system is very INSECURE, the passwords are not encrypted and its a bit open to mysql injection. But it will do for what you want it to do, and as your PHP advances you will be able to change it to make it more secure. Most of my login scripts checks for the username, then uses and IF statement to find out if the password matches.[/code]


Last edited by Josh.Waller on Fri Aug 10, 2007 9:14 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 8:24 pm 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
*side thing to mr mcgeneral* i did try replying but PM went kuput


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 1:02 am 
Newbie
Newbie

Joined: Fri Dec 01, 2006 11:25 pm
Posts: 4
OMG thanks for all of your help! I really appreciate that you took the time to put together that tutorial. I am going to try it out over the next few days and will let you know how it goes.

Thanks :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ]  Moderators: Moderators, Support Team

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Hosting | Domains | Servers | Extras | Order | Support | Contacts | FreeHostia © 2011