AZForums: [Tuto] Login system (PHP) - AZForums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Tuto] Login system (PHP)

#1 User is offline   Strum Icon

  • AZ Student
  • PipPipPipPipPip
  • View blog
Group:
Members
Posts:
229
Joined:
22-April 06

Posted 02 June 2006 - 08:36 PM

Well this seems to be a popular intrest for newbie coders, and some experienced ones, so i'd figure i would post a tutorial for a php / mysql login system. This login system's password will be md5 encoded. You will have to make the script that adds the users yourself. Im just gonna post the login part today.

Step 1:
We will need to create a table for our login system, lets call this table users.
Here is the mysql query to create our table.

CREATE TABLE `users` (
  `id` tinyint(4) NOT NULL auto_increment,
  `user` text NOT NULL,
  `pass` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`)
);


Step 2:
Lets make the file that will connect to our mysql server and select our database.

<?php
mysql_connect("localhost", "db_user", "dbpass") or die("Could not connect to MySQL server!");
mysql_select_db("db_name") or die("Could not find MySQL database");
?>



Lets save this file as connect.php

Step 3:
Lets make the file that actualy protects the pages.

<?php
$user = $_COOKIE['user']; //gets the user from the cookies
$pass = $_COOKIE['pass']; //gets the pass from cookies
include("connect.php"); // connects to our database
$login = mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$pass`"); //selects info from our table if the row has the same user and pass that our cookies do
if(!mysql_num_rows($login)) //if the username and pass are wrong
{
  header("Location: login.php");  //redirects to our login page
  die(); //stops the page from going any further
}
?>


lets call this page protect.php

Step 4:
The Login Form/Processing page.

<?php
$act = $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
  echo('<form action="login.php?act=auth" method="post" name="loginform" id="loginform">
  <p>Username
  <input type="text" name="user">
  </p>
  <p>Password
  <input type="password" name="pass">
  </p>
  <p>
  <input type="submit" name="Submit" value="Login">
  </p>
  </form>');
}
elseif($act == "auth") //if our page action = auth
{
  $user = $_POST['user']; //pulls the username from the form
  $pw = $_POST['pass']; //pulls the pass from the form
  $pass = md5($pw); //makes our password an md5
  include("connect.php"); //connects to our mysql database
  $login = mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$pass`"); //selects info from our table if the row has the same user and pass that our form does
  if(!mysql_num_rows($login)) //if the username and pass are wrong
  {
	header("Location: login.php");  //redirects to our login page
	die(); //stops the page from going any further
  }
  else
  {
	setcookie("user", $user, time()+3600);//sets our user cookie
		setcookie("pass", $pass, time()+3600);//sets our pass cookie
		header("Location: yourpage.php");//instead of yourpage.php it would be your protected page
  } 
}
?>



Lastly
To protect a page add this to the very first line.

<?php require("protect.php"); ?>

RedLine Media™
Anizone Gift : [X] | [X]
MOTW #2 Winner [X]

Love Mozilla : Posted Image
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users