Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: User Accounts

  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question User Accounts

    I want to make user accounts on my website like any popular website like facebook, google, youtube, and other websites have. I have a PHP server on IIS7, but other then that I have no experiance what so ever. I am sorry if this should have been posted in MySQL, but any help would be great.

  • #2
    New Coder
    Join Date
    Jan 2012
    Posts
    53
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Post

    Quote Originally Posted by monkeydo98 View Post
    I want to make user accounts on my website like any popular website like facebook, google, youtube, and other websites have. I have a PHP server on IIS7, but other then that I have no experiance what so ever. I am sorry if this should have been posted in MySQL, but any help would be great.
    If you want, I'll post the code for my registration/login page and explain it all, as well as how to set up a MySQL database.

  • Users who have thanked DarkLaika for this post:

    monkeydo98 (01-22-2012)

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    There's mountains of information on the internet about this particular topic. Google is your best friend
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thank you, i can go with that

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    53
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Post

    Quote Originally Posted by monkeydo98 View Post
    thank you, i can go with that
    Ok, this will be a very long explanation, so here I go.

    First things first, you're going to need to create a MySQL database to store your member's information. I hope you have phpMyAdmin because that's the only database manager I know how to use.

    Firstly, create a MySQL database. How you do this will vary from host to host, but normally it's in the front page of phpMyAdmin or, if you're using cPanel, there will be a MySQL databases option where you can set the name of your database. Call the database something that corresponds to the site you're making.

    Now, in phpMyAdmin, on the left-hand side, click on the database you've just created, and it will take you to a list of the tables in it. You need to create a new table, so near the bottom of the page, where it says "Create table on database *your database name here*" give it a name of users and 8 columns.

    Call the first column "id". Give it a type of INT, an index of PRIMARY and turn on AUTO_INCREMENT.

    Call the next column "username". Give it a type of VARCHAR and a length of however many characters you want the max-length name to be.

    Call the column after that "password". Give it a type of VARCHAR and a length of 100.

    The next column should be called "email", give it a type of VARCHAR and a length of 100.

    Next column needs to be called "is_admin". Give it a type of TINYINT and a length of 1.

    Then make another column named "is_verified". Give it a type of TINYINT and a length of 1.

    Call the next column "verification_code". Give it a type of VARCHAR and a length of 100.

    The second-last column should be called "user_ip". Give this a type of VARCHAR and a length of 50.

    And lastly, called the last column "date_joined". Give it a type of VARCHAR and a length of 20.

    So that's your database set up, now on to the fun part- the PHP.

    Create a new folder in your root directory, call it "register".

    Firstly get reCAPTCHA. Register your site there, then download the PHP Library and extract all the files into the same folder as the registration form. You'll also need to keep the public key and the private key for use.

    Inside the folder., create a file called index.php and insert the following code into it:

    PHP Code:
    <?php
    //Start the session, make sure this is present on all your PHP pages.
    session_start(); 

    //Connect to the MySQL database- replace "username" and "password" with the username and password for your control panel, and change "your_database" with the name of the database you created earlier.
    mysql_connect("localhost""username""password");
    mysql_select_db("your_database");

    //Fetches the reCAPTCHA functions.
    require_once('recaptchalib.php');

    //Replace "public_key" with your reCAPTCHA public key and "private_key" with your reCAPTCHA private key. 
    $publickey "public_key";
    $privatekey "private_key";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Register an Account</title>
        <meta charset="utf-8">
    </head>
    <body>
    <h3>Register</h3>
        <br />
        <?php
            
    //If the form is submitted, do the following...
            
    if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
                
    $response recaptcha_check_answer($privatekey,
                
    $_SERVER["REMOTE_ADDR"],
                
    $_POST["recaptcha_challenge_field"],
                
    $_POST["recaptcha_response_field"]); 

                
    //If the reCAPTCHA is filled out, proceed with the process.
                
    if ($response->is_valid){ 
                    
    //Use the username equal to what the visitor entered.
                    
    $username mysql_real_escape_string($_POST['username']);
                    
    //Get the password that the visitor entered.
                    
    $password mysql_real_escape_string($_POST['password']); 
                    
    //Use the email that the visitor entered.
                    
    $email mysql_real_escape_string($_POST['email']); 
                    
    //Get a verification code for the visitor. This is just the email's MD5 hash.
                    
    $verification_code md5($email);
                    
    //Get the visitor's IP Address.
                    
    $user_ip $_SERVER['REMOTE_ADDR'];
                    
    //Encrypt the password entered into an MD5 hash.
                    
    $password_md5 md5($password);

                    
    //Set the errored status to false by default.
                    
    $errored false;
                    
    //Get usernames from the table you created.
                    
    $sql mysql_query("SELECT username FROM users WHERE username = '".$username."'");
                        
    //Create an error message if the username is already taken.
                        
    if (mysql_num_rows($sql) > 0
                            { 
                                
    $errored true;
                                
    $errorMsg "Username already taken<br/>";
                            }
                            
                        
    //Create an error message if the username is longer than 30. You can change the maximum amount of characters if you wish.
                        
    if(strlen($username) > 30)
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must be between 3 and 30 characters long";
                            }

                        
    //Create an error message if the username is shorter than 3. You can change the minimum amount of characters if you wish.        
                        
    if(strlen($username) < 3)
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must be between 3 and 30 characters long";
                            }

                        
    //Create an error message if the username contains any special characters. If you don't want this to happen delete this bit of code.    
                        
    if(!preg_match("/^[A-Za-z0-9]*$/",$username))
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must contain only letters and numbers";
                            }

                    
    //Get emails from the table you created.
                    
    $sql mysql_query("SELECT email FROM users WHERE email = '".$email."'");
                            
                        
    //Create an error message if the email is already in use.
                        
    if (mysql_num_rows($sql)>0
                            { 
                                
    $errored true;
                                
    $errorMsg "Email already in use";
                            }

                        
    //Create an error message if the email is not valid.    
                        
    if(!filter_var($emailFILTER_VALIDATE_EMAIL))
                            {
                                
    $errored true;
                                
    $errorMsg "Please enter a valid email address";
                            }

                    
    //Create an error message if the password contains any special characters. If you do not want this to happen, delete this bit of code.    
                    
    if(!preg_match("/^[A-Za-z0-9]*$/",$password))
                        {
                            
    $errored true;
                            
    $errorMsg "Password must contain only letters and numbers";
                        }

                    
    //If there is an error, display the appropiate error message.
                    
    if($errored)
                        {
                            echo 
    "$errorMsg";
                        } 
                    else
                        {
                            
    //Insert all information into the database, before sending a verification email to the entered email address.
                            
    mysql_query("INSERT INTO users (username, password, email, verification_code, is_verified, user_ip, is_admin) VALUES ( '$username', '$password', '$email', '$verification_code', 0, '$user_ip', 0)") or die (mysql_error()); 

                            
    //Send the mail to the entered email address.
                            
    $to  $email;

                            
    //The subject for the email. Change this to whatever you want.
                            
    $subject 'Verify Your Account';

                            
    //The message body of the email. Change it to whatever you want, but don't forget to include the verification link!
                            
    $message "
                                <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML+RDFa 1.0//EN' 'http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd'>
                                <html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
                                <head>
                                    <title>Verify Your Account</title>
                                </head>
                                <body>
                                <h3>Verify Your Account</h3>
                                <br />
                                <p>
                                Hi there <b>$email</b>. <b>$user_ip</b> wants to create an account in the name of <b>$username</b> at <a href='http://www.yoursite.com'>yoursite.com</a>.
                                Please confirm this was you by clicking on the link below:
                                http://www.yoursite.com/verify/$verification_code
                                <br />
                                Thanks!
                                </p>
                                </body>
                                </html>
                            "
    ;

                            
    $headers  'MIME-Version: 1.0' "\r\n";
                            
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";

                            
    //Change who the email is sent from.
                            
    $headers .= 'From: Minecraft Global Ban List <register@mcbanlist.net>' "\r\n";

                            
    //Send the email.
                            
    mail($to$subject$message$headers);
      
                            
    //Finish the registration process.
                            
    echo "An email has been sent to <b>$email</b>, please check both your email inbox to verify your account.";
                        }

                        
    //If reCAPTCHA code isn't correct, echo error.
                    
    }    elseif(!$response->is_valid
                        {
                            echo 
    "Whoops, the reCAPTCHA code which you entered appears to be incorrect. Try again?";
                        }
                    }
        
    ?>
        <br />
        <form method="post">
            <!--- Username --->
            Username:&nbsp    <input name="username" type="text" align="center" />
            <br />
            <!--- Password --->
            Password:&nbsp    <input type="password" name="password" />
            <br />
            <!--- Email --->
            Email:&nbsp    <input name="email" type="text" />
            <br />
            <!--- Display the reCAPTCHA input. --->
            <?php echo recaptcha_get_html($publickey$error); ?>
            <br />
            <p>Please take a moment to fill out the reCAPTCHA. Just type out the words that you see on-screen. reCAPTCHA helps us prevent spam.</p>
            <br />
            <!--- Submit the data. --->
            <input type="submit" class="button" value="Submit" />
            <br />
        </form>
    </body>
    </html>
    So now that you've got a registration form, you need to create that verification page that new members will get in their email. Create a new file inside the register folder, call it verify.php and put the following code into it:

    PHP Code:
    <?php
    //Start the session, make sure this is present on all your PHP pages.
    session_start(); 

    //Connect to the MySQL database- replace "username" and "password" with the username and password for your control panel, and change "your_database" with the name of the database you created earlier.
    mysql_connect("localhost""username""password");
    mysql_select_db("your_database");
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>User Verification</title>
    </head>
    <body>
    <h3>User Verification</h3>
    <?php
        
    //If request is formatted properly, do the following...
        
    if(isset($_GET['verify']))
            {
                
    //Get the verification code from the url.
                
    $verify mysql_real_escape_string($_GET['verify']);

                
    //Update the user's verification status.
                
    mysql_query("UPDATE users SET is_verified = 1 WHERE verification_code = '$verify' LIMIT 1");

                
    //If it works...
                
    if(mysql_affected_rows() == 1)
                    {
                        echo 
    "Account verified";
                        
    //Redirect to the login page in 5 seconds.
                        
    echo "<meta http-equiv='refresh' content='5; url=http://www.yoursite.com/login/'>";
                    }
                
    //If an error occurs...
                
    else
                    {
                        echo 
    "An error occured while processing your submission. Please try again later";
                    }
            }
    ?>
    </body>
    </html>
    Now in the root folder of your sites, there should be a file called .htaccess You'll need to edit it. If it's not there, create it.

    Put the following code inside it:

    Code:
    RewriteEngine on
    
    	RewriteRule ^verify/verify/([A-Za-z0-9]+)/$ verify.php?verify=$1
    If this all works, then we can make a start on a login page.

  • Users who have thanked DarkLaika for this post:

    monkeydo98 (01-24-2012)

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by DarkLaika View Post
    Ok, this will be a very long explanation, so here I go.

    First things first, you're going to need to create a MySQL database to store your member's information. I hope you have phpMyAdmin because that's the only database manager I know how to use.

    Firstly, create a MySQL database. How you do this will vary from host to host, but normally it's in the front page of phpMyAdmin or, if you're using cPanel, there will be a MySQL databases option where you can set the name of your database. Call the database something that corresponds to the site you're making.

    Now, in phpMyAdmin, on the left-hand side, click on the database you've just created, and it will take you to a list of the tables in it. You need to create a new table, so near the bottom of the page, where it says "Create table on database *your database name here*" give it a name of users and 8 columns.

    Call the first column "id". Give it a type of INT, an index of PRIMARY and turn on AUTO_INCREMENT.

    Call the next column "username". Give it a type of VARCHAR and a length of however many characters you want the max-length name to be.

    Call the column after that "password". Give it a type of VARCHAR and a length of 100.

    The next column should be called "email", give it a type of VARCHAR and a length of 100.

    Next column needs to be called "is_admin". Give it a type of TINYINT and a length of 1.

    Then make another column named "is_verified". Give it a type of TINYINT and a length of 1.

    Call the next column "verification_code". Give it a type of VARCHAR and a length of 100.

    The second-last column should be called "user_ip". Give this a type of VARCHAR and a length of 50.

    And lastly, called the last column "date_joined". Give it a type of VARCHAR and a length of 20.

    So that's your database set up, now on to the fun part- the PHP.

    Create a new folder in your root directory, call it "register".

    Firstly get reCAPTCHA. Register your site there, then download the PHP Library and extract all the files into the same folder as the registration form. You'll also need to keep the public key and the private key for use.

    Inside the folder., create a file called index.php and insert the following code into it:

    PHP Code:
    <?php
    //Start the session, make sure this is present on all your PHP pages.
    session_start(); 

    //Connect to the MySQL database- replace "username" and "password" with the username and password for your control panel, and change "your_database" with the name of the database you created earlier.
    mysql_connect("localhost""username""password");
    mysql_select_db("your_database");

    //Fetches the reCAPTCHA functions.
    require_once('recaptchalib.php');

    //Replace "public_key" with your reCAPTCHA public key and "private_key" with your reCAPTCHA private key. 
    $publickey "public_key";
    $privatekey "private_key";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Register an Account</title>
        <meta charset="utf-8">
    </head>
    <body>
    <h3>Register</h3>
        <br />
        <?php
            
    //If the form is submitted, do the following...
            
    if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
                
    $response recaptcha_check_answer($privatekey,
                
    $_SERVER["REMOTE_ADDR"],
                
    $_POST["recaptcha_challenge_field"],
                
    $_POST["recaptcha_response_field"]); 

                
    //If the reCAPTCHA is filled out, proceed with the process.
                
    if ($response->is_valid){ 
                    
    //Use the username equal to what the visitor entered.
                    
    $username mysql_real_escape_string($_POST['username']);
                    
    //Get the password that the visitor entered.
                    
    $password mysql_real_escape_string($_POST['password']); 
                    
    //Use the email that the visitor entered.
                    
    $email mysql_real_escape_string($_POST['email']); 
                    
    //Get a verification code for the visitor. This is just the email's MD5 hash.
                    
    $verification_code md5($email);
                    
    //Get the visitor's IP Address.
                    
    $user_ip $_SERVER['REMOTE_ADDR'];
                    
    //Encrypt the password entered into an MD5 hash.
                    
    $password_md5 md5($password);

                    
    //Set the errored status to false by default.
                    
    $errored false;
                    
    //Get usernames from the table you created.
                    
    $sql mysql_query("SELECT username FROM users WHERE username = '".$username."'");
                        
    //Create an error message if the username is already taken.
                        
    if (mysql_num_rows($sql) > 0
                            { 
                                
    $errored true;
                                
    $errorMsg "Username already taken<br/>";
                            }
                            
                        
    //Create an error message if the username is longer than 30. You can change the maximum amount of characters if you wish.
                        
    if(strlen($username) > 30)
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must be between 3 and 30 characters long";
                            }

                        
    //Create an error message if the username is shorter than 3. You can change the minimum amount of characters if you wish.        
                        
    if(strlen($username) < 3)
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must be between 3 and 30 characters long";
                            }

                        
    //Create an error message if the username contains any special characters. If you don't want this to happen delete this bit of code.    
                        
    if(!preg_match("/^[A-Za-z0-9]*$/",$username))
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must contain only letters and numbers";
                            }

                    
    //Get emails from the table you created.
                    
    $sql mysql_query("SELECT email FROM users WHERE email = '".$email."'");
                            
                        
    //Create an error message if the email is already in use.
                        
    if (mysql_num_rows($sql)>0
                            { 
                                
    $errored true;
                                
    $errorMsg "Email already in use";
                            }

                        
    //Create an error message if the email is not valid.    
                        
    if(!filter_var($emailFILTER_VALIDATE_EMAIL))
                            {
                                
    $errored true;
                                
    $errorMsg "Please enter a valid email address";
                            }

                    
    //Create an error message if the password contains any special characters. If you do not want this to happen, delete this bit of code.    
                    
    if(!preg_match("/^[A-Za-z0-9]*$/",$password))
                        {
                            
    $errored true;
                            
    $errorMsg "Password must contain only letters and numbers";
                        }

                    
    //If there is an error, display the appropiate error message.
                    
    if($errored)
                        {
                            echo 
    "$errorMsg";
                        } 
                    else
                        {
                            
    //Insert all information into the database, before sending a verification email to the entered email address.
                            
    mysql_query("INSERT INTO users (username, password, email, verification_code, is_verified, user_ip, is_admin) VALUES ( '$username', '$password', '$email', '$verification_code', 0, '$user_ip', 0)") or die (mysql_error()); 

                            
    //Send the mail to the entered email address.
                            
    $to  $email;

                            
    //The subject for the email. Change this to whatever you want.
                            
    $subject 'Verify Your Account';

                            
    //The message body of the email. Change it to whatever you want, but don't forget to include the verification link!
                            
    $message "
                                <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML+RDFa 1.0//EN' 'http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd'>
                                <html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
                                <head>
                                    <title>Verify Your Account</title>
                                </head>
                                <body>
                                <h3>Verify Your Account</h3>
                                <br />
                                <p>
                                Hi there <b>$email</b>. <b>$user_ip</b> wants to create an account in the name of <b>$username</b> at <a href='http://www.yoursite.com'>yoursite.com</a>.
                                Please confirm this was you by clicking on the link below:
                                http://www.yoursite.com/verify/$verification_code
                                <br />
                                Thanks!
                                </p>
                                </body>
                                </html>
                            "
    ;

                            
    $headers  'MIME-Version: 1.0' "\r\n";
                            
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";

                            
    //Change who the email is sent from.
                            
    $headers .= 'From: Minecraft Global Ban List <register@mcbanlist.net>' "\r\n";

                            
    //Send the email.
                            
    mail($to$subject$message$headers);
      
                            
    //Finish the registration process.
                            
    echo "An email has been sent to <b>$email</b>, please check both your email inbox to verify your account.";
                        }

                        
    //If reCAPTCHA code isn't correct, echo error.
                    
    }    elseif(!$response->is_valid
                        {
                            echo 
    "Whoops, the reCAPTCHA code which you entered appears to be incorrect. Try again?";
                        }
                    }
        
    ?>
        <br />
        <form method="post">
            <!--- Username --->
            Username:&nbsp    <input name="username" type="text" align="center" />
            <br />
            <!--- Password --->
            Password:&nbsp    <input type="password" name="password" />
            <br />
            <!--- Email --->
            Email:&nbsp    <input name="email" type="text" />
            <br />
            <!--- Display the reCAPTCHA input. --->
            <?php echo recaptcha_get_html($publickey$error); ?>
            <br />
            <p>Please take a moment to fill out the reCAPTCHA. Just type out the words that you see on-screen. reCAPTCHA helps us prevent spam.</p>
            <br />
            <!--- Submit the data. --->
            <input type="submit" class="button" value="Submit" />
            <br />
        </form>
    </body>
    </html>
    So now that you've got a registration form, you need to create that verification page that new members will get in their email. Create a new file inside the register folder, call it verify.php and put the following code into it:

    PHP Code:
    <?php
    //Start the session, make sure this is present on all your PHP pages.
    session_start(); 

    //Connect to the MySQL database- replace "username" and "password" with the username and password for your control panel, and change "your_database" with the name of the database you created earlier.
    mysql_connect("localhost""username""password");
    mysql_select_db("your_database");
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>User Verification</title>
    </head>
    <body>
    <h3>User Verification</h3>
    <?php
        
    //If request is formatted properly, do the following...
        
    if(isset($_GET['verify']))
            {
                
    //Get the verification code from the url.
                
    $verify mysql_real_escape_string($_GET['verify']);

                
    //Update the user's verification status.
                
    mysql_query("UPDATE users SET is_verified = 1 WHERE verification_code = '$verify' LIMIT 1");

                
    //If it works...
                
    if(mysql_affected_rows() == 1)
                    {
                        echo 
    "Account verified";
                        
    //Redirect to the login page in 5 seconds.
                        
    echo "<meta http-equiv='refresh' content='5; url=http://www.yoursite.com/login/'>";
                    }
                
    //If an error occurs...
                
    else
                    {
                        echo 
    "An error occured while processing your submission. Please try again later";
                    }
            }
    ?>
    </body>
    </html>
    Now in the root folder of your sites, there should be a file called .htaccess You'll need to edit it. If it's not there, create it.

    Put the following code inside it:

    Code:
    RewriteEngine on
    
    	RewriteRule ^verify/verify/([A-Za-z0-9]+)/$ verify.php?verify=$1
    If this all works, then we can make a start on a login page.
    This sort of thing should really be posted in the 'Post a snippet' forum.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    this is very helpful but i am having trouble with phpmyadmin. i cant start until i figure it out

  • #8
    12k
    12k is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    You dont even have to use phpmyadmin. That is just a query browser. You can also use navicat, or u can use MySql Query browser provided by mysql.

  • #9
    New Coder
    Join Date
    Jan 2012
    Posts
    53
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Post

    Quote Originally Posted by monkeydo98 View Post
    this is very helpful but i am having trouble with phpmyadmin. i cant start until i figure it out
    What sort of problem are you having?

  • #10
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Php

    Quote Originally Posted by DarkLaika View Post
    Ok, this will be a very long explanation, so here I go.

    First things first, you're going to need to create a MySQL database to store your member's information. I hope you have phpMyAdmin because that's the only database manager I know how to use.

    Firstly, create a MySQL database. How you do this will vary from host to host, but normally it's in the front page of phpMyAdmin or, if you're using cPanel, there will be a MySQL databases option where you can set the name of your database. Call the database something that corresponds to the site you're making.

    Now, in phpMyAdmin, on the left-hand side, click on the database you've just created, and it will take you to a list of the tables in it. You need to create a new table, so near the bottom of the page, where it says "Create table on database *your database name here*" give it a name of users and 8 columns.

    Call the first column "id". Give it a type of INT, an index of PRIMARY and turn on AUTO_INCREMENT.

    Call the next column "username". Give it a type of VARCHAR and a length of however many characters you want the max-length name to be.

    Call the column after that "password". Give it a type of VARCHAR and a length of 100.

    The next column should be called "email", give it a type of VARCHAR and a length of 100.

    Next column needs to be called "is_admin". Give it a type of TINYINT and a length of 1.

    Then make another column named "is_verified". Give it a type of TINYINT and a length of 1.

    Call the next column "verification_code". Give it a type of VARCHAR and a length of 100.

    The second-last column should be called "user_ip". Give this a type of VARCHAR and a length of 50.

    And lastly, called the last column "date_joined". Give it a type of VARCHAR and a length of 20.

    So that's your database set up, now on to the fun part- the PHP.

    Create a new folder in your root directory, call it "register".

    Firstly get reCAPTCHA. Register your site there, then download the PHP Library and extract all the files into the same folder as the registration form. You'll also need to keep the public key and the private key for use.

    Inside the folder., create a file called index.php and insert the following code into it:

    PHP Code:
    <?php
    //Start the session, make sure this is present on all your PHP pages.
    session_start(); 

    //Connect to the MySQL database- replace "username" and "password" with the username and password for your control panel, and change "your_database" with the name of the database you created earlier.
    mysql_connect("localhost""username""password");
    mysql_select_db("your_database");

    //Fetches the reCAPTCHA functions.
    require_once('recaptchalib.php');

    //Replace "public_key" with your reCAPTCHA public key and "private_key" with your reCAPTCHA private key. 
    $publickey "public_key";
    $privatekey "private_key";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Register an Account</title>
        <meta charset="utf-8">
    </head>
    <body>
    <h3>Register</h3>
        <br />
        <?php
            
    //If the form is submitted, do the following...
            
    if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
                
    $response recaptcha_check_answer($privatekey,
                
    $_SERVER["REMOTE_ADDR"],
                
    $_POST["recaptcha_challenge_field"],
                
    $_POST["recaptcha_response_field"]); 

                
    //If the reCAPTCHA is filled out, proceed with the process.
                
    if ($response->is_valid){ 
                    
    //Use the username equal to what the visitor entered.
                    
    $username mysql_real_escape_string($_POST['username']);
                    
    //Get the password that the visitor entered.
                    
    $password mysql_real_escape_string($_POST['password']); 
                    
    //Use the email that the visitor entered.
                    
    $email mysql_real_escape_string($_POST['email']); 
                    
    //Get a verification code for the visitor. This is just the email's MD5 hash.
                    
    $verification_code md5($email);
                    
    //Get the visitor's IP Address.
                    
    $user_ip $_SERVER['REMOTE_ADDR'];
                    
    //Encrypt the password entered into an MD5 hash.
                    
    $password_md5 md5($password);

                    
    //Set the errored status to false by default.
                    
    $errored false;
                    
    //Get usernames from the table you created.
                    
    $sql mysql_query("SELECT username FROM users WHERE username = '".$username."'");
                        
    //Create an error message if the username is already taken.
                        
    if (mysql_num_rows($sql) > 0
                            { 
                                
    $errored true;
                                
    $errorMsg "Username already taken<br/>";
                            }
                            
                        
    //Create an error message if the username is longer than 30. You can change the maximum amount of characters if you wish.
                        
    if(strlen($username) > 30)
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must be between 3 and 30 characters long";
                            }

                        
    //Create an error message if the username is shorter than 3. You can change the minimum amount of characters if you wish.        
                        
    if(strlen($username) < 3)
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must be between 3 and 30 characters long";
                            }

                        
    //Create an error message if the username contains any special characters. If you don't want this to happen delete this bit of code.    
                        
    if(!preg_match("/^[A-Za-z0-9]*$/",$username))
                            {
                                
    $errored true;
                                
    $errorMsg "Your username must contain only letters and numbers";
                            }

                    
    //Get emails from the table you created.
                    
    $sql mysql_query("SELECT email FROM users WHERE email = '".$email."'");
                            
                        
    //Create an error message if the email is already in use.
                        
    if (mysql_num_rows($sql)>0
                            { 
                                
    $errored true;
                                
    $errorMsg "Email already in use";
                            }

                        
    //Create an error message if the email is not valid.    
                        
    if(!filter_var($emailFILTER_VALIDATE_EMAIL))
                            {
                                
    $errored true;
                                
    $errorMsg "Please enter a valid email address";
                            }

                    
    //Create an error message if the password contains any special characters. If you do not want this to happen, delete this bit of code.    
                    
    if(!preg_match("/^[A-Za-z0-9]*$/",$password))
                        {
                            
    $errored true;
                            
    $errorMsg "Password must contain only letters and numbers";
                        }

                    
    //If there is an error, display the appropiate error message.
                    
    if($errored)
                        {
                            echo 
    "$errorMsg";
                        } 
                    else
                        {
                            
    //Insert all information into the database, before sending a verification email to the entered email address.
                            
    mysql_query("INSERT INTO users (username, password, email, verification_code, is_verified, user_ip, is_admin) VALUES ( '$username', '$password', '$email', '$verification_code', 0, '$user_ip', 0)") or die (mysql_error()); 

                            
    //Send the mail to the entered email address.
                            
    $to  $email;

                            
    //The subject for the email. Change this to whatever you want.
                            
    $subject 'Verify Your Account';

                            
    //The message body of the email. Change it to whatever you want, but don't forget to include the verification link!
                            
    $message "
                                <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML+RDFa 1.0//EN' 'http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd'>
                                <html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
                                <head>
                                    <title>Verify Your Account</title>
                                </head>
                                <body>
                                <h3>Verify Your Account</h3>
                                <br />
                                <p>
                                Hi there <b>$email</b>. <b>$user_ip</b> wants to create an account in the name of <b>$username</b> at <a href='http://www.yoursite.com'>yoursite.com</a>.
                                Please confirm this was you by clicking on the link below:
                                http://www.yoursite.com/verify/$verification_code
                                <br />
                                Thanks!
                                </p>
                                </body>
                                </html>
                            "
    ;

                            
    $headers  'MIME-Version: 1.0' "\r\n";
                            
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";

                            
    //Change who the email is sent from.
                            
    $headers .= 'From: Minecraft Global Ban List <register@mcbanlist.net>' "\r\n";

                            
    //Send the email.
                            
    mail($to$subject$message$headers);
      
                            
    //Finish the registration process.
                            
    echo "An email has been sent to <b>$email</b>, please check both your email inbox to verify your account.";
                        }

                        
    //If reCAPTCHA code isn't correct, echo error.
                    
    }    elseif(!$response->is_valid
                        {
                            echo 
    "Whoops, the reCAPTCHA code which you entered appears to be incorrect. Try again?";
                        }
                    }
        
    ?>
        <br />
        <form method="post">
            <!--- Username --->
            Username:&nbsp    <input name="username" type="text" align="center" />
            <br />
            <!--- Password --->
            Password:&nbsp    <input type="password" name="password" />
            <br />
            <!--- Email --->
            Email:&nbsp    <input name="email" type="text" />
            <br />
            <!--- Display the reCAPTCHA input. --->
            <?php echo recaptcha_get_html($publickey$error); ?>
            <br />
            <p>Please take a moment to fill out the reCAPTCHA. Just type out the words that you see on-screen. reCAPTCHA helps us prevent spam.</p>
            <br />
            <!--- Submit the data. --->
            <input type="submit" class="button" value="Submit" />
            <br />
        </form>
    </body>
    </html>
    So now that you've got a registration form, you need to create that verification page that new members will get in their email. Create a new file inside the register folder, call it verify.php and put the following code into it:

    PHP Code:
    <?php
    //Start the session, make sure this is present on all your PHP pages.
    session_start(); 

    //Connect to the MySQL database- replace "username" and "password" with the username and password for your control panel, and change "your_database" with the name of the database you created earlier.
    mysql_connect("localhost""username""password");
    mysql_select_db("your_database");
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>User Verification</title>
    </head>
    <body>
    <h3>User Verification</h3>
    <?php
        
    //If request is formatted properly, do the following...
        
    if(isset($_GET['verify']))
            {
                
    //Get the verification code from the url.
                
    $verify mysql_real_escape_string($_GET['verify']);

                
    //Update the user's verification status.
                
    mysql_query("UPDATE users SET is_verified = 1 WHERE verification_code = '$verify' LIMIT 1");

                
    //If it works...
                
    if(mysql_affected_rows() == 1)
                    {
                        echo 
    "Account verified";
                        
    //Redirect to the login page in 5 seconds.
                        
    echo "<meta http-equiv='refresh' content='5; url=http://www.yoursite.com/login/'>";
                    }
                
    //If an error occurs...
                
    else
                    {
                        echo 
    "An error occured while processing your submission. Please try again later";
                    }
            }
    ?>
    </body>
    </html>
    Now in the root folder of your sites, there should be a file called .htaccess You'll need to edit it. If it's not there, create it.

    Put the following code inside it:

    Code:
    RewriteEngine on
    
    	RewriteRule ^verify/verify/([A-Za-z0-9]+)/$ verify.php?verify=$1
    If this all works, then we can make a start on a login page.
    thank you I well follow this.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •