Jump to content



Photo

[Tutorial] Setting up a Simple Login System with PHP - ConnerT


  • This topic is locked This topic is locked
No replies to this topic

#1 Cryo Kinesis

Cryo Kinesis

    Haphost Staff

  • Moderators
  • 128 posts
  • LocationGridania, Eorzea

Posted 05 August 2014 - 05:07 PM

Hi all , i noticed that some people would like to have a login system so i have made this to make it simple and easy to understand, for those people who dont know PHP and HTML

So here goes;

To start off with you will need the following:

  • Web Hosting Account
  • Access to mySQL
  • A Good Code Editor

Okay, to start please download the LoginSystem.zip attachment this contains 6 files

[ATTACH]889[/ATTACH]

Create a mySQL database in your Hosting Panel (unsure PM me)

Then we will edit the "connect.php" inside which looks like this:


<?
//Database Information
//Database Host usually a URL
$dbhost = "localhost";
//Database Name a string of text that you called the database something like example_name
$dbname = "database name";
//Database user this is the username assigned to the database eg user_name
$dbuser = "database user";
//Database password the password assigned to the database
$dbpass = "database password";
//This launches the connect socket the the database which sees if the credentials is correct
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect to MySQL: ".mysql_error());
//This chooses the name form the assigned $dbname and connects to show tablesmysql_select_db($dbname) or die("Could not connect to Database:".mysql_error());
?>


Okay so let me explain each line:

the $dbhost is the connection name to which will start of the process of trying to connect to your database, this is usually a web adress (URL)

next the line which contains $dbname this is the assigned name that you set when creating the database

So that leaves $dbuser and $dbpass , these two are straight forward:

$dbuser is the username assigned to the database
$dbpass is the password to the database and usually the username

So now that that is edited you can Save that ()make sure you remember where !)

Secondly we are going to make a few changes to the Login.html

which looks like this:

<html><form name="login" method="post" action="login.php">
 <table border="0" width="225" align="center">
 <tr>
 <td width="219" bgcolor="#999999">
 <p align="center"><font color="white"><span style="font-size:12pt;"><b>Login</b></span></font></p>
 </td>
 </tr>
 <tr>
 <td width="219">
 <table border="0" width="220" align="center">
 <tr>
 <td width="71"><span style="font-size:10pt;">Username:</span></td>
 <td width="139"><input type="text" name="username"></td>
 </tr>
 <tr>
 <td width="71"><span style="font-size:10pt;">Password:</span></td>
 <td width="139"><input type="password" name="password"></td>
 </tr>
 <tr>
 <td width="71">&nbsp;</td>
 <td width="139">
 <p align="right"><input type="submit" name="submit" value="Submit"></p>
 </td>
 </tr>
 </table>
 </td>
 </tr>
 <tr>
 <td width="219" bgcolor="#999999"><font color="white">Not Registered? </font><a href="register.html" target="_self"><font color="white">Register</font></a><font color="white"> </font><b><i><font color="white">Now!</font></i></b></td>
 </tr>
 </table>
 </form>
</html>

This set of HTML forms uses the Action and POST method to connect with the login.php file and submit the Data that the user has entered

You can edit this to your liking this bit is up to you !, if you want some extra help just reply or PM me i will gladly help you out !

One of the main changes people like to make with the Login.html is to style it to their custom colours and settings.

So now we are going to Ensure that the Login.php is correct, your file should look like this:

<?
    include "connect.php";    $username=mysql_real_escape_string($_POST['username']);    $password=mysql_real_escape_string($_POST['password']);    $sql="SELECT * FROM user WHERE username='$username' and password='$password'";    $result=mysql_query($sql);    $count=mysql_num_rows($result);    if($count==1)    {        session_start();        $_SESSION['username']=$username;        $_SESSION['password']=$password;        header("location:send_message.html");    }    else    {        echo "Login Attempt Failed";    }    ?>
 

So in brief this login.php does all the work ! by using the POST and GET method from the login.html this grabs(GET) the data from the user and parses this information to the next step which then ensures the users exists by having the PHP file make the connection to the database from the previous connect.php.

Next the register.html,

<html><form action="register.php" method="post">
 <table border="0" width="225" align="center">
 <tr>
 <td width="219" bgcolor="#999999">
 <p align="center"><font color="white"><span style="font-size:12pt;"><b>Registration</b></span></font></p>
 </td>
 </tr>
 <tr>
 <td width="219">
 <table border="0" width="282" align="center">
 <tr>
 <td width="116"><span style="font-size:10pt;">Name:</span></td>
 <td width="156"><input type="text" name="name" maxlength="100"></td>
 </tr>
 <tr>
 <td width="116"><span style="font-size:10pt;">Email:</span></td>
 <td width="156"><input type="text" name="email" maxlength="100"></td>
 </tr>
 <tr>
 <td width="116"><span style="font-size:10pt;">Username:</span></td>
 <td width="156"><input type="text" name="username"></td>
 </tr>
 <tr>
 <td width="116"><span style="font-size:10pt;">Password:</span></td>
 <td width="156"><input type="password" name="password"></td>
 </tr>
 <tr>
 <td width="116">&nbsp;</td>
 <td width="156">
 <p align="right"><input type="submit" name="submit" value="Submit"></p>
 </td>
 </tr>
 </table>
 </td>
 </tr>
 <tr>
 <td width="219" bgcolor="#999999">&nbsp;</td>
 </tr>
 </table>
 </form>
</html>

this HMTL form picks up the data from the input boxes and using the same Action and Post method as in the Login.html sends it to the register script located in the register.php file

Your register.php file is included and should also look like this;

<?    include "connect.php";    $username=mysql_real_escape_string($_POST['username']);    $password=mysql_real_escape_string($_POST['password']);    $name=mysql_real_escape_string($_POST['name']);    $email=mysql_real_escape_string($_POST['email']);    $insert=mysql_query("INSERT INTO user (username,password,name,email) VALUES ('$username','$password','$name','$email')") or die("Not Connected");    echo "Successfully Registered....";?> 


this sets the registered username to the username box that was included on the register.html and sends it back to the database which then creates a row and inputs the data into the table for the login.html to pull our of (query)

Nearly done now ! pheww, okay the last step is a bit complicated, but if you follow the guide you should get it , in order to have information sorted in a mySQL database we need to create the first tables.

Navigate to a program on your Control Panel called phpMyAdmin, this allows us to use and interact with the mySQL database.

inside the top bar you should see a link called "Query" or "Script" depending on the phpMyAdmin version.

Next this opens a dialog which you can input a Script to copy and paste this:

CREATETABLEIF NOTEXISTS `login_system` (`username` text NOT NULL,
 `password` text NOT NULL,
 `name` text NOT NULL,
 `email` text NOT NULL
) ENGINE=InnoDB DEFAULTCHARSET=latin1;

that will create the tables and rows needed.


All Done !!! , try your page by navigating to the login.html or register.html and register your account !

If you enjoyed or found this tutorial helpful dont forget the Rep !

Likewise i will gladly help anyone who needs help with this just reply or PM Me !

// ConnerT
Haphost Moderator

Whoops almost forgot !

Add this to the top of any page you would like to protect, meaning that you can only access it if you are logged in !


<?php include 'integration.php' ?>


Make sure the directory is correct relating to the include otherwise it will not recognise the file smile.png

// ConnerT
  • danilogio and Chickex like this

Cryo%20Kinesis%20Haphost%20Sig2.png





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users