Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 04:55 PM
Junior Member
 
Join Date: Apr 2008
Posts: 3
PeeHPee is on a distinguished road
PHP Login - With Cookies [TUT #1]

First, I'll provide you with the files then break them down into pieces

Database.php:

PHP Code:
<?php
$MySQL 
= array();
$MySQL2 = array();
$MySQLDB = array();
$MySQL['host'] = 'localhost';// Host name
$MySQL['user'] = 'user'// Username
$MySQL['pass'] = 'pass'// Password
$MySQL['name'] = 'database'// Database Name
$MySQL['error_site'] = 'un-defined'//Site to go to if there's an error
$conn mysql_connect($MySQL['host'] , $MySQL['user'] , $MySQL['pass']) or die('[MYSQL] Error connecting to SQL server. Please report the bug <a href="' $MySQL['error_site'] . '">here</a>."');
mysql_select_db($MySQL['name']) 
?>
Login.php (To verify)

PHP Code:
<?php
function Redirect($URL) {
    echo 
"<html><head><meta http-equiv=\"refresh\" content=\"0;url=$URL\"></head></html>";
}
    
$Login['user_cookie'] = 'tut_user';
    
$Login['user_cookie_pass'] = 'tut_pass';
    
$Login['site'] = 'your_site.com';
if (isset ( 
$_POST ['username'] ) && isset ( $_POST ['password'] )) {
    include 
"database.php";
    
$user $_POST ['username'];
    
$pass $_POST ['password'];
    
$sql mysql_query "SELECT `password` FROM `user` WHERE username = '" $user "'" );
    
$fetch_em mysql_fetch_array $sql );
    
$numrows mysql_num_rows $sql );
    
    if (
$pass == $fetch_em ["password"] && ! empty ( $fetch_em ["password"] )) {
        
$valid_user 1;
    } else {
        
$valid_user 0;
    }
    
    if (
$valid_user == 1) {
        
        
setcookie $Login['user_cookie'] , $usertime () + 60 60"*.$Login['site']" );
        
setcookie $Login['user_cookie_pass'] , $passtime () + 60 60"*.$Login['site']" );
        
header "location: $Login['site']" );
    } else {
        
Redirect "$Login['site']/login.php?error=invalid" );
        die ( 
"" );
    }

} else {
    die ( 
mysql_error () );
}
?>
index.php (The page with the form)

PHP Code:
<?php if(isset($_GET['error'])){ if($_GET['error'] == "invalid"){echo "<font color=\"red\"><b>Invalid username or password. Please try again.</b></font>";} }    ?>
<form action="login.php" method="post" autocomplete="off">
<table style="opacity: 1;">
<tbody><tr>
<td>Username:</td>
<td><input size="20" name="username" maxlength="12" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input size="20" name="password" maxlength="20" type="password"></td>
</tr>
<tr>
<td></td>
<td align="center"><input value="Secure Login" type="submit"></td>
</tr>
</tbody></table>
</form>
Table.sql

Code:
CREATE TABLE `user` (
  `username` varchar(20) collate latin1_general_ci NOT NULL default '',
  `password` varchar(20) collate latin1_general_ci default NULL,
  `rights` int(1) NOT NULL default '0',
  PRIMARY KEY  (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
Breaking down Database.php

Nothing really to put here, the functions are pretty obvious "mysql_select_db" selects the database after connecting to the host using mysql_connect.

Breaking down login.php

PHP Code:
        $user $_POST ['username'];
    
$pass $_POST ['password'];
    
$sql mysql_query "SELECT `password` FROM `user` WHERE username = '" $user "'" ); 
Gets the defined username and password and checks if they work or not

PHP Code:
        $fetch_em mysql_fetch_array $sql );
    
$numrows mysql_num_rows $sql ); 
Fetches all the passwords and puts them into an array then numbers the rows

PHP Code:
    if ($pass == $fetch_em ["password"] && ! empty ( $fetch_em ["password"] )) {
        
$valid_user 1;
    } else {
        
$valid_user 0;
    } 
If the password is the same as the password for the user in the database, it sets the valid user variable to one, if it's anything else apart from that it's a fail.

PHP Code:
if ($valid_user == 1) {
        
        
setcookie $Login['user_cookie'] , $usertime () + 60 60"*.$Login['site']" );
        
setcookie $Login['user_cookie_pass'] , $passtime () + 60 60"$Login['user_cookie']" ); 
Sets the cookie that's been defined in the variables if everything is correct.

Breaking down Index.php

PHP Code:
<?php if(isset($_GET['error'])){ if($_GET['error'] == "invalid"){echo "<font color=\"red\"><b>Invalid username or password. Please try again.</b></font>";} }    ?>
If there's a get request in the URL (?error=invalid) it displays Invalid Username or password

And that's all I can explain basically.

Thanks for reading - Next a user registration system.

Reply With Quote
Reply

Bookmarks

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 On
Forum Jump


All times are GMT +2. The time now is 02:05 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com