FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Site Members' System

 


clip
This is for satheesh. He requested some PHP tutorials mainly for member's registration and login things. etc etc etc...

This tutorial will be consisting for 4 php pages mainly, the registration page, the login page, the loggedin page and the log out page.

Let's start making some codes then.

This is how you connect to your database.

Code:
<?php
define ('DB_USER', '[b]username[/b]');
define ('DB_PASSWORD', '[b]password[/b]');
define ('DB_HOST', '[b]localhost[/b]');
define ('DB_NAME', '[b]databasename[/b]');

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die('Could not connect to MySQL: <font color="red">' . mysql_error() . '</font>');
@mysql_select_db (DB_NAME) OR die ('Could not select the database: <font color="red">' . mysql_error() . '</font>');
?>


the define codes puts the necessary values needed for connecting and selecting your database. The $dbc variable is just an optional variable and it will act as a reference point.

Save this file as 'mysql_connect.php' without the quotes.

now let's make your database

Code:
CREATE TABLE members (
user_id SMALLINT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL,
password VARCHAR(16) NOT NULL,
email VARCHAR(30) NOT NULL,
PRIMARY KEY (user_id),
UNIQUE (username)
);


This will be your query for your database, you can add a first name, last name, location etc. if you want. But I'll only put the basics for this tutorial.

Now, for the registration page:

Code:
<?php

if (isset($_POST['submit'])) {

 require_once ('mysql_connect.php'); //connect db

function escape_data ($data) {
  global $dbc;
  if (ini_get('magic_quotes_gpc')) {
   $data = stripslashes($data);
  }
  return mysql_real_escape_string($data, $dbc);
}

 $message = NULL;
 
if(empty($_POST['username'])) { //validate username
  $u = FALSE;
  $message .= '<p>Please Enter a Username</p>';
 } else {
  $u = escape_data($_POST['username']);
 }

 if(empty($_POST['password1'])) { //validate password
  $p = FALSE;
  $message .= '<p>Please Enter a Password</p>';
 } else {
  if($_POST['password1'] == $_POST['password2']) {
   $p = escape_data($_POST['password1']);
  } else {
   $p = FALSE;
   $message .= '<p>Your passwords do not match</p>';
  }
}

 if(empty($_POST['email'])) { //validate email
  $e = FALSE;
  $message .= '<p>Please Enter your email address</p>';
  } else {
   $e = escape_data($_POST['email']);
  }

if($e && $u && $p) { //check if everything's OK

 $query = "SELECT username FROM members WHERE username='$u'";
 $result = @mysql_query($query);
 if(mysql_num_rows($result) == 0) {
 $query = "INSERT INTO admins (username, password, email) VALUES ('$u', PASSWORD('$p'), '$e')";
 $result = @mysql_query($query); //Run query
 if($result) { //if ok
  echo '<p align="center"><b>You have been registered!</b></p>';
  exit(); //exit the script
 } else { //if didn't run
  $message = '<p>Sorry for the inconvenience, there is a system error.<br>' . mysql_error() . '</p>';
 }
} else {
 $message = '<p>That username is taken, please choose another.</p>';
}
  mysql_close(); //close connection
 } else {
  $message .= '<p>Please try again.</p>';
 }
}

if (isset($message)) {
 echo '<table width="378" border="0" align="center"><tr><td>';
 echo '<form><fieldset><legend>Errors:</legend>';
 echo '<font color="red"><div align="center">', $message, '</div></font></fieldset></form></td></tr></table>';
}
?>


This code will be followed by a form below. A form with 3 textboxes named 'username', 'password' and 'email' respectively and a button of type submit and also named submit.

save this file as register.php

The login page:

Code:

<?php
if (isset($_POST['submit'])) {

 require_once ('mysql_connect.php'); //connect db

function escape_data ($data) {
  global $dbc;
  if (ini_get('magic_quotes_gpc')) {
   $data = stripslashes($data);
  }
  return mysql_real_escape_string($data, $dbc);
}

 $message = NULL;
 
 if(empty($_POST['username'])) { //validate username
  $u = FALSE;
  $message .= '<p>Please Enter a Username</p>';
 } else {
  $u = escape_data($_POST['username']);
 }
 
 if(empty($_POST['password'])) { //validate password
  $p = FALSE;
  $message .= '<p>Please Enter a Password</p>';
 } else {
    $p = escape_data($_POST['password']);
  }
$message .= $u.$p;
if($u && $p) { //check if everything's OK

 $query = "SELECT user_id FROM members WHERE username='$u' AND password=PASSWORD('$p')";
 $result = @mysql_query($query);
 $row = mysql_fetch_array ($result, MYSQL_NUM);
 
 if($row) {
  setcookie ('user_id', $row[0], time()+3600, '/', '', 0);
  header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/loggedin.php");
  exit();
 } else { //no record found
  $message = '<p>Incorrect username or password.</p>';
 }
  mysql_close(); //close connection
 } else {
  $message .= '<p>Please try again.</p>';
 }
}

if (isset($message)) {
 echo '<table width="378" border="0" align="center"><tr><td>';
 echo '<form><fieldset><legend>Errors:</legend>';
 echo '<font color="red"><div align="center">', $message, '</div></font></fieldset></form></td></tr></table>';
}
?>


this will be your login page, just add you form below with 2 textboxes, username, password and a button of type sumbit.

then save the file as login.php

loggedin page

Code:
<?php

if (!isset($_COOKIE['user_id'])) {
 header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER('PHP_SELF')) . "index.php");
 exit();
}

echo "<p align=\"center\">You are now logged in!</p>";
?>


This page isn't accessible if the user_id cookie is not present. It's actually just a notice saying that the user is currently logged in.

save this file as loggedin.php as login.php will go here if the user authentication is successful.

logout page

Code:
<?php
 if(!isset($_COOKIE['user_id'])) {
  header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
  exit();
 } else {
  setcookie ('user_id', '', time()-300, '/', '', 0);
 }
 
 echo "<p align=\"center\">You are now logged out</p>";
 
 ?>


This code will destroy the user_id cookie. just make a logout link referring to this page.

save this as logout.php

And there you have it! A Member's System for your website! If you guys have comments to this or some corrections then please do tell me my mistakes.

BTW, this is the same as it is on my site and i just edited some things to fit this tutorial. I hope you guys like this. ^^


Last edited by clip on Thu Jan 26, 2006 1:48 pm; edited 5 times in total
satheesh
Hi,

Thanks a lot Clip for the tutorial. I saw the tutorial only today and will be putting up the pages for the same only today. If I am free today I will do all the uploading stuff into the website. But today I would be leaving for a short vacation of one week. If I am not able to finish my work on the website by the end of day today (as I am also trying to put up the advertisements on my forum). I would do it as soon as I return back.

If I am right these data that are input into the database can be saved in the email accounts page so that the mail for each and every user is created for my website and they can log on to it through my website.

How do I retrieve these registration details that I have stored in the database for any purpose that I need them for.For eg, if I need to send mai lto particular number of users etc. If you need specific details I can send them too.

Thanks again to Clip for posting the tutorial. I would post the link here in this page as soon as I finish uploading the pages. Check out as its your creation, Clip. Does anyone know whether chat engine installation is allowed in the websites provided by Frihost.?

Thanks and regards,
Satheesh
adri
I got just one question for this tutorial:
I want members to have access to more pages of my site, and I want that guests can't see them, how can I do that?
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.