PDA

View Full Version : [TUTORIAL] Creating a login script for multiple users.



nbop
06-28-2006, 06:01 PM
Have you ever wanted to make a login script so you can have membercontent?
This tutorial will show you how using php. No database will be used.

1) Creating a form
Here is the code for making the form. The form asks for your username and password. Save this code as login.php



<html>
<head>
<title>Please login</title>
</head>
<body>
<?php
if(isset($_GET["wrong"])){
echo("<b>Username or password is incorrect!<br />Please try again.</b>");
}
?>

<form action="login2.php" method="post">
<br />
Username:<br />
<input type="text" name="username" /><br />
Password:<br />
<input type="password" name="password" />
<br />
<br />
<input type="submit" value="Login" />
</form>

</body>
</html>

The piece of php code will tell the viewer that he or she typed a wrong username or password.
between the <form></form> tags we have 3 input fields. 1 for the username, 1 for the password, and 1 that will submit the inputs and send it to login2.php.
Note that the form will be sent using the "post" method. This is the safest.

2)Checking the userinput
In this step we will check if the username and password are correct.
This is done with php. No html will be used.
Create a new page and call it login2.php. Give it the following content:


<?php
$usernames = array("user1", "user2", "user3", "superman");
$passwords = array("pass1", "pass2", "password3", "supermans password");
$page = "mypage.php";



for($i=0;$i<count($usernames);$i++){
$logindata[$usernames[$i]]=$passwords[$i];
}
if($logindata[$_POST["username"]]==$_POST["password"]){
session_start();
$_SESSION["username"]=$_POST["username"];
header('Location: '.$page);
exit;
}else{
header('Location: login.php?wrong=1');
exit;
}
?>
You might want to edit the first 3 lines.
1)$usernames is an array with all the usernames. To add a new one:

$usernames = array("user1", "user2", "user3", "superman", "new one"); 2)$passwords is an array with all the passwords. To add a new one:

$passwords = array("pass1", "pass2", "password3", "supermans password", "new one"); Make sure that the username and the password have the same position.
user1 will be able to login with the password pass1, not with any other password. user2 with pass2 and no other.
3)$page is the page the user will go to when he is logged in. Change mypage.php to the page you would like to be the main page for logged in users. make sure the quotationmarks are still around it!

3) Checking if the user is still logged in
Create a new page, call it login3.php with the following code:


<?php
session_start();
if(!isset($_SESSION["username"]){
header('Location: login.php');
exit;
}
?>

This code simply checks if the sesion that we made in login2.php has a value.
If it doesn't it sends you to the login page.

4) Making your pages protected
All the pages that you want to be protected need to have a .php extension! otherwise they cannot be protected. Add this piece of code to the first line.

<?php require("login3.php"); ?> Make sure it is on the first line. Otherwise it will give you an error.
Now you pages are protected! simple?


5) Extras
making a log out page:
make a new .php page and add the following contents at the very beginning:


<?php session_start();session_unset();session_destroy(); ?>

Your fancy logout message here
create a link somewhere on your secured pages that leads to the logout page and the user will be logged out when he clicks the link.

An other extra: showing the user's name: just put this piece of code in any secured page where you want to show the name:


<?php echo($_SESSION["username"]); ?>

Watdaflip
06-28-2006, 07:27 PM
Just keep in mind not to intergate this with your website using BV, because its impossible with BV to put php in before the head is declared. This is going to give you an error when using session_start();

session_start(); has to be declared on the first line of a page (or second if you count <?php) before any other header is declared

nbop
06-28-2006, 08:09 PM
If you are using BV just create your page, save it nad then open it with notepad or an other plain text editor. Now you can add the php code.

vedprakash
07-11-2006, 07:41 AM
If you are using BV just create your page, save it nad then open it with notepad or an other plain text editor. Now you can add the php code.

sir,
will you please tell me how can l use the above script if I am using blue
voda.
How to save the pages as .php extension

Pablo
07-11-2006, 08:31 AM
Just keep in mind not to intergate this with your website using BV
Yes, you can insert this code directly into BV:
View->Page HTML->Start of Page

navaldesign
07-11-2006, 03:57 PM
Only one observation: the usernames and passwords in this script are hardcoded in the script itself. This means that the script - login2.php - needs to be edited manually everytime a new user must be added. This is not so convenient, if you expect to have many visitors. There will also be a "dead" time before the site administrator updated the script.

A solution would be (of course) to use a database, or, have the registration form info (username and password) writen in an file, which could be in this way automatically updated, as this BlueVoda Login script does here: > BVs new built in Login system a brief tutorial. (http://www.vodahost.com/vodatalk/bluevoda-general-issues/55281-bv-built-login-system-tutorial.html)


However, very useful!. Well done mate!

vedprakash
07-13-2006, 05:24 PM
I have got the downlodable pages from form maker now where to upload these pages and how to upload.

should it be upload thru publishing blue voda or
blue ftp or cpanel file manager.

navaldesign
07-13-2006, 05:33 PM
Read the installation instructions here (http://www.vodahost.com/fmpdocs/)

konnectedservices
07-17-2006, 12:34 AM
Thanks nbop (http://www.vodahost.com/vodatalk/members/nbop.html) and other member for the tutorial creating vbmenu_register("postmenu_48386", true); login for multiple user, however, have created this on my site but dont know what to do next, what area of my cpanel will i load this into as i am using cpanel and how do i create an account for the user so that as soon as he signin he will see his information, secondly do i need to create database for the login form and who. i will hope to hear from you soon about this

navaldesign
07-17-2006, 06:44 AM
I'm afraid that the above script is not so automated. After a user has registered, you have to MANUALLY insert his username and password in the script as described above. Then you must upload again the updated script to your server and then let the new member know that he / she can go on visiting your site. Till you do this, he / she won't be able to loggin.
You don't need to create a database, not with this script.

konnectedservices
07-18-2006, 01:21 AM
Thanks for the reply what i am saying about is this, i have a site which have a registration page and login page, when a user register his information to the site throught eh login page what do i do next and how do i direct his register information to a page he will see when he login, i have don the pages discribe before but i want to know how i can setup a database for it as i am using cpanel

navaldesign
07-18-2006, 06:17 AM
The above script doesn't make use of a database. Also, it doesn't describe how to make a registration form. You need to
1. Create aregistration form where the user fills in his desired username and password along with other information. Set the form so that the info is sent to you through an email. Update the script with the new username and password, and re-upload it on the server. Let the user know that his registration has been approved and he may now loggin in your site.

greyhame
07-21-2006, 09:46 PM
This script can be inserted into a Blue Voda page simply by using the html tool at the top of the left toolbar to insert an html box. Double-click the box to open it and paste in the code. The php code will be embedded in the page. However, there seems to be a problem with the script. This appears above the form fields for the Name and Password before and information is entered:

Username or password is incorrect!
Please try again.</B>"); } ?>

greyhame
07-21-2006, 10:02 PM
also tried this by uploading the php file to my server. When I ran it I got the following error:
Parse error: syntax error, unexpected '}', expecting ',' or ';' in /home/colescom/public_html/FormTools/phptest.php on line 8 Areany values supposed to load values supposed to be modified before running the script?

konnectedservices
07-24-2006, 01:39 AM
Thanks i got ur explainatioon about the application form, plz can you give me step by step what files to create and what directory are needed to work all this and how do. where do i do when the information on the application form has been sent my email . what thing he see when i register user login

jmoyn2339
09-12-2006, 03:24 AM
Hi...I tried to create a login using the script that was posted...I uploaded it to try it and I am receiving an error message.
The error message is saying the following:
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /homepages/41/d171920401/htdocs/Beta_Site/login2.php on line 9

Does anyone have any suggestions what I m doing wrong?

Thanks,
John

doug145
10-02-2006, 06:12 PM
How do you set up a database containing all of my member login info?

Doug

clanffa
11-28-2006, 05:41 AM
Warning: session_start() [function.session-start (http://www.ffaproam.com/function.session-start)]: Cannot send session cache limiter - headers already sent (output started at /home/yumeqnm/public_html/login2.php:1) in /home/yumeqnm/public_html/login2.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at /home/yumeqnm/public_html/login2.php:1) in /home/yumeqnm/public_html/login2.php on line 14



thats the error message i get

navaldesign
11-28-2006, 05:48 AM
The session cache limiter command has to be before the session_start command. Change your script. However, it can also fail for other reasons too. Please post here the first 14 lines of your script.

clanffa
11-28-2006, 05:54 AM
<?php
$usernames = array("user1", "user2", "user3", "superman");
$passwords = array("pass1", "pass2", "password3", "supermans password");
$page = "testlogin.php";



for($i=0;$i<count($usernames);$i++){
$logindata[$usernames[$i]]=$passwords[$i];
}
if($logindata[$_POST['username']]==$_POST[be 'password']){ what is this "be"?
session_start();
$_SESSION[username]=$_POST['username']; Should
header('Location: '.$page); Should be header ('Location $page');


First 14 just incase heres the rest

exit;
}else{
header('Location: login.php?wrong=1');
exit;
}
?>

navaldesign
11-28-2006, 06:08 AM
I found these in this part of the script. There might be other errors. Please also use single quotes, as i corrected it.

clanffa
11-28-2006, 06:15 AM
i fixed everything but still get the same error message!

navaldesign
11-28-2006, 06:46 AM
Did you place this script inside a BV page ?

clanffa
11-28-2006, 07:20 AM
no. I got to cpanel and then filemanager. I write scripts/edit/fix in notepad and then upload to public_html

navaldesign
11-28-2006, 08:47 AM
Well, to debug i would need to install the whole script and see what it does. However, i see that you have a clan site. I beleive that such a script, where you should always update it manually, is not a good solution for you (i beleive that you expect to have many members, so you would need a script that allows users to register themselves, then the username and password are stored in a database, and the script would retreive the info from the database

clanffa
11-29-2006, 01:42 AM
no, the script is for a friend of mine. and I dont mind managing his users. Also, he doesnt want auto registration. If you could debug the script for me that be sooo awsome!

clanffa
11-29-2006, 03:40 AM
also, navaldesign. consider writing a topic on a mysql login database script?

navaldesign
11-29-2006, 09:50 AM
I do have a full MySQL login script that i created for my clients. However, it requires a registration form and some additional knowledge for installing it, since it is part of a major script system, it is not a stand alone.

Creating such scripts, and then creating a tutorial, is very time consuming, and since my work only allows for limited free time, i will do it when i'll be able to.

clanffa
11-30-2006, 01:56 AM
thank you very much navaldesign

clanffa
11-30-2006, 03:58 AM
so. should I just leave this script alone for now or......

clanffa
11-30-2006, 05:16 AM
just curoius.... naval, hows the debugging coming with the script?

navaldesign
11-30-2006, 05:33 AM
Clanffa,

i'm sorry but i have not made myself clear. I wrote that in order to debug , i would need to install the script and see what it does. I'm sorry, i do not have all this time. As for my script, it also requires lot of time to create a tutorial based on it.

Your solution: a net search for "Free loggin script". You will find many, choose whichever you like.

clanffa
11-30-2006, 07:28 AM
oh ok. no problem navaldesign.

clanffa
12-02-2006, 06:44 AM
can someone plx try to fix the script. im super NOOB at php.

papichulo
12-11-2006, 06:15 AM
Hellooo everyone :) I'm papichulo new to this site I hope to learn a whole -bunch from you'all.

fozzewfc
01-19-2007, 06:18 PM
Right i have installed all of this code i have put the login pages 1-3 in my main directory and then creted a sub folder called members and creted a file called members.php. but no matter what i do i log in and i keeps on stoping at the login2.php page.

login1
login2
login3
members-members.php

Also even if i put in the wrong data you can still get redirected to login2.php.

you can see this at : http://fozze.8888mb.com/login.php.

Please help me!!!

gg3558
05-11-2007, 06:44 PM
I have followed the directions to this tutorial but am having a problem. Any help available? Much appreciated.

davidundalicia
09-17-2008, 04:21 PM
I have a free membership script that will do all of the above which uses a database:
If you want to try it out, go to this thread
http://www.vodahost.com/vodatalk/customer-website-reviews-your-testing-ground/40581-reward-testing-new-site.html
or click on the first entry under my signature.

For those who wish this to be installed for them, I can provide that service for a small fee.

LynnsBiz
09-18-2008, 03:41 AM
I'm new w/ BV so could use some detail help!!!

I just want 1 user name/password for a site that I want only my team to have access to. (Each user doesn't need their own user name/password.)

So, step-by-step, what do I do?

From Pablo: Yes, you can insert this code directly into BV:
View->Page HTML->Start of Page

Which code?

Also, for the other pages that the main page links to, do they need codes?

Thanks for helping a newbie!!!

navaldesign
09-18-2008, 07:43 AM
It is not only a simple oiece of code that you need. You also need, (if only one username / password are required) at least a login page with related code.

However, for your purposes, a protected folder will do the job the same way and much more easier for you. Please watch this tutorial:

http://www.vodahost.com/demowolf/x3/cpanel-x3-protect.html


Create a protected folder, and publish ALL the pages that you only want your members to be able to reach, inside that folder. This way you don't need any login page or other, the necessary login popup is automatically displayed.