How to create a login system for my website?

0 votes
asked by
edited by

I tried to use .htaccess wizard, but unfortunately, I do not see how to get a webpage that allows customers to simply enter a username and password before arriving on their home page.
I just want all my custumers to be able to see the same page via a password and username login. Can you do this?

1 Answer

+1 vote
No avatar answered by (2.1k points)
edited by

Your issue requires a bit more work from your side. In order to create a login system I recommend you implement PHP on your web page. Before doing that make sure the server you are currently using supports PHP and MySQL because we will be using those.
We will need to create about 3 PHP files for this to work, in order to create them you can simply use a text editor and implement the PHP code and then save it as .php , or you can download a software used in web development, such as Adobe Dreamweaver that makes you able view the code side of the page and the design side of the page on your PC, so you will be able to tell how your webpage will actually look on the internet, for the customer.

Let's begin!
If you're server is using CPanel or Plesk(the most common and used control panels) , then you will be able to quickly spot the phpMyAdmin tool that is used to manage the MySQL database, if you're using any other control panel just ask your hosting provider for instructions in case you have difficulties finding it.
While in phpMyAdmin , create a new database and name it "customer" and under it, create a table called "members" , by running this SQL code:

 CREATE TABLE `members` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
INSERT INTO `members` VALUES (1, 'customer1', '1234');

Now create a page named "login.php" , this will be the page used for your custumers to login with a username and password.
Use the fallowing code for login.php :

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Customer Login </strong></td>
</tr>
<tr>
<td width="75">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Next , we need to to check if it's the customer that wants to login, so for that we create a page named "logincheck.php". This page will check if the username and password exists in the database we made earlier.
Use the fallowing code for logincheck.php:

<?php

$host="localhost"; // Your server
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="customer"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$username=$_POST['username']; 
$password=$_POST['password']; 

// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $username and $password, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "customer_area.php"
session_register("username");
session_register("password"); 
header("location:customer_area.php");
}
else {
echo "Wrong Username or Password";
}
?>

And last, we finally reached our destination, the customer's webpage that he can access only by entering the username and password provided. For this create a page named "customerarea.php"
Use the fallowing code on customer
area.php :

<?php
session_start();
if(!session_is_registered(username)){
header("location:login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

On the last page , customer_area.php as long as you leave the PHP code intact, you can modify and add anything you wish to display your customers with.
You can now upload all these 3 files (in the same directory) on your server and test it out by going to login.php(ex: http://yourserver.com/login.php) and typing customer1 as username and 1234 as password. You can always add more "members" by going back to phpMyAdmin and inserting them into the members table.

asked Sep 30, 2014
edited Nov 21, 2014 by
Login code information.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register
...