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, 05:10 PM
Junior Member
 
Join Date: Apr 2008
Posts: 3
PeeHPee is on a distinguished road
PHP Registration - [TUT #2]

We'll be using the data from my last tutorial here <- Clicky

check.php (To check if they can have that username or not)
PHP Code:
<?php
include ("database.php");
function 
showError($errorr){
        echo 
"<b><font color=\"red\">".$errorr."</font></b><br /><br /><br >";
        include 
'form.php';
    }
    if(!isset(
$_POST['username']) || !isset($_POST['password'])){
        
showError("You did not set the username or the password field."); die();
    }
    
$username $_POST['username'];
    
$password $_POST['password'];
    
    
$row1 mysql_query("SELECT * FROM `user` WHERE `username` = '"$username ."'");
    
$check1 mysql_num_rows($row1);
    if(
$check1 != 0){
        
showError("This username is already in use."); die();
    }

    
$validname false;
    
$validpass false;
    
$namelength strlen($username);
    
$passlength strlen($password);
    
$validchars = array('a''A''b''B''c''C''d''D''e''E''f''F''g''G''h''H''i''I''j''J''k''K''l''L''m''M''n''N''o''O''p''P''q''Q''r''R''s''S''t''T''u''U''v''V''w''W''x''X''y''Y''z''Z'' ''_''1''2''3''4''5''6''7''8''9''0');
    for (
$i 0$i $namelength$i++) {
        for(
$b 0$b count($validchars); $b++){
            if(
$username[$i] == $validchars[$b]){
                
$validname true;
            }
        }
    }
    for (
$i 0$i $passlength$i++) {
        for(
$b 0$b count($validchars); $b++){
            if(
$password[$i] == $validchars[$b]){
                
$validpass true;
            }
        }
    }
    if(
$namelength || $namelength 12 || $validname == false){
        
showError("Invalid username, your username should be between 5 and 12 characters and should not contain any special characters."); die();
    }
    if(
$passlength || $passlength 20 || $validpass == false){
        
showError("Invalid password, your password should be between 5 and 20 characters and should not contain any special characters."); die();
    }
    
    if(
$validname && $validpass){
        @
mysql_query("INSERT INTO `user` (`username` , `password` , `rights`) VALUES ('".$username."', '".$password."','0')") or die(mysql_error());
        
$fp = @fopen"account_logs.txt""a" );
        @
fputs($fp"ACCOUNT CREATED: ".getEnv("REMOTE_ADDR")"\n" );
        echo 
"Account created successfully!";
    } else {
        echo 
"Error, please contact the administrator";
    }
?>
Form.php (With the form on it, obvisouly)

PHP Code:
<form method="post" action="check.php" name="reg">
Username: <input type="text" name="username" maxlength="12"><br />
Password: <input type="password" name="password" maxlength="20"><br />
<
input type="submit" value="Check Username" name="submit">
</
form
Breaking down check.php

PHP Code:
    if(!isset($_POST['username']) || !isset($_POST['password'])){
        
showError("You did not set the username or the password field."); die();
    } 
If the user didn't put a value for the fields, it'll show this error.

PHP Code:
        $username $_POST['username'];
    
$password $_POST['password'];
    
    
$row1 mysql_query("SELECT * FROM `user` WHERE `username` = '"$username ."'");
    
$check1 mysql_num_rows($row1);
    if(
$check1 != 0){
        
showError("This username is already in use."); die();
    } 
Gets the value from the fields and checks if they are in use

PHP Code:
$validname false;
    
$validpass false;
    
$namelength strlen($username); //Returns the length of the given string .
    
$passlength strlen($password); //Returns the length of the given string .
    
$validchars = array('a''A''b''B''c''C''d''D''e''E''f''F''g''G''h''H''i''I''j''J''k''K''l''L''m''M''n''N''o''O''p''P''q''Q''r''R''s''S''t''T''u''U''v''V''w''W''x''X''y''Y''z''Z'' ''_''1''2''3''4''5''6''7''8''9''0');
    for (
$i 0$i $namelength$i++) {
        for(
$b 0$b count($validchars); $b++){
            if(
$username[$i] == $validchars[$b]){
                
$validname true;
            }
        }
    }
    for (
$i 0$i $passlength$i++) {
        for(
$b 0$b count($validchars); $b++){
            if(
$password[$i] == $validchars[$b]){
                
$validpass true;
            }
        }
    } 
All this is used to check if the username/password is valid, if it's the amount of characters that are needed and if there's an invalid character in it, (say ~ for example)

PHP Code:
if($namelength || $namelength 12 || $validname == false){
        
showError("Invalid username, your username should be between 5 and 12 characters and should not contain any special characters."); die();
    }
    if(
$passlength || $passlength 20 || $validpass == false){
        
showError("Invalid password, your password should be between 5 and 20 characters and should not contain any special characters."); die();
    } 
If it's not the right length or it contains an invalid character it will give this error.

PHP Code:
if($validname && $validpass){
        @
mysql_query("INSERT INTO `user` (`username` , `password` , `rights`) VALUES ('".$username."', '".$password."','0')") or die(mysql_error());
        
$fp = @fopen"../logging/accounts.txt""a" );
        @
fputs($fp"ACCOUNT CREATED: ".getEnv("REMOTE_ADDR""\n");
        echo 
"Account created successfully!";
    } else {
        echo 
"Error, please contact the administrator";
    } 
If everything's ok, insert into the database

Nothing to explain in Form.php, it's just a form.

Demo

Please visit my friends Web site to keep me posting tutorials

__________________
If I helped you, please visit http://rapidcashbux.com/

Last edited by PeeHPee; 04-11-2008 at 12:52 AM.
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 07:48 PM
VIP Member
 
Join Date: Mar 2008
Location: United Kingdom
Posts: 163
Robbb is on a distinguished road
Send a message via MSN to Robbb Send a message via Skype™ to Robbb
nice tutorial mate!
simple, easy to follow steps. Keep up the good work!
(just a little note, wud be cool to put a link at the top of each tutorial, linking to the last)
Reply With Quote
  #3 (permalink)  
Old 04-11-2008, 12:51 AM
Junior Member
 
Join Date: Apr 2008
Posts: 3
PeeHPee is on a distinguished road
Quote:
Originally Posted by Robbb View Post
nice tutorial mate!
simple, easy to follow steps. Keep up the good work!
(just a little note, wud be cool to put a link at the top of each tutorial, linking to the last)
Oops, thought I did... I'll update it now.
__________________
If I helped you, please visit http://rapidcashbux.com/
Reply With Quote
  #4 (permalink)  
Old 04-12-2008, 08:46 PM
Senior Member
 
Join Date: May 2007
Posts: 181
Blog Entries: 1
skyfe is on a distinguished road
Nice tutorial, good job
Reply With Quote
  #5 (permalink)  
Old 07-18-2008, 05:14 PM
Junior Member
 
Join Date: Feb 2008
Posts: 25
RavenStar is on a distinguished road
Not a bad tutorial, but wouldn't it be easier to use preg_replace to remove 'invalid' characters? That would be much easier and I'm sure also easier to explain/understand as a beginner.

It's also not wise to store usernames or passwords in a cookie, for both user and server security, as I believe you're doing in your previous tutorial that you linked to.
Reply With Quote
  #6 (permalink)  
Old 10-21-2008, 09:56 AM
Junior Member
 
Join Date: Aug 2008
Posts: 25
crazy_lo0p is on a distinguished road
. good tutorial. .thanks
Reply With Quote
  #7 (permalink)  
Old 10-22-2008, 10:04 PM
Junior Member
 
Join Date: Aug 2008
Posts: 25
brumaluco is on a distinguished road
Very Good Tutorial
Reply With Quote
  #8 (permalink)  
Old 10-27-2008, 04:06 PM
Member
 
Join Date: Oct 2008
Location: Clearwater, FL USA
Posts: 41
gmarks is on a distinguished road
Send a message via Yahoo to gmarks Send a message via Skype™ to gmarks
Smashing! Thanks.
Ginger
Reply With Quote
  #9 (permalink)  
Old 11-30-2008, 12:48 PM
Junior Member
 
Join Date: Nov 2008
Posts: 26
ash74 is on a distinguished road
thank for great sharing

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 01:23 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