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.
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
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:
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:
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
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
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
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
