Go Back   Internet Business Forums > Design & Development

Reply
 
Thread Tools Display Modes
Old 17-12-2006, 12:33 PM   #1 (permalink)
Senior Member
 
Join Date: Nov 2006
Posts: 119
Default [PHP] Login script with MySQL

Hey!

First of all, the MySQL code.

Code:
CREATE TABLE `members` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;

INSERT INTO `members` VALUES (1, 'Username', 'Password');

main_login.php

[html]<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.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>[/html]


checklogin.php

PHP Code:
<?php
$host
="localhost"// Host name 
$username=""// Mysql username 
$password=""// Mysql password 
$db_name="test"// Database name 
$tbl_name="members"// Table name 

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

// username and password sent from signup form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['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_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo 
"Wrong Username or Password";
}
?>

login_success.php

PHP Code:
<?php // Check if session is not registered , redirect back to main page. 
// Put this code in first line of web page. 

session_start();
if(!
session_is_registered(myusername)){
header("location:main_login.php");
}
?>

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

Logout.php

PHP Code:
<?php
// Put this code in first line of web page. 
session_start();
session_destroy();
?>


FOR PHP5 USERS -> checklogin.php


PHP Code:
<?php
ob_start
();
$host="localhost"// Host name 
$username=""// Mysql username 
$password=""// Mysql password 
$db_name="test"// Database name 
$tbl_name="members"// Table name 

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

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['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_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo 
"Wrong Username or Password";
}

ob_end_flush();
?>
DoomGuard is offline   Reply With Quote
Old 17-12-2006, 07:07 PM   #2 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [PHP] Login script with MySQL

Good script ... but what do you have to set like on top of every page?

(I know... but others, who don't know PHP that well won't. Please add in your topic .)

Also, you should use the PHP-tags, instead of the CODE-tags:
[ php] [/ php]
(without the spaces)
army is offline   Reply With Quote
Old 18-12-2006, 05:27 AM   #3 (permalink)
Member
 
addik's Avatar
 
Join Date: Oct 2006
Posts: 79
Default Re: [PHP] Login script with MySQL

Can I see a live demo of this script. I want to to a see a live demo before I try it.
addik is offline   Reply With Quote
Old 18-12-2006, 09:46 AM   #4 (permalink)
Senior Member
 
Join Date: Nov 2006
Posts: 119
Default Re: [PHP] Login script with MySQL

sorry . but i dun have a live demo .. it just a very simple login script ..
and also thx army for helping me to edit the code tag into php tag ..
thank..
DoomGuard is offline   Reply With Quote
Old 18-12-2006, 12:58 PM   #5 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [PHP] Login script with MySQL

Here is a demo (hosted by me!):

http://styx.freehostia.com/login/main_login.php
----> Username: demo
----> Password: demo


Note, password isn't encrypted ... and the input field for the password is not hidden. If you want to make it hidden (******) you have to do this:

Chnage the following line in main_login.php: [HTML]<input name="mypassword" type="text" id="mypassword">[/HTML]
Into this: [HTML]<input name="mypassword" type="password" id="mypassword">[/HTML]

An example of this is here:
http://styx.freehostia.com/login/main_login2.php
----> Username: demo
----> Password: demo
army is offline   Reply With Quote
Old 21-12-2006, 06:05 PM   #6 (permalink)
Senior Member
 
Join Date: Nov 2006
Posts: 263
Default Re: [PHP] Login script with MySQL

very nice tutorial. Thanks for sharing.
babuks is offline   Reply With Quote
Old 11-01-2007, 02:37 AM   #7 (permalink)
Senior Member
 
charlesbw's Avatar
 
Join Date: Dec 2006
Posts: 214
Default Re: [PHP] Login script with MySQL

can i intergrate this into a html site, without messing up the whole thing?
charlesbw is offline   Reply With Quote
Old 11-01-2007, 03:02 AM   #8 (permalink)
Senior Member
 
charlesbw's Avatar
 
Join Date: Dec 2006
Posts: 214
Default Re: [PHP] Login script with MySQL

nice job, i just succesfully intergrated it into the site. How do i create a registration form for it and impose registeration restrictions to certain areas of the site?
charlesbw is offline   Reply With Quote
Old 13-01-2007, 04:30 PM   #9 (permalink)
Member
 
Join Date: Nov 2006
Posts: 47
Default Re: [PHP] Login script with MySQL

To check if a user is logged in or not, you need to put this code on the top of the page:
PHP Code:
<?php
session_start
();
if(!
session_is_registered(myusername)){
header("location:main_login.php");
}
?>
Zeggy is offline   Reply With Quote
Old 13-01-2007, 04:33 PM   #10 (permalink)
Senior Member
 
charlesbw's Avatar
 
Join Date: Dec 2006
Posts: 214
Default Re: [PHP] Login script with MySQL

thank you so very much am gonna try that now
charlesbw is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


All times are GMT +1. The time now is 11:13 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.