/***************************************************************************
README.TXT
Login/Registering script
Requirements: PHP and MySQL server.
Includes: config.php, login.php, mysql.php, welcome.php, login.sql
***************************************************************************/
Intructions:
1-First download and unzip the files.
2-Edit config.php and fill out the blank variables, i.e.: $path, $db_host, $db_username, $db_password, $db_dbname.
3-Then execute the login.sql file in a MySQL remote manager such as PHPmyAdmin which is probably the most famous.
4-Include those 3 lines of code in your php web pages:
You can also add an optional welcome message by using:
5-Finally upload all the modified files to your server via a FTP client or a SSH client.
Here is the code for the php files and for the database table creation (zip file download at the end of the page):
/***************************************************************************
config.php
***************************************************************************/
/***************************************************************************
login.php
***************************************************************************/
/***************************************************************************
mysql.php
***************************************************************************/
/***************************************************************************
welcome.php
***************************************************************************/
/***************************************************************************
login.sql
***************************************************************************/
ENJOY!!!
README.TXT
Login/Registering script
Requirements: PHP and MySQL server.
Includes: config.php, login.php, mysql.php, welcome.php, login.sql
***************************************************************************/
Intructions:
1-First download and unzip the files.
2-Edit config.php and fill out the blank variables, i.e.: $path, $db_host, $db_username, $db_password, $db_dbname.
3-Then execute the login.sql file in a MySQL remote manager such as PHPmyAdmin which is probably the most famous.
4-Include those 3 lines of code in your php web pages:
Code :
// hide source code
// hide line numbers
// hide source code
// hide line numbers
1
2 <? require_once("config.php"); ?>
3 <a href="login.php?mode=login">Login</a>
4 <a href="login.php?mode=register">Register</a>
You can also add an optional welcome message by using:
5-Finally upload all the modified files to your server via a FTP client or a SSH client.
Here is the code for the php files and for the database table creation (zip file download at the end of the page):
/***************************************************************************
config.php
***************************************************************************/
Code :
// hide source code
// hide line numbers
// hide source code
// hide line numbers
1
2 <?php
3
4 //Website URL
5 $path = "http://www.mysite.com";
6
7 //Database settings
8 $db_host = "localhost";
9 $db_username = "";
10 $db_password = "";
11 $db_dbname = "";
12
13 //Include MySQL Class
14 require_once("mysql.php");
15
16 //Execute Class Instance
17 $db = new connection($db_username, $db_password, $db_host, $db_dbname);
18 global $db;
19
20 //User Array
21 if(isset($_COOKIE['user_id'])){
22 $loggedIn = 1;
23 $sql = "SELECT * FROM users WHERE id = '$_COOKIE[user_id]'";
24 $query = $db->query($sql);
25 $user = $db->fetch($query);
26 global $user;
27 global $loggedIn;
28 } else {
29 $loggedIn = 0;
30 global $loggedIn;
31 }
32 ?>
33
/***************************************************************************
login.php
***************************************************************************/
Code :
// hide source code
// hide line numbers
// hide source code
// hide line numbers
1 <?php
2
3 //Include Config File
4 require_once("config.php");
5
6 //Create needed forms
7
8 //Login Form
9 $loginForm = '<form name="userLogin" method="post" action="login.php?mode=login">';
10 $loginForm .= 'Username: <input type="text" name="username"><br />';
11 $loginForm .= 'Password: <input type="password" name="password"><br />';
12 $loginForm .= '<input type="submit" name="LogIn" value="Login">';
13 $loginForm .= '</form>';
14
15 //Register Form
16 $registerForm = '<form name="userRegister" method="post" action="login.php?mode=register">';
17 $registerForm .= 'Username: <input type="text" name="username"><br />';
18 $registerForm .= 'Password: <input type="password" name="password"><br />';
19 $registerForm .= 'Email: <input type="text" name="email"><br />';
20 $registerForm .= '<input type="submit" name="newUser" value="Register">';
21 $registerForm .= '</form>';
22
23 //Check to see if Query String is set
24 if(isset($_GET['mode'])){
25
26 //Check if mode is login
27 if($_GET['mode'] == 'login'){
28
29 //Check if login form is submitted
30 if(isset($_POST['LogIn'])){
31
32 //Query database
33 $sql = "SELECT * FROM users WHERE username = '$_POST[username]'";
34 $query = $db->query($sql);
35
36 //Check if username exists
37 if($db->rows($query) == 0){
38 die("Username does not exist!");
39 }
40
41 //Get user array
42 $arr = $db->fetch($query);
43
44 //Check password
45 if($_POST['password'] != $arr['password']){
46 die("Incorrect password!");
47 }
48
49 //Set Cookie
50 setcookie("user_id", $arr["id"]);
51
52 //Redirect to homepage
53 header("location:". $path);
54
55 } else {
56
57 //Display login form
58 echo $loginForm;
59 }
60 } elseif($_GET['mode'] == 'register'){
61
62 //Check if register form is submitted
63 if(isset($_POST['newUser'])){
64
65 //Check if username is taken
66 $sql = "SELECT * FROM users WHERE username = '$_POST[username]'";
67 $query = $db->query($sql);
68 if($db->rows($query) > 0){
69 die("Username already taken!");
70 }
71
72 //Check if email is taken
73 $sql = "SELECT * FROM users WHERE email = '$_POST[email]'";
74 $query = $db->query($sql);
75 if($db->rows($query) > 0){
76 die("There is already a user with that email!");
77 }
78
79 //Insert user into database
80 $sql = "INSERT INTO users (username, password, email) values ('$_POST[username]', '$_POST[password]', '$_POST[email]')";
81 $db->query($sql);
82
83 //Redirect to homepage
84 header("location:". $path);
85
86 } else {
87 //Display Register Form
88 echo $registerForm;
89 }
90 } else {
91 //Redirect to homepage
92 header("location:". $path);
93 }
94 } else {
95 //Redirect to homepage
96 header("location:". $path);
97 }
98
99
100 ?>
/***************************************************************************
mysql.php
***************************************************************************/
Code :
// hide source code
// hide line numbers
// hide source code
// hide line numbers
1 <?php
2 /*
3 This MySQL Class is one i use on many of my sites, it is very handy, saves me the time and effort of rewriting
4 MySQL functions everytime i make a new site, I hope you find it useful.
5 */
6
7 class connection
8 {
9 function connection( $dbuser, $dbpass, $dbhost, $dbname )
10 {
11 $this->user = $dbuser;
12 $this->pass = $dbpass;
13 $this->host = $dbhost;
14 $this->db_name = $dbname;
15
16 $this->db_id = mysql_connect ( $this->host, $this->user, $this->pass ) or die ( "CONNECTION: " . mysql_error () );
17
18 if ( $this->db_id )
19 {
20 $select_db = mysql_select_db ( $this->db_name ) or die ( "DB: " . mysql_error () );
21 return $this->db_id;
22 }
23 else
24 {
25 return false;
26 }
27 }
28
29 function query ( $sql = "", $transaction = FALSE )
30 {
31 unset ( $this->query_result );
32 if ( $sql != "" )
33 {
34 $this->num_queries++;
35
36 $this->query_result = mysql_query ( $sql, $this->db_id );
37 }
38 if ($this->query_result )
39 {
40 unset ($this->row[$this->query_result] );
41 unset ($this->rowset[$this->query_result] );
42 return $this->query_result;
43 }
44 else
45 {
46 return ( $transaction == END_TRANSACTION ) ? true : false;
47 }
48 }
49
50 function numrows ( $query )
51 {
52 if ( !$query )
53 {
54 $query = $this->query_result;
55 }
56
57 return ( $query ) ? mysql_num_rows ( $query ) : false;
58 }
59
60 function fetchrow ( $query )
61 {
62 if( !$query )
63 {
64 $query = $this->query_result;
65 }
66
67 if( $query )
68 {
69 $this->row[$query] = mysql_fetch_array ( $query );
70 return $this->row[$query];
71 }
72 else
73 {
74 return false;
75 }
76 }
77
78 function fetchobject ( $query )
79 {
80 if ( !query )
81 {
82 $query = $this->query_result;
83 }
84 if ( $query )
85 {
86 $this->row[$query] = mysql_fetch_object ( $query );
87 return $this->row[$query];
88 }
89 else
90 {
91 return false;
92 }
93 }
94
95 function affectedrows()
96 {
97 if($this->db_id)
98 {
99 $result = @mysql_affected_rows($this->db_id);
100 return $result;
101 }
102 else
103 {
104 return false;
105 }
106 }
107
108 function nextid()
109 {
110 if ( $this->db_id )
111 {
112 $result = @mysql_insert_id ( $this->db_id );
113 return $result;
114 }
115 else
116 {
117 return false;
118 }
119 }
120 }
121 ?>
/***************************************************************************
welcome.php
***************************************************************************/
Code :
// hide source code
// hide line numbers
// hide source code
// hide line numbers
1 <?php
2 if($loggedIn == 1){
3 //Message to show if they ARE logged in
4 echo 'Welcome back '. $user['username'];
5 } else {
6 //Message to show if they are NOT logged in
7 echo 'You are not logged in,
8 click <a href="login.php?mode=login">Here</a> to login,
9 or <a href="login.php?mode=register">Here</a> to register';
10 }
11 ?>
/***************************************************************************
login.sql
***************************************************************************/
Code :
// hide source code
// hide line numbers
// hide source code
// hide line numbers
1 CREATE TABLE users (
2 id int(10) unsigned NOT NULL auto_increment,
3 username varchar(15) NOT NULL default '',
4 password varchar(15) NOT NULL default '',
5 email varchar(40) NOT NULL default '',
6 PRIMARY KEY (id)
7 ) TYPE=MyISAM
ENJOY!!!

Donation:If you like our free quality work, make a donation by using Paypal and tell us what you would like to see improved on our site for the next few months. |
|













Page Info













