User Registration, Login and logout Script With PHP and MySQL - With Validation

1 comment


User Registration and Login Script With PHP and MySQL: Did you just designed your login page and looking for some tips to create User Registration and Login Script with PHP and MySQL then you are at the right place, we Hack For Security Have published tutorial about User Registration and Login Script With PHP and MySQL.

Login and logout is the most important thing for any good website and here we will teach you how to create user registration and login script in PHP and MySQL

User Registration and Login Script With PHP and MySQL


User Registration, Login and logout Script With PHP and MySQL - With Validation
User Registration, Login and logout Script With PHP and MySQL - With Validation
Here is the steps for the user registration and login script with PHP and MySQL.

First of all Create a new Database and then create on table named users.

And then simply RUN bellow query in the Database.
CREATE TABLE IF NOT EXISTS `users` (
  `u_id` int(4) NOT NULL AUTO_INCREMENT,
  `u_name` varchar(50) NOT NULL,
  `u_email` varchar(50) NOT NULL,
  `u_pwd` varchar(50) NOT NULL,
  `u_abt` text NOT NULL,
  `u_img` text NOT NULL,
  PRIMARY KEY (`u_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Now your user table is ready which includes user id, user name, user email, user password, user profile picture and about user.

Now our database is ready and we have to create php code for the same to register users.

We have divided PHP code in to three parts: one process_register.php, register.php and connect.inc.php.

<form action="process_register.php" method="post" enctype="multipart/form-data">
                           User Name <br />
                            <input type="text" name="nm" />
                            <br><br>
                           
                            Email <br />
                            <input type="text" name="eml" />
                            <br><br>
                           
                            Password <br />
                            <input type="password" name="pwd" />
                            <br><br>
                           
                            Confirm Password <br />
                            <input type="password" name="cpwd" />
                            <br><br>
                           
                            Profile Image <br />
                            <input type="file" name="img" />
                            <br><br>
                           
                            About You <br />
                            <textarea name="abt"></textarea>
                            <br><br>
                           
                            <input type="submit" />
                        </form>    
So make sure that you do the same and in another file, process_register.php the code will be like this.

<?php

    if(empty($_POST)) { exit; }
   
    $errors = array();
   
    //-- General validation -------------------------------
   
    if(empty($_POST["nm"]))
        $errors[] = "Name was empty.";
    if(empty($_POST["eml"]))
        $errors[] = "Email was empty.";
    if(empty($_POST["pwd"]))
        $errors[] = "Password was empty.";
    if(empty($_POST["cpwd"]))
        $errors[] = "Confirm Password was empty.";
    if($_POST["pwd"] != $_POST["cpwd"])
        $errors[] = "Password mismatch.";
    if(strlen($_POST["pwd"]) < 5)
        $errors[] = "Password must be 5+ chars.";
    if(empty($_POST["abt"]))
        $errors[] = "About you was empty.";
   
    //-- file validation ----------------------------------
   
    if(empty($_FILES["img"]["name"]))
        $errors[] = "Image was empty.";
    if($_FILES["img"]["error"] != 0)
        $errors[] = "Error uploading file. try again.";
    $ext = strtolower(substr($_FILES["img"]["name"], -4));
    if($ext != ".jpg")
        $errors[] = "Upload jpg only";
       
    //-- Show error if any --------------------------------
   
    if( ! empty($errors))
    {
        echo "<b>Error(s):</b><hr />";
        foreach($errors as $e) {
            echo "<li>".$e."</li>";
        }
        exit;
    }
   
    $fnm = time() . "_" . $_FILES["img"]["name"];
    move_uploaded_file($_FILES["img"]["tmp_name"], "uploads/".$fnm);
   
    require_once("connect.inc.php");
    $q = "insert into users(u_name, u_email, u_pwd, u_abt, u_img) values('".$_POST["nm"]."','".$_POST["eml"]."','".$_POST["pwd"]."','".$_POST["abt"]."','".$fnm."')";
    mysql_query($q, $link) or die(mysql_error());
   
    header("location: register.php");

?>
We have also included form validations in all the forums and we have also included image validation too.

the code of connect.inc.php is as following.


<?php
    $link = mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("video_site", $link) or die(mysql_error());
?>
 Now our registration code is complete and we have to make login script.

its also divided in to two parts one is login.php and another one is process_login.php, code of login.php is as follow.
    <form action="process_login.php" method="post">
                            User Name <br />
                            <input type="text" name="unm" />
                            <br><br>
                           
                            Password <br />
                            <input type="password" name="pwd" />
                            <br><br>
                           
                            <input type="submit" value="Login" />
                        </form>   
 and the code for Process_login.php is as follow.

<?php session_start();

    if(empty($_POST)) { exit; }
   
    $errors = array();
   
    //-- General validation -------------------------------
   
    if(empty($_POST["unm"]))
        $errors[] = "UserName was empty.";
    if(empty($_POST["pwd"]))
        $errors[] = "Password was empty.";
   
    if( ! empty($errors))
    {
        echo "<b>Error(s):</b><hr />";
        foreach($errors as $e) {
            echo "<li>".$e."</li>";
        }
        exit;
    }

    require_once("connect.inc.php");
    $q = "select * from users where u_name='".$_POST["unm"]."' and u_pwd='".$_POST["pwd"]."'";
    $res = mysql_query($q, $link) or die(mysql_error());
   
    if(mysql_num_rows($res) != 0)
    {
        $row = mysql_fetch_assoc($res);
       
        $_SESSION["uid"] = $row["u_id"];
        $_SESSION["unm"] = $row["u_name"];
       
        header("location: index.php");
    }
    else { echo "java de."; }
   
?>
Now you are good to go, we have completed login and registration script in PHP and MySQL and we will also create one another thing and that is logout.

We should include logout script too that's how we can check it for multiple times and the code for logout.php is as follow.

<?php session_start(); session_destroy(); header("location: index.php"); ?>
Just create one php file and put this code inside it and you are good to go!

Now we have successfully coded  User Registration, Login and logout Script With PHP and MySQL - With Validation and if you have any question about it then feel free to make comments.

1 comment: