Zymic Forums

Webmaster resources

Zymic IRC Server

Chat in real time at irc.zymic.com - Learn More

Welcome

Welcome to the Zymic webmaster forums. Our forums are here to provide people the free ability to discuss a range of websites related topics such as design, development coding and marketing.

In order to post you will need to register for a zymic account or if you already have one simply login by using the form on the left.

left Zymic Webmaster ForumsZymic Free Web HostingTutorials right
9 Pages V   1 2 3 > »    Reply to this topic Start new topic
left right
uncled1023
post Mar 7 2008, 05:54 AM
Post #1


Super Ninja
****

Group: Members
Posts: 353
Joined: 24-February 08
Member No.: 11,718



(tutorial part 1 of many!!!)




1)create a database. name it whatever you want.(EX: members)
2)create a new file and call it db_connect.php
insert the following code into the file and change the database variables to match yours.
CODE
<?php



$dbhost = 'localhost';


// your database username.
    $dbusername = 'database_username';

// the password that corresponds to the above username.
    $dbpasswd = 'password';

// the database name that your username is associated with.
    $database_name = 'database_name';

    
    $connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")

    or die ("Couldn't connect to server.");


$db = mysql_select_db("$database_name", $connection)

    or die("Couldn't select database.");


// we write this later on, ignore for now.


include('check_login.php');


?>


3)now save it.
4)now go to your phpadmin and create a table with this query:
CODE
CREATE TABLE users (
id int(10) NOT NULL auto_increment,
username varchar(40),
password varchar(50),
regdate varchar(20),
email varchar(100),
website varchar(150),
location varchar(150),
show_email int(2) DEFAULT '0',
last_login varchar(20),
PRIMARY KEY(id))


5)now create a new file and call it register.php and insert the following code:
CODE
<?php
require('db_connect.php');    // database connect script.
?>

<html>
<head>
<title>Register an Account</title>
</head>
<body>

<?php

if (isset($_POST['submit'])) { // if form has been submitted
    /* check they filled in what they supposed to,
    passwords matched, username
    isn't already taken, etc. */

    if (!$_POST['uname'] || !$_POST['passwd'] ||
        !$_POST['passwd_again'] || !$_POST['email']) {
        die('You did not fill in a required field.');
    }

    // check if username exists in database.

    if (!get_magic_quotes_gpc()) {
        $_POST['uname'] = addslashes($_POST['uname']);
    }

    $qry = "SELECT username FROM users WHERE username = '".$_POST['uname']."'";
                $sqlmembers = mysql_query($qry);
                $name_check = mysql_fetch_array ($sqlmembers);
                $name_checkk = mysql_num_rows ($sqlmembers);
    
    if ($name_checkk != 0) {
        die('Sorry, the username: <strong>'.$_POST['uname'].'</strong>'
          . ' is already taken, please pick another one.');
    }

    // check passwords match

    if ($_POST['passwd'] != $_POST['passwd_again']) {
        die('Passwords did not match.');
    }

    // check e-mail format

    if (!preg_match("/.*@.*..*/", $_POST['email']) ||
         preg_match("/(<|>)/", $_POST['email'])) {
        die('Invalid e-mail address.');
    }

    // no HTML tags in username, website, location, password

    $_POST['uname'] = strip_tags($_POST['uname']);
    $_POST['passwd'] = strip_tags($_POST['passwd']);
    $_POST['website'] = strip_tags($_POST['website']);
    $_POST['location'] = strip_tags($_POST['location']);

    // check show_email data

    if ($_POST['show_email'] != 0 & $_POST['show_email'] != 1) {
        die('Nope');
    }

    /* the rest of the information is optional, the only thing we need to
    check is if they submitted a website,
    and if so, check the format is ok. */

    if ($_POST['website'] != '' & !preg_match("/^(http|ftp):///", $_POST['website'])) {
        $_POST['website'] = 'http://'.$_POST['website'];
    }

    // now we can add them to the database.
    // encrypt password

    $_POST['passwd'] = md5($_POST['passwd']);

    if (!get_magic_quotes_gpc()) {
        $_POST['passwd'] = addslashes($_POST['passwd']);
        $_POST['email'] = addslashes($_POST['email']);
        $_POST['website'] = addslashes($_POST['website']);
        $_POST['location'] = addslashes($_POST['location']);
    }

    $regdate = date('m d, Y');

    $insert = "INSERT INTO users (
            username,
            password,
            regdate,
            email,
            website,
            location,
            show_email,
            last_login)
            VALUES (
            '".$_POST['uname']."',
            '".$_POST['passwd']."',
            '$regdate',
            '".$_POST['email']."',
            '".$_POST['website']."',
            '".$_POST['location']."',
            '".$_POST['show_email']."',
            'Never')";

    $sqlmembers = mysql_query($insert);
?>

<h1>Registered</h1>

<p>Thank you, your information has been added to the database,
you may now <a href="login.php" title="Login">log in</a>.</p>

<?php

} else {    // if form hasn't been submitted

?>
<h1>Register</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="center" border="1" cellspacing="0" cellpadding="3">
<tr><td>Username*:</td><td>
<input type="text" name="uname" maxlength="40">
</td></tr>
<tr><td>Password*:</td><td>
<input type="password" name="passwd" maxlength="50">
</td></tr>
<tr><td>Confirm Password*:</td><td>
<input type="password" name="passwd_again" maxlength="50">
</td></tr>
<tr><td>E-Mail*:</td><td>
<input type="text" name="email" maxlength="100">
</td></tr>
<tr><td>Website:</td><td>
<input type="text" name="website" maxlength="150">
</td></tr>
<tr><td>Location</td><td>
<input type="text" name="location" maxlength="150">
</td></tr>
<tr><td>Show E-Mail?</td><td>
<select name="show_email">
<option value="1" selected="selected">Yes</option>
<option value="0">No</option></select>
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Sign Up">
</td></tr>
</table>
</form>

<?php

}

?>
</body>
</html>


6)save the file.
7)Create a new file and call it check_login.php and add this code:
CODE
<?php

session_start();

if (!isset($_SESSION['username']) || !isset($_SESSION['password'])) {
    $logged_in = 0;
    return;
} else {

    // remember, $_SESSION['password'] will be encrypted.

    if(!get_magic_quotes_gpc()) {
        $_SESSION['username'] = addslashes($_SESSION['username']);
    }
    // addslashes to session username before using in a query.
    $qry = "SELECT password FROM users WHERE username = '".$_SESSION['username']."'";
    $sqlmembers = mysql_query($qry);
    $pass =  mysql_num_rows($sqlmembers);

    if($pass != 1) {
        $logged_in = 0;
        unset($_SESSION['username']);
        unset($_SESSION['password']);
        // kill incorrect session variables.
    }

    $db_pass =  mysql_fetch_array ($sqlmembers);

    // now we have encrypted pass from DB in
    //$db_pass['password'], stripslashes() just incase:

    $db_pass['password'] = stripslashes($db_pass['password']);
    $_SESSION['password'] = stripslashes($_SESSION['password']);

    //compare:

    if($_SESSION['password'] == $db_pass['password']) {
        // valid password for username
        $logged_in = 1; // they have correct info
                    // in session variables.
    } else {
        $logged_in = 0;
        unset($_SESSION['username']);
        unset($_SESSION['password']);
        // kill incorrect session variables.
    }
}

// clean up
unset($db_pass['password']);

$_SESSION['username'] = stripslashes($_SESSION['username']);

?>


8)Save the file.
9)Create another file and name it login.php and paste the following code:
CODE
<?php

// database connect script.

require 'db_connect.php';

if($logged_in == 1) {
    die('You are already logged in, '.$_SESSION['username'].'.');

}

?>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php

if (isset($_POST['submit'])) { // if form has been submitted



    /* check they filled in what they were supposed to and authenticate */

    if(!$_POST['uname'] | !$_POST['passwd']) {

        die('You did not fill in a required field.');

    }



    // authenticate.



    if (!get_magic_quotes_gpc()) {

        $_POST['uname'] = addslashes($_POST['uname']);

    }



    $qry = "SELECT username, password FROM users WHERE username = '".$_POST['uname']."'";
    $sqlmembers = mysql_query($qry);
$info = mysql_fetch_array ($sqlmembers);

    $check = mysql_num_rows ($sqlmembers);



    if ($check == 0) {

        die('That Account does not exist in our database.');

    }






    // check passwords match



    $_POST['passwd'] = stripslashes($_POST['passwd']);

    $info['password'] = stripslashes($info['password']);

    $_POST['passwd'] = md5($_POST['passwd']);



    if ($_POST['passwd'] != $info['password']) {

        echo "Incorrect password, please try again.";

    }



    // if we get here username and password are correct,

    //register session variables and set last login time.



    $date = date('m d, Y');



    $qry = "UPDATE users SET last_login = '$date' WHERE username = '".$_POST['uname']."'";

    $query=mysql_query($qry);



    $_POST['uname'] = stripslashes($_POST['uname']);

    $_SESSION['username'] = $_POST['uname'];

    $_SESSION['password'] = $_POST['passwd'];



?>
<h1>Logged in</h1>
<p>Welcome back <?php echo $_SESSION['username']; ?>, you are logged in.</p>

<?php

} else {    // if form hasn't been submitted

?>
<h1>Login</h1>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table align="center" border="1" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td>
<input type="text" name="uname" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="passwd" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>
</body>
</html>


10)save the file.
11)now make a new file and call it logout.php and put in the following code. but remember to change the __URL__to the page you want them to go to after they log out.
CODE
<?php

require 'db_connect.php';    // database connect script.

if ($logged_in == 0) {
    die('You are not logged in so you cannot log out.');
}

unset($_SESSION['username']);
unset($_SESSION['password']);
// kill session variables
$_SESSION = array(); // reset session array
session_destroy();   // destroy session.
header('Location: __URL__');
// redirect them to anywhere you like.
?>


12)after you save it, open whatever page you would like to protect.
13)add the following code before your header:
CODE
<?php

require 'db_connect.php';

// require our database connection
// which also contains the check_login.php
// script. We have $logged_in for use.

if ($logged_in == 0) {
    ?>


14)then add the conent you want them to see if they are not logged in.(for example:)
CODE
<?php

require 'db_connect.php';

// require our database connection
// which also contains the check_login.php
// script. We have $logged_in for use.

if ($logged_in == 0) {
    ?>
<html>
<head>
<title>Member Profile</title>
</head>
<body>
Im sorry, but you must be logged in to view this page!
</body>
</html>


15)now add
CODE
<?php
}
else {  ?>

after the end of what you wish to show to those not logged in. Now we will add the content that we will show the logged in users.
CODE
<html>
<head>
<title>Member Profile</title>
</head>
<body>
Welcome to the members only section!!!</body>
</html>


16)now we need to end the if/else statement with the following code:
CODE
<?php
}
?>


17) this should be what your members only page would look like:
CODE
<?php

require 'db_connect.php';

// require our database connection
// which also contains the check_login.php
// script. We have $logged_in for use.

if ($logged_in == 0) {
    ?>
<html>
<head>
<title>Member Profile</title>
</head>
<body>
Im sorry, but you must be logged in to view this page!
</body>
</html>
<?php
}
else {  ?>
<html>
<head>
<title>Member Profile</title>
</head>
<body>
Welcome to the members only section!!!</body>
</html>
<?php
}
?>


now save it.

And there you have it! A nice users script that will automatically add users to the datavase once they register, and will them to log in and access special pages!!

If you have any questions, PM me or visit my website at www.city-scapes.net.
Go to the top of the page 
 
  + Quote Post
wozzym
post Mar 7 2008, 06:13 AM
Post #2


Outrageously Uber Ninja
Group Icon

Group: Community Helper
Posts: 2,197
Joined: 2-March 08
From: Australia
Member No.: 12,578



wow excellent job. ill try it tomorrow. biggrin.gif

Edit: php right? (im just learning php right now)
Go to the top of the page 
 
  + Quote Post
uncled1023
post Mar 7 2008, 04:17 PM
Post #3


Super Ninja
****

Group: Members
Posts: 353
Joined: 24-February 08
Member No.: 11,718



yes, its php
Go to the top of the page 
 
  + Quote Post
Gollum
post Mar 8 2008, 01:26 AM
Post #4


Newbie
*

Group: Members
Posts: 8
Joined: 8-March 08
Member No.: 13,139



Thank you so much. I have been looking for a decent tutorial on simple login scripts and never was able to find an easy to follow one.

Just a little stupid question though (I don't know PHP currently), does the database information in the code (has $ before it) able to be seen by users accessing the site, what I mean is it visible on the source code?
Go to the top of the page 
 
  + Quote Post
uncled1023
post Mar 8 2008, 01:41 AM
Post #5


Super Ninja
****

Group: Members
Posts: 353
Joined: 24-February 08
Member No.: 11,718



no, because it is before the <head>. glad you like it!
Go to the top of the page 
 
  + Quote Post
MrTouz
post Mar 8 2008, 01:41 PM
Post #6


Outrageously Uber Ninja
*******

Group: Members
Posts: 1,196
Joined: 19-September 07
Member No.: 234



what could be good is an other tut based on this one with a user profile ! which is something ALL these scripts are missing
Go to the top of the page 
 
  + Quote Post
Brandon
post Mar 8 2008, 01:59 PM
Post #7


Outrageously Uber Ninja
Group Icon

Group: Community Helper
Posts: 1,746
Joined: 11-November 07
From: HouseMaybe?
Member No.: 2,689



QUOTE(MrTouz @ Mar 8 2008, 01:41 PM) *
what could be good is an other tut based on this one with a user profile ! which is something ALL these scripts are missing

I may do one laters on when im not busy and not onway out tongue.gif
Go to the top of the page 
 
  + Quote Post
Banjo
post Mar 8 2008, 02:19 PM
Post #8


Marvellous Ninja
******

Group: Moderators
Posts: 539
Joined: 25-October 07
From: Windsor, UK
Member No.: 1,866



QUOTE(MrTouz @ Mar 8 2008, 01:41 PM) *
what could be good is an other tut based on this one with a user profile ! which is something ALL these scripts are missing


He has a user profile up on his site which he may release the source for and im currently helping him to make it so you can edit details.
Go to the top of the page 
 
  + Quote Post
wozzym
post Mar 8 2008, 03:40 PM
Post #9


Outrageously Uber Ninja
Group Icon

Group: Community Helper
Posts: 2,197
Joined: 2-March 08
From: Australia
Member No.: 12,578



ya if not ill try to make one tongue.gif
Go to the top of the page 
 
  + Quote Post
uncled1023
post Mar 9 2008, 12:39 AM
Post #10


Super Ninja
****

Group: Members
Posts: 353
Joined: 24-February 08
Member No.: 11,718



yes, i will release the user profile and view others profile system later, and also i might put up a tutorial for a pm system integrating the users...
Go to the top of the page 
 
  + Quote Post
anhbinh
post Mar 9 2008, 01:19 AM
Post #11


Newbie
*

Group: Members
Posts: 5
Joined: 9-March 08
Member No.: 13,272



Good Keep post
Go to the top of the page 
 
  + Quote Post
Soul Of Me
post Mar 10 2008, 08:59 PM
Post #12


Newbie
*

Group: Members
Posts: 24
Joined: 18-January 08
Member No.: 7,304



Added it and it works perfectly!
I would R3p you if it was possible!
Go to the top of the page 
 
  + Quote Post
Brandon
post Mar 10 2008, 10:01 PM
Post #13


Outrageously Uber Ninja
Group Icon

Group: Community Helper
Posts: 1,746
Joined: 11-November 07
From: HouseMaybe?
Member No.: 2,689



QUOTE(Soul Of Me @ Mar 10 2008, 08:59 PM) *
Added it and it works perfectly!
I would R3p you if it was possible!

On his profile where his avatar is there is a like 5star thing you can click on a star which i think gives him rep
Go to the top of the page 
 
  + Quote Post
Brandon
post Mar 12 2008, 02:55 PM
Post #14


Outrageously Uber Ninja
Group Icon

Group: Community Helper
Posts: 1,746
Joined: 11-November 07
From: HouseMaybe?
Member No.: 2,689



I seemed to be haveing problems with this script ...
http://www.crownstyles.com/LogSystem/register.php
What can i do ?

Meh dont matter doing my own
Go to the top of the page 
 
  + Quote Post
uncled1023
post Mar 13 2008, 02:02 AM
Post #15


Super Ninja
****

Group: Members
Posts: 353
Joined: 24-February 08
Member No.: 11,718



it looks like you have the registration alligned right, try alligning it center.
Go to the top of the page 
 
  + Quote Post
Banjo
post Mar 16 2008, 05:40 PM
Post #16


Marvellous Ninja
******

Group: Moderators
Posts: 539
Joined: 25-October 07
From: Windsor, UK
Member No.: 1,866



If you planning on using this then you might want to add something to the registration script.

Read this

http://www.pixel2life.com/publish/tutorial...ord_encryption/

Will make it more secure.
Go to the top of the page 
 
  + Quote Post
ORiOn
post Mar 19 2008, 09:42 PM
Post #17


Newbie
*

Group: Members
Posts: 11
Joined: 2-December 07
From: Suisse
Member No.: 3,981



cant create the table sad.gif


error:

CREATE TABLE users(
id int( 10 ) DEFAULT '0' NOT NULL AUTO_INCREMENT ,
username varchar( 40 ) ,
PASSWORD varchar( 50 ) ,
regdate varchar( 20 ) ,
email varchar( 100 ) ,
website varchar( 150 ) ,
location varchar( 150 ) ,
show_email int( 2 ) DEFAULT '0',
last_login varchar( 20 ) ,
PRIMARY KEY ( id )
)

Mensagens do MySQL : Documentação
#1067 - Invalid default value for 'id'



tanks for any help
Go to the top of the page 
 
  + Quote Post
uncled1023
post Mar 19 2008, 09:52 PM
Post #18


Super Ninja
****

Group: Members
Posts: 353
Joined: 24-February 08
Member No.: 11,718



yea, there is a problem. so i just manually installed it.
Go to the top of the page 
 
  + Quote Post
theraptor
post Mar 21 2008, 01:28 PM
Post #19


Member
**

Group: Members
Posts: 74
Joined: 15-March 08
Member No.: 14,130



This looks good. Is there a way you can import users from and already existing database and add users that join here to that database?
Go to the top of the page 
 
  + Quote Post
uncled1023
post Mar 21 2008, 06:52 PM
Post #20


Super Ninja
****

Group: Members
Posts: 353
Joined: 24-February 08
Member No.: 11,718



yes, you would just need to copy the following and add it right above the "insert into" script.
CODE
$insert = "INSERT INTO users (
            username,
            password,
            regdate,
            email,
            website,
            location,
            show_email,
            last_login)
            VALUES (
            '".$_POST['uname']."',
            '".$_POST['passwd']."',
            '$regdate',
            '".$_POST['email']."',
            '".$_POST['website']."',
            '".$_POST['location']."',
            '".$_POST['show_email']."',
            'Never')";
$add_member = $db_object->query($insert);

    if (DB::isError($add_member)) {
        die($add_member->getMessage());
    }
require('db_connect.php');

and change the "users" to the other table you want to use. now if you are using a different database, then you will also have to make another db_connect2 with the right information for the new database and put a
CODE
require('db_connect2.php');
before the above script, and the entire script.
Go to the top of the page 
 
  + Quote Post
9 Pages V   1 2 3 > » 
 Reply to this topic Start new topic
left right
0 Members:
left right
 


Lo-Fi Version Time is now: 21st November 2009 - 09:38 PM