You are viewing our Forum Archives. To view or take place in current topics click here.
[PHP] Create a Login Page Tutorial
Posted:

[PHP] Create a Login Page TutorialPosted:

GameDev14
  • Junior Member
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
Firstly make sure you have an SQL Database ready to be used or just create one on you webserver. Also create a new table and just name it 'members' or something like that. If you dont know how to create a table then google it, if you are using a free webhost with cpanel then you will most likely have phpmyadmin which you can use to create the table. Anyway lets get started

For our documents we are going to be using Notepad++
If you dont have Notepad++ then download it here:
[ Register or Signin to view external links. ]

Now lets create our document and then before typing anything save it as 'login.php'.
This 'login.php' is just going to be a simple login form with an action so add the code below...

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checkuser.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>


Now lets create a new file and before typing anything again, save it as 'checkuser.php'.
Now add the script below and edit it to your database values

<?php

$host="YOUR_DB_HOST"; // Host name
$username="YOUR_DB_USERNAME"; // Mysql username
$password="YOUR_DB_PASSWORD"; // Mysql password
$db_name="DB_NAME"; // Database name
$tbl_name="TABLE_NAME"; // Table name

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

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

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

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

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

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


Now lets create our final document and save it as 'login_complete.php'
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
?>

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


Now add a new user to your database and then upload all 3 files to your webhost and go to the url, you should now have a fully functional login page and be able to login with the account you just created. If you have any questions please pm me or leave a comment

Remember PHP is server sided so opening the files you have created in you browser wont work and your browser will just ask you to save it or it will just open it as plain text. For this login page to work you will need to upload it to your server or webhost.

The following 1 user thanked GameDev14 for this useful post:

phpBB (01-16-2014)
#2. Posted:
phpBB
  • TTG Senior
Status: Offline
Joined: Dec 31, 201112Year Member
Posts: 1,274
Reputation Power: 54
Status: Offline
Joined: Dec 31, 201112Year Member
Posts: 1,274
Reputation Power: 54
ahhhhhhhh the beautiful language of php :3
#3. Posted:
GameDev14
  • Junior Member
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
phpBB wrote ahhhhhhhh the beautiful language of php :3


Yep, you gotta love it. When its not a complete headache
#4. Posted:
Z61
  • V5 Launch
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
This is hardly a tutorial.
#5. Posted:
Cgallagher21
  • Powerhouse
Status: Offline
Joined: Mar 13, 201113Year Member
Posts: 402
Reputation Power: 15
Status: Offline
Joined: Mar 13, 201113Year Member
Posts: 402
Reputation Power: 15
If anyone's planning on using this use mysqli not mysql.
#6. Posted:
GameDev14
  • Junior Member
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
Cgallagher21 wrote If anyone's planning on using this use mysqli not mysql.


Yeah i should of really included that but to be fair its one of my first tutorials and it didn't even cross my mind
#7. Posted:
GameDev14
  • Junior Member
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
Status: Offline
Joined: Jan 10, 201410Year Member
Posts: 80
Reputation Power: 3
Z61 wrote This is hardly a tutorial.


Well what would you call it then? Its a basic tutorial, Yes i could of added a lot more but its one of my first and in my future tut's i will make them more detailed
#8. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
GameDev14 wrote
Cgallagher21 wrote If anyone's planning on using this use mysqli not mysql.


Yeah i should of really included that but to be fair its one of my first tutorials and it didn't even cross my mind


Which if nothing else, shows you're in no position to be teaching others PHP. You don't simply 'forget' not to use a deprecated function, you obviously just don't know any other way.

Here's an actual tutorial on the mysqli class; [ Register or Signin to view external links. ]
#9. Posted:
Saop
  • TTG Senior
Status: Offline
Joined: Apr 30, 201311Year Member
Posts: 1,200
Reputation Power: 57
Status: Offline
Joined: Apr 30, 201311Year Member
Posts: 1,200
Reputation Power: 57
This would come in handy if I knew what I was doing thanks great tut Might try it
#10. Posted:
Nic
  • Retired Staff
Status: Offline
Joined: Jun 08, 201013Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
Motto: I've been watching you all day.
Status: Offline
Joined: Jun 08, 201013Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
It seems that you copied the majority of the code from a website.
[ Register or Signin to view external links. ]

Not even to mention that person explicitly says that he is having trouble with it (getting errors), and you think it's a good idea to use that code for a tutorial? The code you posted is full of errors and mistakes.

Few tips to improve this tutorial:
  • It wouldn't hurt to have something about how to actually create a database. If someone needs a tutorial for a login then they probably don't know much about databases either.
  • The password input field has the type "text" instead of "password". No one wants their password to be displayed on the screen as they enter it.
  • mysql_* commands are soon to be deprecated, for good reasons. At least switch to mysqli_* commands for basic tutorials, but preferably to MySQLi OO or PDO. They also have good methods to sanitize inputs.
  • Simply checking if a user has a username registered, with even an already deprecated function, is not good enough. On every page the login details should be run through the database to see if they actually exist or are still the same.

I suggest you look into PHP some more before posting tutorials. At least follow another tutorial and try it out to make sure it actually works.


Z61 wrote This is hardly a tutorial.
And this is hardly feedback. Next time, don't bother posting if you have nothing useful to say.


Last edited by Nic ; edited 2 times in total
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.